#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define fi first
#define se second
#define For(i, l, r) for (auto i = (l); i < (r); i++)
#define ForE(i, l, r) for (auto i = (l); i <= (r); i++)
#define FordE(i, l, r) for (auto i = (l); i >= (r); i--)
#define Fora(v, a) for (auto v: (a))
#define bend(a) (a).begin(), (a).end()
#define isz(a) ((signed)(a).size())
using ll = long long;
using ld = long double;
using pii = pair <int, int>;
using vi = vector <int>;
using vpii = vector <pii>;
using vvi = vector <vi>;
const int N = 70 + 5, inf = 1e9 + 7;
int n, m;
int adj[N][N];
int dp[N][N][N];
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// freopen("KEK.inp", "r", stdin);
// freopen("KEK.out", "w", stdout);
cin >> n >> m;
ForE(u, 1, n){
ForE(v, 1, n){
adj[u][v] = inf;
}
}
while (m--){
int u, v, w; cin >> u >> v >> w;
adj[u][v] = min(adj[u][v], w);
}
int k, q; cin >> k >> q; k = min(k, n);
ForE(u, 1, n){
ForE(v, 1, n){
dp[0][u][v] = (u == v ? 0 : inf);
}
}
ForE(i, 1, k){
ForE(u, 1, n){
ForE(v, 1, n){
dp[i][u][v] = (u == v ? 0 : inf);
}
}
ForE(w, 1, n){
ForE(u, 1, n){
ForE(v, 1, n){
dp[i][u][v] = min(dp[i][u][v], dp[i - 1][u][w] + adj[w][v]);
}
}
}
}
while (q--){
int u, v; cin >> u >> v;
cout << (dp[k][u][v] == inf ? -1 : dp[k][u][v]) << endl;
}
}
/*
==================================================+
INPUT |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
OUTPUT |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
476 KB |
Output is correct |
3 |
Correct |
3 ms |
472 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
3 ms |
476 KB |
Output is correct |
7 |
Correct |
3 ms |
520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
328 KB |
Output is correct |
7 |
Correct |
10 ms |
852 KB |
Output is correct |
8 |
Correct |
25 ms |
1892 KB |
Output is correct |
9 |
Correct |
4 ms |
476 KB |
Output is correct |
10 |
Correct |
14 ms |
1124 KB |
Output is correct |
11 |
Correct |
7 ms |
604 KB |
Output is correct |
12 |
Correct |
18 ms |
1340 KB |
Output is correct |
13 |
Correct |
89 ms |
3108 KB |
Output is correct |
14 |
Correct |
95 ms |
3124 KB |
Output is correct |
15 |
Correct |
114 ms |
4264 KB |
Output is correct |
16 |
Correct |
103 ms |
3688 KB |
Output is correct |
17 |
Correct |
93 ms |
3276 KB |
Output is correct |
18 |
Correct |
94 ms |
3232 KB |
Output is correct |
19 |
Correct |
117 ms |
4704 KB |
Output is correct |
20 |
Correct |
104 ms |
4164 KB |
Output is correct |
21 |
Correct |
101 ms |
3372 KB |
Output is correct |
22 |
Correct |
106 ms |
3660 KB |
Output is correct |
23 |
Correct |
108 ms |
3500 KB |
Output is correct |
24 |
Correct |
118 ms |
4192 KB |
Output is correct |
25 |
Correct |
95 ms |
3276 KB |
Output is correct |
26 |
Correct |
96 ms |
3644 KB |
Output is correct |
27 |
Correct |
109 ms |
3832 KB |
Output is correct |
28 |
Correct |
105 ms |
3600 KB |
Output is correct |
29 |
Correct |
101 ms |
3276 KB |
Output is correct |
30 |
Correct |
97 ms |
3612 KB |
Output is correct |
31 |
Correct |
102 ms |
4048 KB |
Output is correct |
32 |
Correct |
115 ms |
4692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
328 KB |
Output is correct |
7 |
Correct |
2 ms |
344 KB |
Output is correct |
8 |
Correct |
2 ms |
476 KB |
Output is correct |
9 |
Correct |
3 ms |
472 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
3 ms |
476 KB |
Output is correct |
13 |
Correct |
3 ms |
520 KB |
Output is correct |
14 |
Correct |
10 ms |
852 KB |
Output is correct |
15 |
Correct |
25 ms |
1892 KB |
Output is correct |
16 |
Correct |
4 ms |
476 KB |
Output is correct |
17 |
Correct |
14 ms |
1124 KB |
Output is correct |
18 |
Correct |
7 ms |
604 KB |
Output is correct |
19 |
Correct |
18 ms |
1340 KB |
Output is correct |
20 |
Correct |
89 ms |
3108 KB |
Output is correct |
21 |
Correct |
95 ms |
3124 KB |
Output is correct |
22 |
Correct |
114 ms |
4264 KB |
Output is correct |
23 |
Correct |
103 ms |
3688 KB |
Output is correct |
24 |
Correct |
93 ms |
3276 KB |
Output is correct |
25 |
Correct |
94 ms |
3232 KB |
Output is correct |
26 |
Correct |
117 ms |
4704 KB |
Output is correct |
27 |
Correct |
104 ms |
4164 KB |
Output is correct |
28 |
Correct |
101 ms |
3372 KB |
Output is correct |
29 |
Correct |
106 ms |
3660 KB |
Output is correct |
30 |
Correct |
108 ms |
3500 KB |
Output is correct |
31 |
Correct |
118 ms |
4192 KB |
Output is correct |
32 |
Correct |
95 ms |
3276 KB |
Output is correct |
33 |
Correct |
96 ms |
3644 KB |
Output is correct |
34 |
Correct |
109 ms |
3832 KB |
Output is correct |
35 |
Correct |
105 ms |
3600 KB |
Output is correct |
36 |
Correct |
101 ms |
3276 KB |
Output is correct |
37 |
Correct |
97 ms |
3612 KB |
Output is correct |
38 |
Correct |
102 ms |
4048 KB |
Output is correct |
39 |
Correct |
115 ms |
4692 KB |
Output is correct |
40 |
Correct |
8 ms |
724 KB |
Output is correct |
41 |
Correct |
27 ms |
1936 KB |
Output is correct |
42 |
Correct |
6 ms |
596 KB |
Output is correct |
43 |
Correct |
30 ms |
1932 KB |
Output is correct |
44 |
Correct |
6 ms |
596 KB |
Output is correct |
45 |
Correct |
27 ms |
1864 KB |
Output is correct |
46 |
Correct |
89 ms |
3336 KB |
Output is correct |
47 |
Correct |
120 ms |
4008 KB |
Output is correct |
48 |
Correct |
93 ms |
3632 KB |
Output is correct |
49 |
Correct |
132 ms |
4808 KB |
Output is correct |
50 |
Correct |
103 ms |
3360 KB |
Output is correct |
51 |
Correct |
112 ms |
3676 KB |
Output is correct |
52 |
Correct |
89 ms |
3416 KB |
Output is correct |
53 |
Correct |
120 ms |
4920 KB |
Output is correct |
54 |
Correct |
95 ms |
3568 KB |
Output is correct |
55 |
Correct |
91 ms |
2512 KB |
Output is correct |
56 |
Correct |
111 ms |
3460 KB |
Output is correct |
57 |
Correct |
130 ms |
3700 KB |
Output is correct |
58 |
Correct |
92 ms |
2116 KB |
Output is correct |
59 |
Correct |
100 ms |
2704 KB |
Output is correct |
60 |
Correct |
117 ms |
3300 KB |
Output is correct |
61 |
Correct |
141 ms |
3684 KB |
Output is correct |
62 |
Correct |
102 ms |
2196 KB |
Output is correct |
63 |
Correct |
97 ms |
2220 KB |
Output is correct |
64 |
Correct |
107 ms |
2776 KB |
Output is correct |
65 |
Correct |
122 ms |
3652 KB |
Output is correct |