#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 || 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 |
0 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 |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
117 ms |
188252 KB |
Output is correct |
2 |
Correct |
110 ms |
188192 KB |
Output is correct |
3 |
Correct |
112 ms |
188276 KB |
Output is correct |
4 |
Correct |
115 ms |
188224 KB |
Output is correct |
5 |
Correct |
112 ms |
188244 KB |
Output is correct |
6 |
Correct |
110 ms |
188228 KB |
Output is correct |
7 |
Correct |
113 ms |
188244 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 |
0 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 |
145 ms |
188272 KB |
Output is correct |
8 |
Correct |
219 ms |
188492 KB |
Output is correct |
9 |
Correct |
114 ms |
188264 KB |
Output is correct |
10 |
Correct |
204 ms |
188312 KB |
Output is correct |
11 |
Correct |
144 ms |
188364 KB |
Output is correct |
12 |
Correct |
273 ms |
188564 KB |
Output is correct |
13 |
Correct |
200 ms |
188228 KB |
Output is correct |
14 |
Correct |
207 ms |
188276 KB |
Output is correct |
15 |
Correct |
387 ms |
188592 KB |
Output is correct |
16 |
Correct |
254 ms |
188360 KB |
Output is correct |
17 |
Correct |
199 ms |
188364 KB |
Output is correct |
18 |
Correct |
194 ms |
188220 KB |
Output is correct |
19 |
Correct |
368 ms |
188344 KB |
Output is correct |
20 |
Correct |
279 ms |
188364 KB |
Output is correct |
21 |
Correct |
196 ms |
188320 KB |
Output is correct |
22 |
Correct |
226 ms |
188344 KB |
Output is correct |
23 |
Correct |
221 ms |
188324 KB |
Output is correct |
24 |
Correct |
282 ms |
188332 KB |
Output is correct |
25 |
Correct |
205 ms |
188404 KB |
Output is correct |
26 |
Correct |
235 ms |
188360 KB |
Output is correct |
27 |
Correct |
262 ms |
188272 KB |
Output is correct |
28 |
Correct |
252 ms |
188328 KB |
Output is correct |
29 |
Correct |
201 ms |
188320 KB |
Output is correct |
30 |
Correct |
230 ms |
188404 KB |
Output is correct |
31 |
Correct |
321 ms |
188676 KB |
Output is correct |
32 |
Correct |
437 ms |
188788 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 |
0 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 |
117 ms |
188252 KB |
Output is correct |
8 |
Correct |
110 ms |
188192 KB |
Output is correct |
9 |
Correct |
112 ms |
188276 KB |
Output is correct |
10 |
Correct |
115 ms |
188224 KB |
Output is correct |
11 |
Correct |
112 ms |
188244 KB |
Output is correct |
12 |
Correct |
110 ms |
188228 KB |
Output is correct |
13 |
Correct |
113 ms |
188244 KB |
Output is correct |
14 |
Correct |
145 ms |
188272 KB |
Output is correct |
15 |
Correct |
219 ms |
188492 KB |
Output is correct |
16 |
Correct |
114 ms |
188264 KB |
Output is correct |
17 |
Correct |
204 ms |
188312 KB |
Output is correct |
18 |
Correct |
144 ms |
188364 KB |
Output is correct |
19 |
Correct |
273 ms |
188564 KB |
Output is correct |
20 |
Correct |
200 ms |
188228 KB |
Output is correct |
21 |
Correct |
207 ms |
188276 KB |
Output is correct |
22 |
Correct |
387 ms |
188592 KB |
Output is correct |
23 |
Correct |
254 ms |
188360 KB |
Output is correct |
24 |
Correct |
199 ms |
188364 KB |
Output is correct |
25 |
Correct |
194 ms |
188220 KB |
Output is correct |
26 |
Correct |
368 ms |
188344 KB |
Output is correct |
27 |
Correct |
279 ms |
188364 KB |
Output is correct |
28 |
Correct |
196 ms |
188320 KB |
Output is correct |
29 |
Correct |
226 ms |
188344 KB |
Output is correct |
30 |
Correct |
221 ms |
188324 KB |
Output is correct |
31 |
Correct |
282 ms |
188332 KB |
Output is correct |
32 |
Correct |
205 ms |
188404 KB |
Output is correct |
33 |
Correct |
235 ms |
188360 KB |
Output is correct |
34 |
Correct |
262 ms |
188272 KB |
Output is correct |
35 |
Correct |
252 ms |
188328 KB |
Output is correct |
36 |
Correct |
201 ms |
188320 KB |
Output is correct |
37 |
Correct |
230 ms |
188404 KB |
Output is correct |
38 |
Correct |
321 ms |
188676 KB |
Output is correct |
39 |
Correct |
437 ms |
188788 KB |
Output is correct |
40 |
Correct |
132 ms |
188316 KB |
Output is correct |
41 |
Execution timed out |
1086 ms |
188344 KB |
Time limit exceeded |
42 |
Halted |
0 ms |
0 KB |
- |