#include <bits/stdc++.h>
using namespace std;
const int N = 70 + 5, INF = 1e9 + 5;
int n, m, a, b, c, k, q, dist[N][N][N];
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
for(int k = 0; k < N; k++){
dist[i][j][k] = INF;
}
}
}
cin >> n >> m;
for(int i = 1; i <= m; i++){
cin >> a >> b >> c;
dist[1][a][b] = min(dist[1][a][b], c);
}
for(int i = 1; i <= n; i++) dist[1][i][i] = 0;
for(int i = 2; i <= n; i++){
for(int a = 1; a <= n; a++){
for(int b = 1; b <= n; b++){
dist[i][a][b] = dist[i - 1][a][b];
for(int v = 1; v <= n; v++){
dist[i][a][b] = min(dist[i][a][b], dist[i - 1][a][v] + dist[1][v][b]);
}
}
}
}
cin >> k >> q;
while(q--){
cin >> a >> b;
cout << (dist[min(n, k)][a][b] == INF ? -1 : dist[min(n, k)][a][b]) << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2016 KB |
Output is correct |
2 |
Correct |
1 ms |
1976 KB |
Output is correct |
3 |
Correct |
2 ms |
1976 KB |
Output is correct |
4 |
Correct |
2 ms |
1972 KB |
Output is correct |
5 |
Correct |
2 ms |
1976 KB |
Output is correct |
6 |
Correct |
2 ms |
1876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
2120 KB |
Output is correct |
2 |
Correct |
34 ms |
2004 KB |
Output is correct |
3 |
Correct |
37 ms |
2004 KB |
Output is correct |
4 |
Correct |
34 ms |
1984 KB |
Output is correct |
5 |
Correct |
38 ms |
2008 KB |
Output is correct |
6 |
Correct |
39 ms |
2028 KB |
Output is correct |
7 |
Correct |
32 ms |
2004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2016 KB |
Output is correct |
2 |
Correct |
1 ms |
1976 KB |
Output is correct |
3 |
Correct |
2 ms |
1976 KB |
Output is correct |
4 |
Correct |
2 ms |
1972 KB |
Output is correct |
5 |
Correct |
2 ms |
1976 KB |
Output is correct |
6 |
Correct |
2 ms |
1876 KB |
Output is correct |
7 |
Correct |
38 ms |
2124 KB |
Output is correct |
8 |
Correct |
34 ms |
2004 KB |
Output is correct |
9 |
Correct |
34 ms |
2008 KB |
Output is correct |
10 |
Correct |
30 ms |
2008 KB |
Output is correct |
11 |
Correct |
35 ms |
2004 KB |
Output is correct |
12 |
Correct |
37 ms |
2036 KB |
Output is correct |
13 |
Correct |
143 ms |
8104 KB |
Output is correct |
14 |
Correct |
140 ms |
8084 KB |
Output is correct |
15 |
Correct |
139 ms |
8108 KB |
Output is correct |
16 |
Correct |
138 ms |
8212 KB |
Output is correct |
17 |
Correct |
136 ms |
8140 KB |
Output is correct |
18 |
Correct |
141 ms |
8180 KB |
Output is correct |
19 |
Correct |
155 ms |
8184 KB |
Output is correct |
20 |
Correct |
158 ms |
8176 KB |
Output is correct |
21 |
Correct |
144 ms |
8220 KB |
Output is correct |
22 |
Correct |
150 ms |
8220 KB |
Output is correct |
23 |
Correct |
143 ms |
8212 KB |
Output is correct |
24 |
Correct |
156 ms |
8204 KB |
Output is correct |
25 |
Correct |
138 ms |
8300 KB |
Output is correct |
26 |
Correct |
136 ms |
8272 KB |
Output is correct |
27 |
Correct |
148 ms |
8308 KB |
Output is correct |
28 |
Correct |
133 ms |
8304 KB |
Output is correct |
29 |
Correct |
133 ms |
7968 KB |
Output is correct |
30 |
Correct |
135 ms |
7960 KB |
Output is correct |
31 |
Correct |
144 ms |
8052 KB |
Output is correct |
32 |
Correct |
161 ms |
7960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2016 KB |
Output is correct |
2 |
Correct |
1 ms |
1976 KB |
Output is correct |
3 |
Correct |
2 ms |
1976 KB |
Output is correct |
4 |
Correct |
2 ms |
1972 KB |
Output is correct |
5 |
Correct |
2 ms |
1976 KB |
Output is correct |
6 |
Correct |
2 ms |
1876 KB |
Output is correct |
7 |
Correct |
37 ms |
2120 KB |
Output is correct |
8 |
Correct |
34 ms |
2004 KB |
Output is correct |
9 |
Correct |
37 ms |
2004 KB |
Output is correct |
10 |
Correct |
34 ms |
1984 KB |
Output is correct |
11 |
Correct |
38 ms |
2008 KB |
Output is correct |
12 |
Correct |
39 ms |
2028 KB |
Output is correct |
13 |
Correct |
32 ms |
2004 KB |
Output is correct |
14 |
Correct |
38 ms |
2124 KB |
Output is correct |
15 |
Correct |
34 ms |
2004 KB |
Output is correct |
16 |
Correct |
34 ms |
2008 KB |
Output is correct |
17 |
Correct |
30 ms |
2008 KB |
Output is correct |
18 |
Correct |
35 ms |
2004 KB |
Output is correct |
19 |
Correct |
37 ms |
2036 KB |
Output is correct |
20 |
Correct |
143 ms |
8104 KB |
Output is correct |
21 |
Correct |
140 ms |
8084 KB |
Output is correct |
22 |
Correct |
139 ms |
8108 KB |
Output is correct |
23 |
Correct |
138 ms |
8212 KB |
Output is correct |
24 |
Correct |
136 ms |
8140 KB |
Output is correct |
25 |
Correct |
141 ms |
8180 KB |
Output is correct |
26 |
Correct |
155 ms |
8184 KB |
Output is correct |
27 |
Correct |
158 ms |
8176 KB |
Output is correct |
28 |
Correct |
144 ms |
8220 KB |
Output is correct |
29 |
Correct |
150 ms |
8220 KB |
Output is correct |
30 |
Correct |
143 ms |
8212 KB |
Output is correct |
31 |
Correct |
156 ms |
8204 KB |
Output is correct |
32 |
Correct |
138 ms |
8300 KB |
Output is correct |
33 |
Correct |
136 ms |
8272 KB |
Output is correct |
34 |
Correct |
148 ms |
8308 KB |
Output is correct |
35 |
Correct |
133 ms |
8304 KB |
Output is correct |
36 |
Correct |
133 ms |
7968 KB |
Output is correct |
37 |
Correct |
135 ms |
7960 KB |
Output is correct |
38 |
Correct |
144 ms |
8052 KB |
Output is correct |
39 |
Correct |
161 ms |
7960 KB |
Output is correct |
40 |
Correct |
34 ms |
1988 KB |
Output is correct |
41 |
Correct |
30 ms |
2004 KB |
Output is correct |
42 |
Correct |
40 ms |
2004 KB |
Output is correct |
43 |
Correct |
32 ms |
2008 KB |
Output is correct |
44 |
Correct |
35 ms |
2028 KB |
Output is correct |
45 |
Correct |
33 ms |
2048 KB |
Output is correct |
46 |
Correct |
149 ms |
8084 KB |
Output is correct |
47 |
Correct |
130 ms |
8104 KB |
Output is correct |
48 |
Correct |
137 ms |
8100 KB |
Output is correct |
49 |
Correct |
149 ms |
8092 KB |
Output is correct |
50 |
Correct |
149 ms |
8184 KB |
Output is correct |
51 |
Correct |
133 ms |
8096 KB |
Output is correct |
52 |
Correct |
134 ms |
8088 KB |
Output is correct |
53 |
Correct |
138 ms |
8180 KB |
Output is correct |
54 |
Correct |
125 ms |
8212 KB |
Output is correct |
55 |
Correct |
138 ms |
8204 KB |
Output is correct |
56 |
Correct |
153 ms |
8220 KB |
Output is correct |
57 |
Correct |
134 ms |
8208 KB |
Output is correct |
58 |
Correct |
142 ms |
8216 KB |
Output is correct |
59 |
Correct |
143 ms |
8220 KB |
Output is correct |
60 |
Correct |
133 ms |
8304 KB |
Output is correct |
61 |
Correct |
142 ms |
8228 KB |
Output is correct |
62 |
Correct |
151 ms |
7956 KB |
Output is correct |
63 |
Correct |
133 ms |
7964 KB |
Output is correct |
64 |
Correct |
147 ms |
8040 KB |
Output is correct |
65 |
Correct |
170 ms |
7956 KB |
Output is correct |