#include "bits/stdc++.h"
#define int long long
using namespace std;
const int MAXN = 1e5 + 10;
void solve(int tc) {
int n;
cin >> n;
int m;
cin >> m;
int raw[n+1][n+1];
for(int i=0; i<=n; i++) {
for(int j=0; j<=n; j++) {
raw[i][j] = (i==j ? 0 : 1e16);
}
}
for(int i=1; i<=m; i++) {
int a, b, t;
cin >> a >> b >> t;
raw[a][b] = min(raw[a][b], t);
}
int K, q; cin >> K >> q;
K = min(K, n);
int dp[K+1][n+1][n+1];
for(int i=1; i<=n; i++) {
for(int j=1; j<=n; j++) {
dp[1][i][j] = raw[i][j];
}
}
for(int i=2; i<=K; i++) {
for(int j=1; j<=n; j++) {
for(int k=1; k<=n; k++) {
dp[i][j][k] = 1e16;
for(int l=1; l<=n; l++) {
dp[i][j][k] = min(min(dp[i][j][k], dp[i-1][j][k]), dp[i-1][j][l] + raw[l][k]);
}
}
}
}
while(q--) {
int c, d;
cin >> c >> d;
cout << (dp[K][c][d] == 1e16 ? -1 : dp[K][c][d]) << "\n";
}
}
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
int t=1; //cin >> t;
for(int i=1; i<=t; i++) solve(i);
}
/*
4 7
1 2 1
1 4 10
2 3 1
2 4 5
3 2 2
3 4 1
4 3 2
2 3
1 4
4 2
3 3
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
316 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
3 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
3 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
316 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
13 ms |
1364 KB |
Output is correct |
8 |
Correct |
34 ms |
3020 KB |
Output is correct |
9 |
Correct |
3 ms |
584 KB |
Output is correct |
10 |
Correct |
20 ms |
1892 KB |
Output is correct |
11 |
Correct |
6 ms |
844 KB |
Output is correct |
12 |
Correct |
24 ms |
2232 KB |
Output is correct |
13 |
Correct |
72 ms |
6608 KB |
Output is correct |
14 |
Correct |
72 ms |
6732 KB |
Output is correct |
15 |
Correct |
99 ms |
8776 KB |
Output is correct |
16 |
Correct |
80 ms |
7244 KB |
Output is correct |
17 |
Correct |
76 ms |
6736 KB |
Output is correct |
18 |
Correct |
73 ms |
6608 KB |
Output is correct |
19 |
Correct |
104 ms |
9288 KB |
Output is correct |
20 |
Correct |
89 ms |
8112 KB |
Output is correct |
21 |
Correct |
92 ms |
6732 KB |
Output is correct |
22 |
Correct |
78 ms |
7116 KB |
Output is correct |
23 |
Correct |
78 ms |
7132 KB |
Output is correct |
24 |
Correct |
114 ms |
8008 KB |
Output is correct |
25 |
Correct |
75 ms |
6860 KB |
Output is correct |
26 |
Correct |
80 ms |
7368 KB |
Output is correct |
27 |
Correct |
84 ms |
7632 KB |
Output is correct |
28 |
Correct |
91 ms |
7492 KB |
Output is correct |
29 |
Correct |
74 ms |
6596 KB |
Output is correct |
30 |
Correct |
78 ms |
7064 KB |
Output is correct |
31 |
Correct |
89 ms |
7892 KB |
Output is correct |
32 |
Correct |
105 ms |
9164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
316 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
3 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
3 ms |
596 KB |
Output is correct |
14 |
Correct |
13 ms |
1364 KB |
Output is correct |
15 |
Correct |
34 ms |
3020 KB |
Output is correct |
16 |
Correct |
3 ms |
584 KB |
Output is correct |
17 |
Correct |
20 ms |
1892 KB |
Output is correct |
18 |
Correct |
6 ms |
844 KB |
Output is correct |
19 |
Correct |
24 ms |
2232 KB |
Output is correct |
20 |
Correct |
72 ms |
6608 KB |
Output is correct |
21 |
Correct |
72 ms |
6732 KB |
Output is correct |
22 |
Correct |
99 ms |
8776 KB |
Output is correct |
23 |
Correct |
80 ms |
7244 KB |
Output is correct |
24 |
Correct |
76 ms |
6736 KB |
Output is correct |
25 |
Correct |
73 ms |
6608 KB |
Output is correct |
26 |
Correct |
104 ms |
9288 KB |
Output is correct |
27 |
Correct |
89 ms |
8112 KB |
Output is correct |
28 |
Correct |
92 ms |
6732 KB |
Output is correct |
29 |
Correct |
78 ms |
7116 KB |
Output is correct |
30 |
Correct |
78 ms |
7132 KB |
Output is correct |
31 |
Correct |
114 ms |
8008 KB |
Output is correct |
32 |
Correct |
75 ms |
6860 KB |
Output is correct |
33 |
Correct |
80 ms |
7368 KB |
Output is correct |
34 |
Correct |
84 ms |
7632 KB |
Output is correct |
35 |
Correct |
91 ms |
7492 KB |
Output is correct |
36 |
Correct |
74 ms |
6596 KB |
Output is correct |
37 |
Correct |
78 ms |
7064 KB |
Output is correct |
38 |
Correct |
89 ms |
7892 KB |
Output is correct |
39 |
Correct |
105 ms |
9164 KB |
Output is correct |
40 |
Correct |
10 ms |
1108 KB |
Output is correct |
41 |
Correct |
35 ms |
3172 KB |
Output is correct |
42 |
Correct |
5 ms |
724 KB |
Output is correct |
43 |
Correct |
36 ms |
3184 KB |
Output is correct |
44 |
Correct |
6 ms |
852 KB |
Output is correct |
45 |
Correct |
36 ms |
3228 KB |
Output is correct |
46 |
Correct |
91 ms |
6728 KB |
Output is correct |
47 |
Correct |
89 ms |
7864 KB |
Output is correct |
48 |
Correct |
82 ms |
7248 KB |
Output is correct |
49 |
Correct |
104 ms |
9288 KB |
Output is correct |
50 |
Correct |
74 ms |
6728 KB |
Output is correct |
51 |
Correct |
82 ms |
7244 KB |
Output is correct |
52 |
Correct |
73 ms |
6604 KB |
Output is correct |
53 |
Correct |
114 ms |
9308 KB |
Output is correct |
54 |
Correct |
73 ms |
6728 KB |
Output is correct |
55 |
Correct |
74 ms |
6860 KB |
Output is correct |
56 |
Correct |
102 ms |
9044 KB |
Output is correct |
57 |
Correct |
106 ms |
9388 KB |
Output is correct |
58 |
Correct |
80 ms |
6820 KB |
Output is correct |
59 |
Correct |
87 ms |
7764 KB |
Output is correct |
60 |
Correct |
100 ms |
8908 KB |
Output is correct |
61 |
Correct |
107 ms |
9484 KB |
Output is correct |
62 |
Correct |
73 ms |
6608 KB |
Output is correct |
63 |
Correct |
95 ms |
7172 KB |
Output is correct |
64 |
Correct |
82 ms |
7368 KB |
Output is correct |
65 |
Correct |
117 ms |
9160 KB |
Output is correct |