#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;
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) 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 |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
113 ms |
188276 KB |
Output is correct |
2 |
Correct |
114 ms |
188232 KB |
Output is correct |
3 |
Correct |
116 ms |
188356 KB |
Output is correct |
4 |
Correct |
115 ms |
188176 KB |
Output is correct |
5 |
Correct |
121 ms |
188272 KB |
Output is correct |
6 |
Correct |
121 ms |
188288 KB |
Output is correct |
7 |
Correct |
121 ms |
188224 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 |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
155 ms |
188336 KB |
Output is correct |
8 |
Correct |
254 ms |
188432 KB |
Output is correct |
9 |
Correct |
120 ms |
188412 KB |
Output is correct |
10 |
Correct |
217 ms |
188404 KB |
Output is correct |
11 |
Correct |
150 ms |
188304 KB |
Output is correct |
12 |
Correct |
282 ms |
188548 KB |
Output is correct |
13 |
Correct |
213 ms |
188264 KB |
Output is correct |
14 |
Correct |
213 ms |
188256 KB |
Output is correct |
15 |
Correct |
402 ms |
188584 KB |
Output is correct |
16 |
Correct |
267 ms |
188432 KB |
Output is correct |
17 |
Correct |
266 ms |
188320 KB |
Output is correct |
18 |
Correct |
201 ms |
188228 KB |
Output is correct |
19 |
Correct |
392 ms |
188388 KB |
Output is correct |
20 |
Correct |
292 ms |
188368 KB |
Output is correct |
21 |
Correct |
218 ms |
188296 KB |
Output is correct |
22 |
Correct |
229 ms |
188264 KB |
Output is correct |
23 |
Correct |
242 ms |
188332 KB |
Output is correct |
24 |
Correct |
339 ms |
188356 KB |
Output is correct |
25 |
Correct |
206 ms |
188264 KB |
Output is correct |
26 |
Correct |
268 ms |
188268 KB |
Output is correct |
27 |
Correct |
276 ms |
188380 KB |
Output is correct |
28 |
Correct |
335 ms |
188272 KB |
Output is correct |
29 |
Correct |
218 ms |
188392 KB |
Output is correct |
30 |
Correct |
238 ms |
188404 KB |
Output is correct |
31 |
Correct |
346 ms |
188736 KB |
Output is correct |
32 |
Correct |
463 ms |
188888 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 |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
113 ms |
188276 KB |
Output is correct |
8 |
Correct |
114 ms |
188232 KB |
Output is correct |
9 |
Correct |
116 ms |
188356 KB |
Output is correct |
10 |
Correct |
115 ms |
188176 KB |
Output is correct |
11 |
Correct |
121 ms |
188272 KB |
Output is correct |
12 |
Correct |
121 ms |
188288 KB |
Output is correct |
13 |
Correct |
121 ms |
188224 KB |
Output is correct |
14 |
Correct |
155 ms |
188336 KB |
Output is correct |
15 |
Correct |
254 ms |
188432 KB |
Output is correct |
16 |
Correct |
120 ms |
188412 KB |
Output is correct |
17 |
Correct |
217 ms |
188404 KB |
Output is correct |
18 |
Correct |
150 ms |
188304 KB |
Output is correct |
19 |
Correct |
282 ms |
188548 KB |
Output is correct |
20 |
Correct |
213 ms |
188264 KB |
Output is correct |
21 |
Correct |
213 ms |
188256 KB |
Output is correct |
22 |
Correct |
402 ms |
188584 KB |
Output is correct |
23 |
Correct |
267 ms |
188432 KB |
Output is correct |
24 |
Correct |
266 ms |
188320 KB |
Output is correct |
25 |
Correct |
201 ms |
188228 KB |
Output is correct |
26 |
Correct |
392 ms |
188388 KB |
Output is correct |
27 |
Correct |
292 ms |
188368 KB |
Output is correct |
28 |
Correct |
218 ms |
188296 KB |
Output is correct |
29 |
Correct |
229 ms |
188264 KB |
Output is correct |
30 |
Correct |
242 ms |
188332 KB |
Output is correct |
31 |
Correct |
339 ms |
188356 KB |
Output is correct |
32 |
Correct |
206 ms |
188264 KB |
Output is correct |
33 |
Correct |
268 ms |
188268 KB |
Output is correct |
34 |
Correct |
276 ms |
188380 KB |
Output is correct |
35 |
Correct |
335 ms |
188272 KB |
Output is correct |
36 |
Correct |
218 ms |
188392 KB |
Output is correct |
37 |
Correct |
238 ms |
188404 KB |
Output is correct |
38 |
Correct |
346 ms |
188736 KB |
Output is correct |
39 |
Correct |
463 ms |
188888 KB |
Output is correct |
40 |
Correct |
149 ms |
188356 KB |
Output is correct |
41 |
Execution timed out |
1102 ms |
188268 KB |
Time limit exceeded |
42 |
Halted |
0 ms |
0 KB |
- |