Luckyleaves's Blog

stay hungry,stay foolish greedy and lucky

NOI2020 美食家

矩阵快速幂。

Solution

题目传送门

首先发现$n\le 50,T\le 10^9$,大概可以猜到是矩阵快速幂,但是转移的话有边长度的限制,需要拆一下,拆边有点爆炸,考虑拆点,拆完点之后对美食节时间排序,每次转移到下一个美食节开始的位置,然后添加贡献即可,复杂度:$O((5n)^3\cdot k)$,可以发现,这样做复杂度有点爆炸,预处理出2的整次幂的矩阵,每次用原矩阵和转移矩阵相乘,复杂度就可以将为$O((5n)^2 \cdot k)$。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include<bits/stdc++.h>
#define LL long long
#define PII pair<int, pair<int, int> >
using namespace std;
template <class T>
inline void read(T &res)
{
res = 0; bool flag = 0;
char c = getchar();
while('0' > c || c > '9') { if(c == '-') flag = 1; c = getchar();}
while('0' <= c && c <= '9') res = (res << 3) + (res << 1) + (c ^ 48), c = getchar();
if(flag) res = -res;
}
template <class T, class ...Arg>
inline void read(T &res, Arg &...com){ read(res), read(com...);}
template <class T>
void out(T res)
{
if(res > 9) out(res / 10);
putchar(res % 10 + '0');
}
template <class T>
inline void write(T res)
{
if(res < 0) putchar('-'), res = -res;
out(res), putchar('\n');
}
const int N = 250 + 5, Inf = 0x3f3f3f3f;
int n, m, T, k, len;
LL f[N], g[N];
struct Node{
LL a[N][N];
Node operator* (Node b)
{
Node c;
for(int i = 1;i <= len;i ++)
for(int j = 1;j <= len;j ++)
{
c.a[i][j] = -Inf;
for(int k = 1;k <= len;k ++)
c.a[i][j] = max(c.a[i][j], a[i][k] + b.a[k][j]);
}

return c;
}
}a[40], b;
PII t[N];
int main()
{
read(n, m, T, k);
for(int i = 1;i <= 5 * n;i ++)
for(int j = 1;j <= 5 * n;j ++)
a[0].a[i][j] = -Inf;
for(int i = 1;i <= n;i ++) read(f[i]);
for(int i = 1, o, u, v;i <= m;i ++)
read(o, u, v), a[0].a[n * (v - 1) + o][u] = f[u];
for(int i = 1;i <= n;i ++)
for(int j = 1;j <= 4;j ++)
a[0].a[n * (j - 1) + i][n * j + i] = 0;
len = n * 5;
for(int i = 1;i < 32;i ++) a[i] = a[i - 1] * a[i - 1];
for(int i = 1, o, id, u;i <= k;i ++) read(o, id, u), t[i] = {o, {id, u}};
t[0] = {0, {0, 0}};
t[++ k] = {T, {0, 0}};
sort(t + 1, t + k + 1);
for(int i = 2;i <= len;i ++) f[i] = -Inf;
for(int times = 1;times <= k;times ++)
{
int o = t[times].first - t[times - 1].first;
for(int cnt = 0;cnt <= 31;cnt ++)
{
if(o >> cnt & 1)
{
for(int j = 1;j <= len;j ++) g[j] = -Inf;
for(int j = 1;j <= len;j ++)
for(int l = 1;l <= len;l ++)
g[j] = max(g[j], f[l] + a[cnt].a[l][j]);
for(int j = 1;j <= len;j ++) f[j] = g[j];
}
}
f[t[times].second.first] += t[times].second.second;
}
if(f[1] < 0) puts("-1");
else write(f[1]);
return 0;
}s