#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair<int, int>
#define iii pair<int, ii>
#define ff first
#define ss second
const int INF = 1e18;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int v, e; cin >> v >> e;
int mat[v][v];
for(int i = 0; i < v; i ++)
for(int k = 0; k < v; k ++)
mat[i][k] = INF;
for(int i = 0; i < v; i ++)
mat[i][i] = 0;
for(int i = 0; i < e; i ++){
int a, b, w; cin >> a >> b >> w;
mat[a-1][b-1] = min(mat[a-1][b-1], w);
}
vector<ii> adj[v];
for(int i = 0; i < v; i ++)
for(int j = 0; j < v; j ++)
if(mat[i][j] != INF)
adj[i].emplace_back(j, mat[i][j]);
int dist[v][v][v+1];
memset(dist, -1, sizeof dist);
for(int i = 0; i < v; i ++){
priority_queue<iii, vector<iii>, greater<iii>> pq;
pq.push({0, {i, 0}}); dist[i][i][0] = 0;
while(!pq.empty()){
auto tp = pq.top(); pq.pop();
if(dist[i][tp.ss.ff][tp.ss.ss] != tp.ff || tp.ss.ss == v)
continue;
for(auto& [nxt, wt] : adj[tp.ss.ff]){
if(dist[i][nxt][tp.ss.ss+1] == -1 || dist[i][nxt][tp.ss.ss+1] > wt + tp.ff){
dist[i][nxt][tp.ss.ss+1] = wt + tp.ff;
pq.push({dist[i][nxt][tp.ss.ss+1], {nxt, tp.ss.ss+1}});
}
}
}
for(int j = 0; j < v; j ++)
for(int k = 1; k <= v; k ++)
if(dist[i][j][k-1] != -1)
dist[i][j][k] = min(dist[i][j][k-1], dist[i][j][k]);
}
int K, q; cin >> K >> q;
K = min(K, v);
while(q --){
int ci, di; cin >> ci >> di;
cout << dist[ci-1][di-1][K] << '\n';
}
}
/*
4 7
1 2 1
1 4 10
2 3 1
2 4 5
3 2 2
3 4 1
4 3 2
1 3
1 4
4 2
3 3
*/
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:39:23: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
39 | for(auto& [nxt, wt] : adj[tp.ss.ff]){
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
86 ms |
3400 KB |
Output is correct |
2 |
Correct |
81 ms |
3404 KB |
Output is correct |
3 |
Correct |
82 ms |
3412 KB |
Output is correct |
4 |
Correct |
111 ms |
3456 KB |
Output is correct |
5 |
Correct |
139 ms |
3456 KB |
Output is correct |
6 |
Correct |
203 ms |
3880 KB |
Output is correct |
7 |
Correct |
202 ms |
3876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 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 |
83 ms |
3392 KB |
Output is correct |
8 |
Correct |
85 ms |
3404 KB |
Output is correct |
9 |
Correct |
110 ms |
3472 KB |
Output is correct |
10 |
Correct |
110 ms |
3476 KB |
Output is correct |
11 |
Correct |
200 ms |
3892 KB |
Output is correct |
12 |
Correct |
244 ms |
3860 KB |
Output is correct |
13 |
Correct |
300 ms |
3936 KB |
Output is correct |
14 |
Correct |
322 ms |
3960 KB |
Output is correct |
15 |
Correct |
298 ms |
3932 KB |
Output is correct |
16 |
Correct |
384 ms |
3932 KB |
Output is correct |
17 |
Correct |
269 ms |
4000 KB |
Output is correct |
18 |
Correct |
269 ms |
3848 KB |
Output is correct |
19 |
Correct |
260 ms |
3924 KB |
Output is correct |
20 |
Correct |
273 ms |
3888 KB |
Output is correct |
21 |
Correct |
234 ms |
3840 KB |
Output is correct |
22 |
Correct |
239 ms |
3832 KB |
Output is correct |
23 |
Correct |
251 ms |
3844 KB |
Output is correct |
24 |
Correct |
232 ms |
3844 KB |
Output is correct |
25 |
Correct |
331 ms |
4120 KB |
Output is correct |
26 |
Correct |
277 ms |
3900 KB |
Output is correct |
27 |
Correct |
279 ms |
3916 KB |
Output is correct |
28 |
Correct |
291 ms |
3900 KB |
Output is correct |
29 |
Correct |
313 ms |
4328 KB |
Output is correct |
30 |
Correct |
339 ms |
4556 KB |
Output is correct |
31 |
Correct |
314 ms |
4364 KB |
Output is correct |
32 |
Correct |
320 ms |
4476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
1 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 |
86 ms |
3400 KB |
Output is correct |
8 |
Correct |
81 ms |
3404 KB |
Output is correct |
9 |
Correct |
82 ms |
3412 KB |
Output is correct |
10 |
Correct |
111 ms |
3456 KB |
Output is correct |
11 |
Correct |
139 ms |
3456 KB |
Output is correct |
12 |
Correct |
203 ms |
3880 KB |
Output is correct |
13 |
Correct |
202 ms |
3876 KB |
Output is correct |
14 |
Correct |
83 ms |
3392 KB |
Output is correct |
15 |
Correct |
85 ms |
3404 KB |
Output is correct |
16 |
Correct |
110 ms |
3472 KB |
Output is correct |
17 |
Correct |
110 ms |
3476 KB |
Output is correct |
18 |
Correct |
200 ms |
3892 KB |
Output is correct |
19 |
Correct |
244 ms |
3860 KB |
Output is correct |
20 |
Correct |
300 ms |
3936 KB |
Output is correct |
21 |
Correct |
322 ms |
3960 KB |
Output is correct |
22 |
Correct |
298 ms |
3932 KB |
Output is correct |
23 |
Correct |
384 ms |
3932 KB |
Output is correct |
24 |
Correct |
269 ms |
4000 KB |
Output is correct |
25 |
Correct |
269 ms |
3848 KB |
Output is correct |
26 |
Correct |
260 ms |
3924 KB |
Output is correct |
27 |
Correct |
273 ms |
3888 KB |
Output is correct |
28 |
Correct |
234 ms |
3840 KB |
Output is correct |
29 |
Correct |
239 ms |
3832 KB |
Output is correct |
30 |
Correct |
251 ms |
3844 KB |
Output is correct |
31 |
Correct |
232 ms |
3844 KB |
Output is correct |
32 |
Correct |
331 ms |
4120 KB |
Output is correct |
33 |
Correct |
277 ms |
3900 KB |
Output is correct |
34 |
Correct |
279 ms |
3916 KB |
Output is correct |
35 |
Correct |
291 ms |
3900 KB |
Output is correct |
36 |
Correct |
313 ms |
4328 KB |
Output is correct |
37 |
Correct |
339 ms |
4556 KB |
Output is correct |
38 |
Correct |
314 ms |
4364 KB |
Output is correct |
39 |
Correct |
320 ms |
4476 KB |
Output is correct |
40 |
Correct |
78 ms |
3368 KB |
Output is correct |
41 |
Correct |
82 ms |
3372 KB |
Output is correct |
42 |
Correct |
113 ms |
3448 KB |
Output is correct |
43 |
Correct |
118 ms |
3464 KB |
Output is correct |
44 |
Correct |
202 ms |
3848 KB |
Output is correct |
45 |
Correct |
207 ms |
3900 KB |
Output is correct |
46 |
Correct |
317 ms |
3960 KB |
Output is correct |
47 |
Correct |
305 ms |
3928 KB |
Output is correct |
48 |
Correct |
309 ms |
4000 KB |
Output is correct |
49 |
Correct |
342 ms |
4048 KB |
Output is correct |
50 |
Correct |
255 ms |
3856 KB |
Output is correct |
51 |
Correct |
297 ms |
3868 KB |
Output is correct |
52 |
Correct |
288 ms |
3944 KB |
Output is correct |
53 |
Correct |
267 ms |
3892 KB |
Output is correct |
54 |
Correct |
259 ms |
3864 KB |
Output is correct |
55 |
Correct |
252 ms |
3864 KB |
Output is correct |
56 |
Correct |
289 ms |
3888 KB |
Output is correct |
57 |
Correct |
277 ms |
3836 KB |
Output is correct |
58 |
Correct |
315 ms |
3888 KB |
Output is correct |
59 |
Correct |
310 ms |
3928 KB |
Output is correct |
60 |
Correct |
304 ms |
3996 KB |
Output is correct |
61 |
Correct |
313 ms |
3900 KB |
Output is correct |
62 |
Correct |
406 ms |
4684 KB |
Output is correct |
63 |
Correct |
348 ms |
4372 KB |
Output is correct |
64 |
Correct |
402 ms |
4364 KB |
Output is correct |
65 |
Correct |
341 ms |
4356 KB |
Output is correct |