#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 () {
ios::sync_with_stdio(0);
cin.tie(0);
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 |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
600 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
756 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
860 KB |
Output is correct |
8 |
Correct |
4 ms |
956 KB |
Output is correct |
9 |
Correct |
2 ms |
796 KB |
Output is correct |
10 |
Correct |
4 ms |
860 KB |
Output is correct |
11 |
Correct |
3 ms |
604 KB |
Output is correct |
12 |
Correct |
5 ms |
860 KB |
Output is correct |
13 |
Correct |
69 ms |
744 KB |
Output is correct |
14 |
Correct |
71 ms |
596 KB |
Output is correct |
15 |
Correct |
70 ms |
892 KB |
Output is correct |
16 |
Correct |
73 ms |
744 KB |
Output is correct |
17 |
Correct |
69 ms |
596 KB |
Output is correct |
18 |
Correct |
69 ms |
728 KB |
Output is correct |
19 |
Correct |
73 ms |
852 KB |
Output is correct |
20 |
Correct |
71 ms |
836 KB |
Output is correct |
21 |
Correct |
73 ms |
732 KB |
Output is correct |
22 |
Correct |
70 ms |
596 KB |
Output is correct |
23 |
Correct |
70 ms |
596 KB |
Output is correct |
24 |
Correct |
72 ms |
848 KB |
Output is correct |
25 |
Correct |
76 ms |
848 KB |
Output is correct |
26 |
Correct |
75 ms |
776 KB |
Output is correct |
27 |
Correct |
73 ms |
848 KB |
Output is correct |
28 |
Correct |
71 ms |
852 KB |
Output is correct |
29 |
Correct |
68 ms |
596 KB |
Output is correct |
30 |
Correct |
88 ms |
848 KB |
Output is correct |
31 |
Correct |
71 ms |
848 KB |
Output is correct |
32 |
Correct |
86 ms |
752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
600 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
756 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
3 ms |
860 KB |
Output is correct |
15 |
Correct |
4 ms |
956 KB |
Output is correct |
16 |
Correct |
2 ms |
796 KB |
Output is correct |
17 |
Correct |
4 ms |
860 KB |
Output is correct |
18 |
Correct |
3 ms |
604 KB |
Output is correct |
19 |
Correct |
5 ms |
860 KB |
Output is correct |
20 |
Correct |
69 ms |
744 KB |
Output is correct |
21 |
Correct |
71 ms |
596 KB |
Output is correct |
22 |
Correct |
70 ms |
892 KB |
Output is correct |
23 |
Correct |
73 ms |
744 KB |
Output is correct |
24 |
Correct |
69 ms |
596 KB |
Output is correct |
25 |
Correct |
69 ms |
728 KB |
Output is correct |
26 |
Correct |
73 ms |
852 KB |
Output is correct |
27 |
Correct |
71 ms |
836 KB |
Output is correct |
28 |
Correct |
73 ms |
732 KB |
Output is correct |
29 |
Correct |
70 ms |
596 KB |
Output is correct |
30 |
Correct |
70 ms |
596 KB |
Output is correct |
31 |
Correct |
72 ms |
848 KB |
Output is correct |
32 |
Correct |
76 ms |
848 KB |
Output is correct |
33 |
Correct |
75 ms |
776 KB |
Output is correct |
34 |
Correct |
73 ms |
848 KB |
Output is correct |
35 |
Correct |
71 ms |
852 KB |
Output is correct |
36 |
Correct |
68 ms |
596 KB |
Output is correct |
37 |
Correct |
88 ms |
848 KB |
Output is correct |
38 |
Correct |
71 ms |
848 KB |
Output is correct |
39 |
Correct |
86 ms |
752 KB |
Output is correct |
40 |
Correct |
3 ms |
860 KB |
Output is correct |
41 |
Correct |
16 ms |
1884 KB |
Output is correct |
42 |
Correct |
2 ms |
600 KB |
Output is correct |
43 |
Correct |
14 ms |
1624 KB |
Output is correct |
44 |
Correct |
3 ms |
604 KB |
Output is correct |
45 |
Correct |
17 ms |
1884 KB |
Output is correct |
46 |
Correct |
69 ms |
592 KB |
Output is correct |
47 |
Correct |
70 ms |
848 KB |
Output is correct |
48 |
Correct |
70 ms |
736 KB |
Output is correct |
49 |
Correct |
85 ms |
1560 KB |
Output is correct |
50 |
Correct |
77 ms |
752 KB |
Output is correct |
51 |
Correct |
70 ms |
848 KB |
Output is correct |
52 |
Correct |
78 ms |
592 KB |
Output is correct |
53 |
Correct |
85 ms |
1776 KB |
Output is correct |
54 |
Correct |
69 ms |
748 KB |
Output is correct |
55 |
Correct |
71 ms |
748 KB |
Output is correct |
56 |
Correct |
74 ms |
884 KB |
Output is correct |
57 |
Correct |
89 ms |
1620 KB |
Output is correct |
58 |
Correct |
70 ms |
484 KB |
Output is correct |
59 |
Correct |
73 ms |
848 KB |
Output is correct |
60 |
Correct |
76 ms |
856 KB |
Output is correct |
61 |
Correct |
87 ms |
1872 KB |
Output is correct |
62 |
Correct |
68 ms |
592 KB |
Output is correct |
63 |
Correct |
71 ms |
852 KB |
Output is correct |
64 |
Correct |
71 ms |
852 KB |
Output is correct |
65 |
Correct |
96 ms |
1768 KB |
Output is correct |