#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];
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) 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; 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 |
4 ms |
3028 KB |
Output is correct |
2 |
Correct |
4 ms |
3020 KB |
Output is correct |
3 |
Correct |
4 ms |
3020 KB |
Output is correct |
4 |
Correct |
4 ms |
3020 KB |
Output is correct |
5 |
Correct |
6 ms |
3044 KB |
Output is correct |
6 |
Correct |
7 ms |
3036 KB |
Output is correct |
7 |
Correct |
10 ms |
3148 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 |
41 ms |
3104 KB |
Output is correct |
8 |
Correct |
122 ms |
3212 KB |
Output is correct |
9 |
Correct |
9 ms |
3020 KB |
Output is correct |
10 |
Correct |
103 ms |
3092 KB |
Output is correct |
11 |
Correct |
38 ms |
3108 KB |
Output is correct |
12 |
Correct |
174 ms |
3304 KB |
Output is correct |
13 |
Correct |
92 ms |
3020 KB |
Output is correct |
14 |
Correct |
99 ms |
3028 KB |
Output is correct |
15 |
Correct |
283 ms |
3328 KB |
Output is correct |
16 |
Correct |
145 ms |
3180 KB |
Output is correct |
17 |
Correct |
91 ms |
3024 KB |
Output is correct |
18 |
Correct |
110 ms |
3020 KB |
Output is correct |
19 |
Correct |
251 ms |
3140 KB |
Output is correct |
20 |
Correct |
170 ms |
3028 KB |
Output is correct |
21 |
Correct |
88 ms |
3020 KB |
Output is correct |
22 |
Correct |
134 ms |
3020 KB |
Output is correct |
23 |
Correct |
133 ms |
3020 KB |
Output is correct |
24 |
Correct |
179 ms |
3020 KB |
Output is correct |
25 |
Correct |
104 ms |
3020 KB |
Output is correct |
26 |
Correct |
135 ms |
3020 KB |
Output is correct |
27 |
Correct |
164 ms |
3020 KB |
Output is correct |
28 |
Correct |
141 ms |
3108 KB |
Output is correct |
29 |
Correct |
113 ms |
3136 KB |
Output is correct |
30 |
Correct |
133 ms |
3184 KB |
Output is correct |
31 |
Correct |
216 ms |
3468 KB |
Output is correct |
32 |
Correct |
371 ms |
3520 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 |
4 ms |
3028 KB |
Output is correct |
8 |
Correct |
4 ms |
3020 KB |
Output is correct |
9 |
Correct |
4 ms |
3020 KB |
Output is correct |
10 |
Correct |
4 ms |
3020 KB |
Output is correct |
11 |
Correct |
6 ms |
3044 KB |
Output is correct |
12 |
Correct |
7 ms |
3036 KB |
Output is correct |
13 |
Correct |
10 ms |
3148 KB |
Output is correct |
14 |
Correct |
41 ms |
3104 KB |
Output is correct |
15 |
Correct |
122 ms |
3212 KB |
Output is correct |
16 |
Correct |
9 ms |
3020 KB |
Output is correct |
17 |
Correct |
103 ms |
3092 KB |
Output is correct |
18 |
Correct |
38 ms |
3108 KB |
Output is correct |
19 |
Correct |
174 ms |
3304 KB |
Output is correct |
20 |
Correct |
92 ms |
3020 KB |
Output is correct |
21 |
Correct |
99 ms |
3028 KB |
Output is correct |
22 |
Correct |
283 ms |
3328 KB |
Output is correct |
23 |
Correct |
145 ms |
3180 KB |
Output is correct |
24 |
Correct |
91 ms |
3024 KB |
Output is correct |
25 |
Correct |
110 ms |
3020 KB |
Output is correct |
26 |
Correct |
251 ms |
3140 KB |
Output is correct |
27 |
Correct |
170 ms |
3028 KB |
Output is correct |
28 |
Correct |
88 ms |
3020 KB |
Output is correct |
29 |
Correct |
134 ms |
3020 KB |
Output is correct |
30 |
Correct |
133 ms |
3020 KB |
Output is correct |
31 |
Correct |
179 ms |
3020 KB |
Output is correct |
32 |
Correct |
104 ms |
3020 KB |
Output is correct |
33 |
Correct |
135 ms |
3020 KB |
Output is correct |
34 |
Correct |
164 ms |
3020 KB |
Output is correct |
35 |
Correct |
141 ms |
3108 KB |
Output is correct |
36 |
Correct |
113 ms |
3136 KB |
Output is correct |
37 |
Correct |
133 ms |
3184 KB |
Output is correct |
38 |
Correct |
216 ms |
3468 KB |
Output is correct |
39 |
Correct |
371 ms |
3520 KB |
Output is correct |
40 |
Correct |
29 ms |
3076 KB |
Output is correct |
41 |
Correct |
113 ms |
3112 KB |
Output is correct |
42 |
Correct |
19 ms |
3076 KB |
Output is correct |
43 |
Correct |
205 ms |
3104 KB |
Output is correct |
44 |
Correct |
38 ms |
3020 KB |
Output is correct |
45 |
Correct |
296 ms |
3468 KB |
Output is correct |
46 |
Correct |
94 ms |
3020 KB |
Output is correct |
47 |
Correct |
202 ms |
3272 KB |
Output is correct |
48 |
Correct |
130 ms |
3268 KB |
Output is correct |
49 |
Correct |
338 ms |
3468 KB |
Output is correct |
50 |
Correct |
95 ms |
3024 KB |
Output is correct |
51 |
Correct |
129 ms |
3020 KB |
Output is correct |
52 |
Correct |
93 ms |
3020 KB |
Output is correct |
53 |
Correct |
268 ms |
3192 KB |
Output is correct |
54 |
Correct |
88 ms |
3020 KB |
Output is correct |
55 |
Correct |
103 ms |
3020 KB |
Output is correct |
56 |
Correct |
259 ms |
3140 KB |
Output is correct |
57 |
Correct |
271 ms |
3028 KB |
Output is correct |
58 |
Correct |
94 ms |
3020 KB |
Output is correct |
59 |
Correct |
199 ms |
3268 KB |
Output is correct |
60 |
Correct |
272 ms |
3140 KB |
Output is correct |
61 |
Correct |
341 ms |
3172 KB |
Output is correct |
62 |
Correct |
89 ms |
3020 KB |
Output is correct |
63 |
Correct |
133 ms |
3196 KB |
Output is correct |
64 |
Correct |
160 ms |
3264 KB |
Output is correct |
65 |
Correct |
333 ms |
3524 KB |
Output is correct |