#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define int ll
const ll mod = 1e9 + 7;
main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
int n , m , k , q;cin >> n >> m;
vector<vector<vector<int>>> dp(n + 1 , vector<vector<int>>(n + 1 , vector<int>(n + 1)));
for(int i = 1;i <= n;i ++){
for(int j = 1;j <= n;j ++){
for(int K = 1;K <= n;K ++){
dp[i][j][K] = 1e9;
if(i == j) dp[i][j][K] = 0;
}
}
}
for(int i = 1;i <= m;i ++){
int u , v , t;cin >> u >> v >> t;
dp[u][v][1] = min(dp[u][v][1] , t);
}
cin >> k >> q;
k = min(k , n);
for(int kk = 2;kk <= k;kk ++){
for(int j = 1;j <= n;j ++){
for(int b = 1;b <= n;b ++){
for(int a = 1;a <= n;a ++){
dp[j][a][kk] = min(dp[j][a][kk] , dp[j][b][kk - 1] + dp[b][a][1]);
}
}
}
}
while(q --){
int u , v;cin >> u >> v;
cout << (dp[u][v][k] >= 1e9 ? -1 : dp[u][v][k]) << endl;
}
}
Compilation message
Main.cpp:15:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
15 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
3284 KB |
Output is correct |
2 |
Correct |
11 ms |
3344 KB |
Output is correct |
3 |
Correct |
13 ms |
3272 KB |
Output is correct |
4 |
Correct |
11 ms |
3284 KB |
Output is correct |
5 |
Correct |
13 ms |
3368 KB |
Output is correct |
6 |
Correct |
16 ms |
3408 KB |
Output is correct |
7 |
Correct |
15 ms |
3416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
38 ms |
3348 KB |
Output is correct |
8 |
Correct |
83 ms |
3352 KB |
Output is correct |
9 |
Correct |
14 ms |
3332 KB |
Output is correct |
10 |
Correct |
50 ms |
3352 KB |
Output is correct |
11 |
Correct |
23 ms |
3412 KB |
Output is correct |
12 |
Correct |
66 ms |
3284 KB |
Output is correct |
13 |
Correct |
110 ms |
9456 KB |
Output is correct |
14 |
Correct |
125 ms |
9436 KB |
Output is correct |
15 |
Correct |
172 ms |
9444 KB |
Output is correct |
16 |
Correct |
137 ms |
9420 KB |
Output is correct |
17 |
Correct |
106 ms |
9432 KB |
Output is correct |
18 |
Correct |
104 ms |
9548 KB |
Output is correct |
19 |
Correct |
182 ms |
9520 KB |
Output is correct |
20 |
Correct |
149 ms |
9548 KB |
Output is correct |
21 |
Correct |
106 ms |
9572 KB |
Output is correct |
22 |
Correct |
124 ms |
9572 KB |
Output is correct |
23 |
Correct |
130 ms |
9548 KB |
Output is correct |
24 |
Correct |
141 ms |
9540 KB |
Output is correct |
25 |
Correct |
106 ms |
9564 KB |
Output is correct |
26 |
Correct |
122 ms |
9572 KB |
Output is correct |
27 |
Correct |
129 ms |
9576 KB |
Output is correct |
28 |
Correct |
122 ms |
9560 KB |
Output is correct |
29 |
Correct |
110 ms |
9308 KB |
Output is correct |
30 |
Correct |
118 ms |
9304 KB |
Output is correct |
31 |
Correct |
150 ms |
9308 KB |
Output is correct |
32 |
Correct |
180 ms |
9304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
10 ms |
3284 KB |
Output is correct |
8 |
Correct |
11 ms |
3344 KB |
Output is correct |
9 |
Correct |
13 ms |
3272 KB |
Output is correct |
10 |
Correct |
11 ms |
3284 KB |
Output is correct |
11 |
Correct |
13 ms |
3368 KB |
Output is correct |
12 |
Correct |
16 ms |
3408 KB |
Output is correct |
13 |
Correct |
15 ms |
3416 KB |
Output is correct |
14 |
Correct |
38 ms |
3348 KB |
Output is correct |
15 |
Correct |
83 ms |
3352 KB |
Output is correct |
16 |
Correct |
14 ms |
3332 KB |
Output is correct |
17 |
Correct |
50 ms |
3352 KB |
Output is correct |
18 |
Correct |
23 ms |
3412 KB |
Output is correct |
19 |
Correct |
66 ms |
3284 KB |
Output is correct |
20 |
Correct |
110 ms |
9456 KB |
Output is correct |
21 |
Correct |
125 ms |
9436 KB |
Output is correct |
22 |
Correct |
172 ms |
9444 KB |
Output is correct |
23 |
Correct |
137 ms |
9420 KB |
Output is correct |
24 |
Correct |
106 ms |
9432 KB |
Output is correct |
25 |
Correct |
104 ms |
9548 KB |
Output is correct |
26 |
Correct |
182 ms |
9520 KB |
Output is correct |
27 |
Correct |
149 ms |
9548 KB |
Output is correct |
28 |
Correct |
106 ms |
9572 KB |
Output is correct |
29 |
Correct |
124 ms |
9572 KB |
Output is correct |
30 |
Correct |
130 ms |
9548 KB |
Output is correct |
31 |
Correct |
141 ms |
9540 KB |
Output is correct |
32 |
Correct |
106 ms |
9564 KB |
Output is correct |
33 |
Correct |
122 ms |
9572 KB |
Output is correct |
34 |
Correct |
129 ms |
9576 KB |
Output is correct |
35 |
Correct |
122 ms |
9560 KB |
Output is correct |
36 |
Correct |
110 ms |
9308 KB |
Output is correct |
37 |
Correct |
118 ms |
9304 KB |
Output is correct |
38 |
Correct |
150 ms |
9308 KB |
Output is correct |
39 |
Correct |
180 ms |
9304 KB |
Output is correct |
40 |
Correct |
29 ms |
3364 KB |
Output is correct |
41 |
Correct |
86 ms |
3284 KB |
Output is correct |
42 |
Correct |
18 ms |
3284 KB |
Output is correct |
43 |
Correct |
91 ms |
3344 KB |
Output is correct |
44 |
Correct |
22 ms |
3412 KB |
Output is correct |
45 |
Correct |
84 ms |
3388 KB |
Output is correct |
46 |
Correct |
118 ms |
9432 KB |
Output is correct |
47 |
Correct |
148 ms |
9432 KB |
Output is correct |
48 |
Correct |
130 ms |
9440 KB |
Output is correct |
49 |
Correct |
175 ms |
9428 KB |
Output is correct |
50 |
Correct |
107 ms |
9432 KB |
Output is correct |
51 |
Correct |
118 ms |
9436 KB |
Output is correct |
52 |
Correct |
100 ms |
9436 KB |
Output is correct |
53 |
Correct |
178 ms |
9524 KB |
Output is correct |
54 |
Correct |
119 ms |
9540 KB |
Output is correct |
55 |
Correct |
117 ms |
9564 KB |
Output is correct |
56 |
Correct |
168 ms |
9556 KB |
Output is correct |
57 |
Correct |
177 ms |
9544 KB |
Output is correct |
58 |
Correct |
111 ms |
9548 KB |
Output is correct |
59 |
Correct |
133 ms |
9552 KB |
Output is correct |
60 |
Correct |
173 ms |
9564 KB |
Output is correct |
61 |
Correct |
184 ms |
9700 KB |
Output is correct |
62 |
Correct |
102 ms |
9392 KB |
Output is correct |
63 |
Correct |
116 ms |
9292 KB |
Output is correct |
64 |
Correct |
129 ms |
9304 KB |
Output is correct |
65 |
Correct |
173 ms |
9312 KB |
Output is correct |