#include <bits/stdc++.h>
using namespace std;
const int N = 75;
int n, m, dp[N][N][N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
for (int u = 1; u <= n; ++u) {
for (int v = 1; v <= n; ++v) {
dp[i][u][v] = (u != v? 1e9 : 0);
}
}
}
for (int i = 0; i < m; ++i) {
int u, v, c;
cin >> u >> v >> c;
dp[1][u][v] = min(dp[1][u][v], c);
}
for (int i = 2; i <= n; ++i) {
for (int u = 1; u <= n; ++u) {
for (int v = 1; v <= n; ++v) {
dp[i][u][v] = dp[i - 1][u][v];
}
}
for (int u = 1; u <= n; ++u) {
for (int v = 1; v <= n; ++v) {
for (int k = 1; k <= n; ++k) {
dp[i][u][v] = min(dp[i][u][v], dp[i - 1][u][k] + dp[1][k][v]);
}
}
}
}
int k, q;
cin >> k >> q;
k = min(k, n);
for (int i = 0; i < q; ++i) {
int u, v;
cin >> u >> v;
cout << (dp[k][u][v] == 1e9? -1 : dp[k][u][v]) << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
1840 KB |
Output is correct |
2 |
Correct |
30 ms |
1864 KB |
Output is correct |
3 |
Correct |
30 ms |
1856 KB |
Output is correct |
4 |
Correct |
33 ms |
1848 KB |
Output is correct |
5 |
Correct |
34 ms |
1856 KB |
Output is correct |
6 |
Correct |
31 ms |
1856 KB |
Output is correct |
7 |
Correct |
32 ms |
1856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
31 ms |
1856 KB |
Output is correct |
8 |
Correct |
29 ms |
1860 KB |
Output is correct |
9 |
Correct |
35 ms |
1848 KB |
Output is correct |
10 |
Correct |
33 ms |
1856 KB |
Output is correct |
11 |
Correct |
32 ms |
1856 KB |
Output is correct |
12 |
Correct |
31 ms |
1856 KB |
Output is correct |
13 |
Correct |
125 ms |
2724 KB |
Output is correct |
14 |
Correct |
124 ms |
3144 KB |
Output is correct |
15 |
Correct |
120 ms |
3232 KB |
Output is correct |
16 |
Correct |
131 ms |
7448 KB |
Output is correct |
17 |
Correct |
122 ms |
6660 KB |
Output is correct |
18 |
Correct |
141 ms |
5816 KB |
Output is correct |
19 |
Correct |
145 ms |
7304 KB |
Output is correct |
20 |
Correct |
130 ms |
7160 KB |
Output is correct |
21 |
Correct |
139 ms |
7344 KB |
Output is correct |
22 |
Correct |
122 ms |
7388 KB |
Output is correct |
23 |
Correct |
119 ms |
7200 KB |
Output is correct |
24 |
Correct |
148 ms |
6620 KB |
Output is correct |
25 |
Correct |
130 ms |
6520 KB |
Output is correct |
26 |
Correct |
118 ms |
5208 KB |
Output is correct |
27 |
Correct |
126 ms |
1868 KB |
Output is correct |
28 |
Correct |
125 ms |
1868 KB |
Output is correct |
29 |
Correct |
118 ms |
1980 KB |
Output is correct |
30 |
Correct |
122 ms |
6108 KB |
Output is correct |
31 |
Correct |
132 ms |
7728 KB |
Output is correct |
32 |
Correct |
124 ms |
7336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
30 ms |
1840 KB |
Output is correct |
8 |
Correct |
30 ms |
1864 KB |
Output is correct |
9 |
Correct |
30 ms |
1856 KB |
Output is correct |
10 |
Correct |
33 ms |
1848 KB |
Output is correct |
11 |
Correct |
34 ms |
1856 KB |
Output is correct |
12 |
Correct |
31 ms |
1856 KB |
Output is correct |
13 |
Correct |
32 ms |
1856 KB |
Output is correct |
14 |
Correct |
31 ms |
1856 KB |
Output is correct |
15 |
Correct |
29 ms |
1860 KB |
Output is correct |
16 |
Correct |
35 ms |
1848 KB |
Output is correct |
17 |
Correct |
33 ms |
1856 KB |
Output is correct |
18 |
Correct |
32 ms |
1856 KB |
Output is correct |
19 |
Correct |
31 ms |
1856 KB |
Output is correct |
20 |
Correct |
125 ms |
2724 KB |
Output is correct |
21 |
Correct |
124 ms |
3144 KB |
Output is correct |
22 |
Correct |
120 ms |
3232 KB |
Output is correct |
23 |
Correct |
131 ms |
7448 KB |
Output is correct |
24 |
Correct |
122 ms |
6660 KB |
Output is correct |
25 |
Correct |
141 ms |
5816 KB |
Output is correct |
26 |
Correct |
145 ms |
7304 KB |
Output is correct |
27 |
Correct |
130 ms |
7160 KB |
Output is correct |
28 |
Correct |
139 ms |
7344 KB |
Output is correct |
29 |
Correct |
122 ms |
7388 KB |
Output is correct |
30 |
Correct |
119 ms |
7200 KB |
Output is correct |
31 |
Correct |
148 ms |
6620 KB |
Output is correct |
32 |
Correct |
130 ms |
6520 KB |
Output is correct |
33 |
Correct |
118 ms |
5208 KB |
Output is correct |
34 |
Correct |
126 ms |
1868 KB |
Output is correct |
35 |
Correct |
125 ms |
1868 KB |
Output is correct |
36 |
Correct |
118 ms |
1980 KB |
Output is correct |
37 |
Correct |
122 ms |
6108 KB |
Output is correct |
38 |
Correct |
132 ms |
7728 KB |
Output is correct |
39 |
Correct |
124 ms |
7336 KB |
Output is correct |
40 |
Correct |
31 ms |
1860 KB |
Output is correct |
41 |
Correct |
32 ms |
1852 KB |
Output is correct |
42 |
Correct |
34 ms |
1868 KB |
Output is correct |
43 |
Correct |
41 ms |
1872 KB |
Output is correct |
44 |
Correct |
34 ms |
1868 KB |
Output is correct |
45 |
Correct |
32 ms |
1868 KB |
Output is correct |
46 |
Correct |
126 ms |
5816 KB |
Output is correct |
47 |
Correct |
151 ms |
5972 KB |
Output is correct |
48 |
Correct |
118 ms |
7096 KB |
Output is correct |
49 |
Correct |
114 ms |
7288 KB |
Output is correct |
50 |
Correct |
145 ms |
7608 KB |
Output is correct |
51 |
Correct |
118 ms |
6212 KB |
Output is correct |
52 |
Correct |
118 ms |
7108 KB |
Output is correct |
53 |
Correct |
124 ms |
7136 KB |
Output is correct |
54 |
Correct |
149 ms |
7440 KB |
Output is correct |
55 |
Correct |
119 ms |
6756 KB |
Output is correct |
56 |
Correct |
135 ms |
7992 KB |
Output is correct |
57 |
Correct |
115 ms |
4560 KB |
Output is correct |
58 |
Correct |
124 ms |
8076 KB |
Output is correct |
59 |
Correct |
141 ms |
7912 KB |
Output is correct |
60 |
Correct |
124 ms |
8116 KB |
Output is correct |
61 |
Correct |
155 ms |
8116 KB |
Output is correct |
62 |
Correct |
135 ms |
7852 KB |
Output is correct |
63 |
Correct |
116 ms |
7844 KB |
Output is correct |
64 |
Correct |
124 ms |
2636 KB |
Output is correct |
65 |
Correct |
122 ms |
5644 KB |
Output is correct |