#include <bits/stdc++.h>
typedef long long int ll;
const ll inf = 1e18;
using namespace std;
int main(){
ios_base::sync_with_stdio(false); cin.tie(NULL);
ll n, m;
cin >> n >> m;
vector<array<ll, 3>> edge(m);
for(auto &[u, v, w] : edge)
cin >> u >> v >> w, --u, --v;
ll val[n][n];
for(ll i = 0; i < n; i++)
for(ll j = 0; j < n; j++)
val[i][j] = inf;
for(auto [u, v, w] : edge)
val[u][v] = min(val[u][v], w);
ll k, q;
cin >> k >> q;
if(k > n - 1)
k = n - 1;
ll dp[k + 1][n][n];
for(ll l = 0; l <= k; l++)
for(ll i = 0; i < n; i++)
for(ll j = 0; j < n; j++)
dp[l][i][j] = inf;
for(ll i = 0; i < n; i++)
dp[0][i][i] = 0;
for(ll l = 1; l <= k; l++)
for(ll v = 0; v < n; v++)
for(ll i = 0; i < n; i++)
for(ll j = 0; j < n; j++)
if(dp[l - 1][i][v] != inf && edge[v][j] != inf)
dp[l][i][j] = min(dp[l][i][j], dp[l - 1][i][v] + val[v][j]);
while(q--){
ll u, v, ans = inf;
cin >> u >> v; --u; --v;
for(ll l = 0; l <= k; l++)
ans = min(ans, dp[l][u][v]);
cout << (ans != inf ? ans : -1) << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 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 |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
460 KB |
Output is correct |
2 |
Correct |
3 ms |
472 KB |
Output is correct |
3 |
Correct |
3 ms |
460 KB |
Output is correct |
4 |
Correct |
3 ms |
460 KB |
Output is correct |
5 |
Correct |
3 ms |
460 KB |
Output is correct |
6 |
Correct |
3 ms |
588 KB |
Output is correct |
7 |
Correct |
4 ms |
588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 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 |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
12 ms |
1316 KB |
Output is correct |
8 |
Correct |
32 ms |
2984 KB |
Output is correct |
9 |
Correct |
4 ms |
460 KB |
Output is correct |
10 |
Correct |
18 ms |
1832 KB |
Output is correct |
11 |
Correct |
6 ms |
820 KB |
Output is correct |
12 |
Correct |
22 ms |
2124 KB |
Output is correct |
13 |
Correct |
98 ms |
11964 KB |
Output is correct |
14 |
Correct |
95 ms |
12088 KB |
Output is correct |
15 |
Correct |
116 ms |
14096 KB |
Output is correct |
16 |
Correct |
101 ms |
12600 KB |
Output is correct |
17 |
Correct |
99 ms |
12172 KB |
Output is correct |
18 |
Correct |
94 ms |
12172 KB |
Output is correct |
19 |
Correct |
123 ms |
14660 KB |
Output is correct |
20 |
Correct |
112 ms |
13576 KB |
Output is correct |
21 |
Correct |
95 ms |
12164 KB |
Output is correct |
22 |
Correct |
102 ms |
12612 KB |
Output is correct |
23 |
Correct |
104 ms |
12560 KB |
Output is correct |
24 |
Correct |
110 ms |
13448 KB |
Output is correct |
25 |
Correct |
98 ms |
12516 KB |
Output is correct |
26 |
Correct |
103 ms |
13032 KB |
Output is correct |
27 |
Correct |
111 ms |
13380 KB |
Output is correct |
28 |
Correct |
104 ms |
13168 KB |
Output is correct |
29 |
Correct |
96 ms |
12068 KB |
Output is correct |
30 |
Correct |
98 ms |
12484 KB |
Output is correct |
31 |
Correct |
108 ms |
13252 KB |
Output is correct |
32 |
Correct |
134 ms |
14508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 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 |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
2 ms |
460 KB |
Output is correct |
8 |
Correct |
3 ms |
472 KB |
Output is correct |
9 |
Correct |
3 ms |
460 KB |
Output is correct |
10 |
Correct |
3 ms |
460 KB |
Output is correct |
11 |
Correct |
3 ms |
460 KB |
Output is correct |
12 |
Correct |
3 ms |
588 KB |
Output is correct |
13 |
Correct |
4 ms |
588 KB |
Output is correct |
14 |
Correct |
12 ms |
1316 KB |
Output is correct |
15 |
Correct |
32 ms |
2984 KB |
Output is correct |
16 |
Correct |
4 ms |
460 KB |
Output is correct |
17 |
Correct |
18 ms |
1832 KB |
Output is correct |
18 |
Correct |
6 ms |
820 KB |
Output is correct |
19 |
Correct |
22 ms |
2124 KB |
Output is correct |
20 |
Correct |
98 ms |
11964 KB |
Output is correct |
21 |
Correct |
95 ms |
12088 KB |
Output is correct |
22 |
Correct |
116 ms |
14096 KB |
Output is correct |
23 |
Correct |
101 ms |
12600 KB |
Output is correct |
24 |
Correct |
99 ms |
12172 KB |
Output is correct |
25 |
Correct |
94 ms |
12172 KB |
Output is correct |
26 |
Correct |
123 ms |
14660 KB |
Output is correct |
27 |
Correct |
112 ms |
13576 KB |
Output is correct |
28 |
Correct |
95 ms |
12164 KB |
Output is correct |
29 |
Correct |
102 ms |
12612 KB |
Output is correct |
30 |
Correct |
104 ms |
12560 KB |
Output is correct |
31 |
Correct |
110 ms |
13448 KB |
Output is correct |
32 |
Correct |
98 ms |
12516 KB |
Output is correct |
33 |
Correct |
103 ms |
13032 KB |
Output is correct |
34 |
Correct |
111 ms |
13380 KB |
Output is correct |
35 |
Correct |
104 ms |
13168 KB |
Output is correct |
36 |
Correct |
96 ms |
12068 KB |
Output is correct |
37 |
Correct |
98 ms |
12484 KB |
Output is correct |
38 |
Correct |
108 ms |
13252 KB |
Output is correct |
39 |
Correct |
134 ms |
14508 KB |
Output is correct |
40 |
Correct |
9 ms |
1100 KB |
Output is correct |
41 |
Correct |
31 ms |
3028 KB |
Output is correct |
42 |
Correct |
5 ms |
716 KB |
Output is correct |
43 |
Correct |
32 ms |
3020 KB |
Output is correct |
44 |
Correct |
7 ms |
844 KB |
Output is correct |
45 |
Correct |
33 ms |
3148 KB |
Output is correct |
46 |
Correct |
94 ms |
12104 KB |
Output is correct |
47 |
Correct |
126 ms |
13124 KB |
Output is correct |
48 |
Correct |
98 ms |
12476 KB |
Output is correct |
49 |
Correct |
123 ms |
14528 KB |
Output is correct |
50 |
Correct |
96 ms |
12228 KB |
Output is correct |
51 |
Correct |
102 ms |
12816 KB |
Output is correct |
52 |
Correct |
95 ms |
12168 KB |
Output is correct |
53 |
Correct |
123 ms |
14732 KB |
Output is correct |
54 |
Correct |
97 ms |
12168 KB |
Output is correct |
55 |
Correct |
96 ms |
12308 KB |
Output is correct |
56 |
Correct |
121 ms |
14532 KB |
Output is correct |
57 |
Correct |
123 ms |
14740 KB |
Output is correct |
58 |
Correct |
96 ms |
12516 KB |
Output is correct |
59 |
Correct |
115 ms |
13572 KB |
Output is correct |
60 |
Correct |
119 ms |
14564 KB |
Output is correct |
61 |
Correct |
131 ms |
15072 KB |
Output is correct |
62 |
Correct |
93 ms |
11944 KB |
Output is correct |
63 |
Correct |
103 ms |
12584 KB |
Output is correct |
64 |
Correct |
104 ms |
12704 KB |
Output is correct |
65 |
Correct |
132 ms |
14532 KB |
Output is correct |