#include <bits/stdc++.h>
using namespace std;
#define int long long
const int inf = 1e14;
int n;
vector <vector <int>> iden;
vector <vector <int>> mul (vector <vector <int>> a, vector <vector <int>> b) {
vector <vector <int>> ret (n + 1, vector <int> (n + 1));
for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) ret[i][j] = inf;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
for (int k = 1; k <= n; k++) {
ret[i][j] = min(ret[i][j], a[i][k] + b[k][j]);
}
}
}
return ret;
}
vector <vector <int>> power (vector <vector <int>> a, int b) {
if (b == 1) return a;
auto u = power(a, b >> 1);
u = mul(u, u);
if (b & 1) u = mul(u, a);
return u;
}
signed main () {
cin >> n; int m;
cin >> m;
vector <vector <int>> adj(n + 1, vector <int>(n + 1));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i != j) {
adj[i][j] = inf;
}
}
}
while (m--) {
int a, b, c;
cin >> a >> b >> c;
adj[a][b] = min(adj[a][b], c);
}
iden = vector <vector <int>> (n + 1, vector <int> (n + 1));
for (int i = 1; i <= n; i++) iden[i][i] = 1;
int k, q;
cin >> k >> q;
adj = power(adj, k);
while (q--) {
int a, b;
cin >> a >> b;
cout << (adj[a][b] >= inf ? -1 : adj[a][b]) << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
500 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
604 KB |
Output is correct |
2 |
Correct |
8 ms |
604 KB |
Output is correct |
3 |
Correct |
8 ms |
708 KB |
Output is correct |
4 |
Correct |
9 ms |
604 KB |
Output is correct |
5 |
Correct |
9 ms |
604 KB |
Output is correct |
6 |
Correct |
10 ms |
604 KB |
Output is correct |
7 |
Correct |
10 ms |
812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
500 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
12 ms |
1112 KB |
Output is correct |
8 |
Correct |
11 ms |
880 KB |
Output is correct |
9 |
Correct |
8 ms |
600 KB |
Output is correct |
10 |
Correct |
10 ms |
860 KB |
Output is correct |
11 |
Correct |
11 ms |
812 KB |
Output is correct |
12 |
Correct |
12 ms |
928 KB |
Output is correct |
13 |
Correct |
242 ms |
6896 KB |
Output is correct |
14 |
Correct |
263 ms |
6848 KB |
Output is correct |
15 |
Correct |
245 ms |
6968 KB |
Output is correct |
16 |
Correct |
232 ms |
6740 KB |
Output is correct |
17 |
Correct |
232 ms |
6740 KB |
Output is correct |
18 |
Correct |
232 ms |
6740 KB |
Output is correct |
19 |
Correct |
243 ms |
7224 KB |
Output is correct |
20 |
Correct |
237 ms |
6992 KB |
Output is correct |
21 |
Correct |
253 ms |
6976 KB |
Output is correct |
22 |
Correct |
246 ms |
6980 KB |
Output is correct |
23 |
Correct |
245 ms |
7228 KB |
Output is correct |
24 |
Correct |
249 ms |
7096 KB |
Output is correct |
25 |
Correct |
240 ms |
6996 KB |
Output is correct |
26 |
Correct |
240 ms |
7100 KB |
Output is correct |
27 |
Correct |
245 ms |
7236 KB |
Output is correct |
28 |
Correct |
244 ms |
6992 KB |
Output is correct |
29 |
Correct |
232 ms |
6992 KB |
Output is correct |
30 |
Correct |
239 ms |
6980 KB |
Output is correct |
31 |
Correct |
252 ms |
6848 KB |
Output is correct |
32 |
Correct |
232 ms |
6848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
500 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
8 ms |
604 KB |
Output is correct |
8 |
Correct |
8 ms |
604 KB |
Output is correct |
9 |
Correct |
8 ms |
708 KB |
Output is correct |
10 |
Correct |
9 ms |
604 KB |
Output is correct |
11 |
Correct |
9 ms |
604 KB |
Output is correct |
12 |
Correct |
10 ms |
604 KB |
Output is correct |
13 |
Correct |
10 ms |
812 KB |
Output is correct |
14 |
Correct |
12 ms |
1112 KB |
Output is correct |
15 |
Correct |
11 ms |
880 KB |
Output is correct |
16 |
Correct |
8 ms |
600 KB |
Output is correct |
17 |
Correct |
10 ms |
860 KB |
Output is correct |
18 |
Correct |
11 ms |
812 KB |
Output is correct |
19 |
Correct |
12 ms |
928 KB |
Output is correct |
20 |
Correct |
242 ms |
6896 KB |
Output is correct |
21 |
Correct |
263 ms |
6848 KB |
Output is correct |
22 |
Correct |
245 ms |
6968 KB |
Output is correct |
23 |
Correct |
232 ms |
6740 KB |
Output is correct |
24 |
Correct |
232 ms |
6740 KB |
Output is correct |
25 |
Correct |
232 ms |
6740 KB |
Output is correct |
26 |
Correct |
243 ms |
7224 KB |
Output is correct |
27 |
Correct |
237 ms |
6992 KB |
Output is correct |
28 |
Correct |
253 ms |
6976 KB |
Output is correct |
29 |
Correct |
246 ms |
6980 KB |
Output is correct |
30 |
Correct |
245 ms |
7228 KB |
Output is correct |
31 |
Correct |
249 ms |
7096 KB |
Output is correct |
32 |
Correct |
240 ms |
6996 KB |
Output is correct |
33 |
Correct |
240 ms |
7100 KB |
Output is correct |
34 |
Correct |
245 ms |
7236 KB |
Output is correct |
35 |
Correct |
244 ms |
6992 KB |
Output is correct |
36 |
Correct |
232 ms |
6992 KB |
Output is correct |
37 |
Correct |
239 ms |
6980 KB |
Output is correct |
38 |
Correct |
252 ms |
6848 KB |
Output is correct |
39 |
Correct |
232 ms |
6848 KB |
Output is correct |
40 |
Correct |
9 ms |
860 KB |
Output is correct |
41 |
Correct |
25 ms |
1884 KB |
Output is correct |
42 |
Correct |
9 ms |
604 KB |
Output is correct |
43 |
Correct |
22 ms |
1628 KB |
Output is correct |
44 |
Correct |
12 ms |
860 KB |
Output is correct |
45 |
Correct |
25 ms |
1884 KB |
Output is correct |
46 |
Correct |
233 ms |
6740 KB |
Output is correct |
47 |
Correct |
230 ms |
6996 KB |
Output is correct |
48 |
Correct |
234 ms |
6740 KB |
Output is correct |
49 |
Correct |
252 ms |
7904 KB |
Output is correct |
50 |
Correct |
233 ms |
6976 KB |
Output is correct |
51 |
Correct |
237 ms |
6976 KB |
Output is correct |
52 |
Correct |
240 ms |
6840 KB |
Output is correct |
53 |
Correct |
262 ms |
8016 KB |
Output is correct |
54 |
Correct |
235 ms |
6740 KB |
Output is correct |
55 |
Correct |
238 ms |
6852 KB |
Output is correct |
56 |
Correct |
238 ms |
7116 KB |
Output is correct |
57 |
Correct |
254 ms |
8264 KB |
Output is correct |
58 |
Correct |
255 ms |
6992 KB |
Output is correct |
59 |
Correct |
242 ms |
6992 KB |
Output is correct |
60 |
Correct |
260 ms |
7224 KB |
Output is correct |
61 |
Correct |
254 ms |
8116 KB |
Output is correct |
62 |
Correct |
257 ms |
6996 KB |
Output is correct |
63 |
Correct |
230 ms |
6736 KB |
Output is correct |
64 |
Correct |
231 ms |
6716 KB |
Output is correct |
65 |
Correct |
250 ms |
8024 KB |
Output is correct |