#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, e, x, y, w, k, q;
cin >> n >> e;
int adj[n][n], res;
int dist[n][n][n*n];
memset(adj, -1, sizeof(adj));
memset(dist, -1, sizeof(dist));
for(int i = 0; i < e; i++) {
cin >> x >> y >> w;
x--; y--;
if(adj[x][y] == -1) adj[x][y] = w;
else adj[x][y] = min(adj[x][y], w);
}
cin >> k >> q;
k = min(k, n-1);
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> pq;
for(int i = 0; i < n; i++) {
dist[i][i][0] = 0;
pq.push(make_pair(0, make_pair(i, 0)));
while(!pq.empty()) {
pair<int, pair<int, int>> c = pq.top();
pq.pop();
if(c.first != dist[i][c.second.first][c.second.second]) continue;
if(c.second.second == k || c.second.second == n*n) continue;
for(int j = 0; j < n; j++) {
if(adj[c.second.first][j] == -1) continue;
if(dist[i][j][c.second.second+1] == -1 || dist[i][j][c.second.second+1] > c.first + adj[c.second.first][j]) {
dist[i][j][c.second.second+1] = c.first + adj[c.second.first][j];
pq.push(make_pair(dist[i][j][c.second.second+1], make_pair(j, c.second.second+1)));
}
}
}
}
for(int i = 0; i < q; i++) {
cin >> x >> y;
x--; y--;
res = 1e18;
for(int j = 0; j < n*n; j++) if(dist[x][y][j] != -1) res = min(res, dist[x][y][j]);
if(res == 1e18) cout << -1 << '\n';
else cout << res << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
124 ms |
188264 KB |
Output is correct |
2 |
Correct |
121 ms |
188296 KB |
Output is correct |
3 |
Correct |
119 ms |
188236 KB |
Output is correct |
4 |
Correct |
121 ms |
188168 KB |
Output is correct |
5 |
Correct |
122 ms |
188184 KB |
Output is correct |
6 |
Correct |
127 ms |
188412 KB |
Output is correct |
7 |
Correct |
125 ms |
188280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
163 ms |
188360 KB |
Output is correct |
8 |
Correct |
253 ms |
188500 KB |
Output is correct |
9 |
Correct |
123 ms |
188292 KB |
Output is correct |
10 |
Correct |
233 ms |
188304 KB |
Output is correct |
11 |
Correct |
181 ms |
188292 KB |
Output is correct |
12 |
Correct |
288 ms |
188560 KB |
Output is correct |
13 |
Correct |
211 ms |
188228 KB |
Output is correct |
14 |
Correct |
231 ms |
188272 KB |
Output is correct |
15 |
Correct |
403 ms |
188548 KB |
Output is correct |
16 |
Correct |
282 ms |
188360 KB |
Output is correct |
17 |
Correct |
219 ms |
188292 KB |
Output is correct |
18 |
Correct |
205 ms |
188196 KB |
Output is correct |
19 |
Correct |
399 ms |
188552 KB |
Output is correct |
20 |
Correct |
329 ms |
188356 KB |
Output is correct |
21 |
Correct |
207 ms |
188268 KB |
Output is correct |
22 |
Correct |
243 ms |
188348 KB |
Output is correct |
23 |
Correct |
229 ms |
188336 KB |
Output is correct |
24 |
Correct |
332 ms |
188268 KB |
Output is correct |
25 |
Correct |
220 ms |
188268 KB |
Output is correct |
26 |
Correct |
266 ms |
188272 KB |
Output is correct |
27 |
Correct |
292 ms |
188276 KB |
Output is correct |
28 |
Correct |
254 ms |
188276 KB |
Output is correct |
29 |
Correct |
216 ms |
188280 KB |
Output is correct |
30 |
Correct |
254 ms |
188528 KB |
Output is correct |
31 |
Correct |
335 ms |
188720 KB |
Output is correct |
32 |
Correct |
503 ms |
188860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
124 ms |
188264 KB |
Output is correct |
8 |
Correct |
121 ms |
188296 KB |
Output is correct |
9 |
Correct |
119 ms |
188236 KB |
Output is correct |
10 |
Correct |
121 ms |
188168 KB |
Output is correct |
11 |
Correct |
122 ms |
188184 KB |
Output is correct |
12 |
Correct |
127 ms |
188412 KB |
Output is correct |
13 |
Correct |
125 ms |
188280 KB |
Output is correct |
14 |
Correct |
163 ms |
188360 KB |
Output is correct |
15 |
Correct |
253 ms |
188500 KB |
Output is correct |
16 |
Correct |
123 ms |
188292 KB |
Output is correct |
17 |
Correct |
233 ms |
188304 KB |
Output is correct |
18 |
Correct |
181 ms |
188292 KB |
Output is correct |
19 |
Correct |
288 ms |
188560 KB |
Output is correct |
20 |
Correct |
211 ms |
188228 KB |
Output is correct |
21 |
Correct |
231 ms |
188272 KB |
Output is correct |
22 |
Correct |
403 ms |
188548 KB |
Output is correct |
23 |
Correct |
282 ms |
188360 KB |
Output is correct |
24 |
Correct |
219 ms |
188292 KB |
Output is correct |
25 |
Correct |
205 ms |
188196 KB |
Output is correct |
26 |
Correct |
399 ms |
188552 KB |
Output is correct |
27 |
Correct |
329 ms |
188356 KB |
Output is correct |
28 |
Correct |
207 ms |
188268 KB |
Output is correct |
29 |
Correct |
243 ms |
188348 KB |
Output is correct |
30 |
Correct |
229 ms |
188336 KB |
Output is correct |
31 |
Correct |
332 ms |
188268 KB |
Output is correct |
32 |
Correct |
220 ms |
188268 KB |
Output is correct |
33 |
Correct |
266 ms |
188272 KB |
Output is correct |
34 |
Correct |
292 ms |
188276 KB |
Output is correct |
35 |
Correct |
254 ms |
188276 KB |
Output is correct |
36 |
Correct |
216 ms |
188280 KB |
Output is correct |
37 |
Correct |
254 ms |
188528 KB |
Output is correct |
38 |
Correct |
335 ms |
188720 KB |
Output is correct |
39 |
Correct |
503 ms |
188860 KB |
Output is correct |
40 |
Correct |
143 ms |
188356 KB |
Output is correct |
41 |
Correct |
229 ms |
188336 KB |
Output is correct |
42 |
Correct |
156 ms |
188448 KB |
Output is correct |
43 |
Correct |
313 ms |
188372 KB |
Output is correct |
44 |
Correct |
153 ms |
188340 KB |
Output is correct |
45 |
Correct |
398 ms |
188756 KB |
Output is correct |
46 |
Correct |
219 ms |
188252 KB |
Output is correct |
47 |
Correct |
318 ms |
188540 KB |
Output is correct |
48 |
Correct |
310 ms |
188440 KB |
Output is correct |
49 |
Correct |
448 ms |
188784 KB |
Output is correct |
50 |
Correct |
221 ms |
188268 KB |
Output is correct |
51 |
Correct |
250 ms |
188312 KB |
Output is correct |
52 |
Correct |
212 ms |
188296 KB |
Output is correct |
53 |
Correct |
387 ms |
188432 KB |
Output is correct |
54 |
Correct |
222 ms |
188312 KB |
Output is correct |
55 |
Correct |
223 ms |
188356 KB |
Output is correct |
56 |
Correct |
372 ms |
188268 KB |
Output is correct |
57 |
Correct |
393 ms |
188364 KB |
Output is correct |
58 |
Correct |
207 ms |
188260 KB |
Output is correct |
59 |
Correct |
315 ms |
188528 KB |
Output is correct |
60 |
Correct |
394 ms |
188408 KB |
Output is correct |
61 |
Correct |
500 ms |
188484 KB |
Output is correct |
62 |
Correct |
215 ms |
188212 KB |
Output is correct |
63 |
Correct |
270 ms |
188560 KB |
Output is correct |
64 |
Correct |
268 ms |
188600 KB |
Output is correct |
65 |
Correct |
463 ms |
188808 KB |
Output is correct |