#include <bits/stdc++.h>
using namespace std;
#define int long long
#define int long long
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define foru(i, l, r) for(int i = l; i <= r; i++)
#define ford(i, r, l) for(int i = r; i >= l; i--)
#define __TIME (1.0 * clock() / CLOCKS_PER_SEC)
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
const int N = 1e6 + 5;
const int oo = 1e9, mod = 1e9 + 7;
int n, m, k, q;
string s;
vector<ii> adj[N];
// precalc
void process() {
cin >> n >> m;
vector<vector<int>> c(n+1, vector<int>(n+1, oo));
foru(i,0,n-1) c[i][i]=0;
foru(i,1,m) {
int u, v, t; cin >> u >> v >> t;
u--, v--;
c[u][v]=min(c[u][v], t);
}
cin >> k >> q;
k = min(k, n);
auto sol = c;
while (--k) {
auto new_sol = sol;
for (int x = 0; x < n; x++)
for (int y = 0; y < n; y++)
for (int z = 0; z < n; z++)
new_sol[x][y] = min(new_sol[x][y], sol[x][z] + c[z][y]);
sol = new_sol;
}
while (q--) {
int s, t;
cin >> s >> t;
s--, t--;
if (sol[s][t]>=oo) cout << -1 << '\n'; else cout << sol[s][t] << '\n';
}
return;
}
signed main() {
cin.tie(0)->sync_with_stdio(false);
//freopen(".inp", "r", stdin);
//freopen(".out", "w", stdout);
process();
cerr << "Time elapsed: " << __TIME << " s.\n";
return 0;
}
/*
Xét các trường hợp đặc biệt
Kiểm tra lại input/output
Cố gắng trâu
Lật ngược bài toán
Keep calm and get VOI
Flow:
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
23896 KB |
Output is correct |
2 |
Correct |
5 ms |
23900 KB |
Output is correct |
3 |
Correct |
6 ms |
23900 KB |
Output is correct |
4 |
Correct |
5 ms |
23912 KB |
Output is correct |
5 |
Correct |
5 ms |
23900 KB |
Output is correct |
6 |
Correct |
6 ms |
23900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
23900 KB |
Output is correct |
2 |
Correct |
7 ms |
23900 KB |
Output is correct |
3 |
Correct |
7 ms |
23900 KB |
Output is correct |
4 |
Correct |
8 ms |
23900 KB |
Output is correct |
5 |
Correct |
7 ms |
23900 KB |
Output is correct |
6 |
Correct |
7 ms |
24156 KB |
Output is correct |
7 |
Correct |
8 ms |
24152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
23896 KB |
Output is correct |
2 |
Correct |
5 ms |
23900 KB |
Output is correct |
3 |
Correct |
6 ms |
23900 KB |
Output is correct |
4 |
Correct |
5 ms |
23912 KB |
Output is correct |
5 |
Correct |
5 ms |
23900 KB |
Output is correct |
6 |
Correct |
6 ms |
23900 KB |
Output is correct |
7 |
Correct |
14 ms |
23900 KB |
Output is correct |
8 |
Correct |
29 ms |
24120 KB |
Output is correct |
9 |
Correct |
7 ms |
23900 KB |
Output is correct |
10 |
Correct |
18 ms |
23956 KB |
Output is correct |
11 |
Correct |
10 ms |
24156 KB |
Output is correct |
12 |
Correct |
23 ms |
23924 KB |
Output is correct |
13 |
Correct |
79 ms |
27472 KB |
Output is correct |
14 |
Correct |
77 ms |
27216 KB |
Output is correct |
15 |
Correct |
96 ms |
27288 KB |
Output is correct |
16 |
Correct |
85 ms |
27276 KB |
Output is correct |
17 |
Correct |
78 ms |
27156 KB |
Output is correct |
18 |
Correct |
79 ms |
27216 KB |
Output is correct |
19 |
Correct |
102 ms |
27292 KB |
Output is correct |
20 |
Correct |
89 ms |
27380 KB |
Output is correct |
21 |
Correct |
79 ms |
27412 KB |
Output is correct |
22 |
Correct |
82 ms |
27220 KB |
Output is correct |
23 |
Correct |
80 ms |
27220 KB |
Output is correct |
24 |
Correct |
89 ms |
27388 KB |
Output is correct |
25 |
Correct |
80 ms |
27216 KB |
Output is correct |
26 |
Correct |
83 ms |
27220 KB |
Output is correct |
27 |
Correct |
85 ms |
27216 KB |
Output is correct |
28 |
Correct |
92 ms |
27220 KB |
Output is correct |
29 |
Correct |
76 ms |
27296 KB |
Output is correct |
30 |
Correct |
81 ms |
27148 KB |
Output is correct |
31 |
Correct |
93 ms |
27148 KB |
Output is correct |
32 |
Correct |
101 ms |
27148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
23896 KB |
Output is correct |
2 |
Correct |
5 ms |
23900 KB |
Output is correct |
3 |
Correct |
6 ms |
23900 KB |
Output is correct |
4 |
Correct |
5 ms |
23912 KB |
Output is correct |
5 |
Correct |
5 ms |
23900 KB |
Output is correct |
6 |
Correct |
6 ms |
23900 KB |
Output is correct |
7 |
Correct |
7 ms |
23900 KB |
Output is correct |
8 |
Correct |
7 ms |
23900 KB |
Output is correct |
9 |
Correct |
7 ms |
23900 KB |
Output is correct |
10 |
Correct |
8 ms |
23900 KB |
Output is correct |
11 |
Correct |
7 ms |
23900 KB |
Output is correct |
12 |
Correct |
7 ms |
24156 KB |
Output is correct |
13 |
Correct |
8 ms |
24152 KB |
Output is correct |
14 |
Correct |
14 ms |
23900 KB |
Output is correct |
15 |
Correct |
29 ms |
24120 KB |
Output is correct |
16 |
Correct |
7 ms |
23900 KB |
Output is correct |
17 |
Correct |
18 ms |
23956 KB |
Output is correct |
18 |
Correct |
10 ms |
24156 KB |
Output is correct |
19 |
Correct |
23 ms |
23924 KB |
Output is correct |
20 |
Correct |
79 ms |
27472 KB |
Output is correct |
21 |
Correct |
77 ms |
27216 KB |
Output is correct |
22 |
Correct |
96 ms |
27288 KB |
Output is correct |
23 |
Correct |
85 ms |
27276 KB |
Output is correct |
24 |
Correct |
78 ms |
27156 KB |
Output is correct |
25 |
Correct |
79 ms |
27216 KB |
Output is correct |
26 |
Correct |
102 ms |
27292 KB |
Output is correct |
27 |
Correct |
89 ms |
27380 KB |
Output is correct |
28 |
Correct |
79 ms |
27412 KB |
Output is correct |
29 |
Correct |
82 ms |
27220 KB |
Output is correct |
30 |
Correct |
80 ms |
27220 KB |
Output is correct |
31 |
Correct |
89 ms |
27388 KB |
Output is correct |
32 |
Correct |
80 ms |
27216 KB |
Output is correct |
33 |
Correct |
83 ms |
27220 KB |
Output is correct |
34 |
Correct |
85 ms |
27216 KB |
Output is correct |
35 |
Correct |
92 ms |
27220 KB |
Output is correct |
36 |
Correct |
76 ms |
27296 KB |
Output is correct |
37 |
Correct |
81 ms |
27148 KB |
Output is correct |
38 |
Correct |
93 ms |
27148 KB |
Output is correct |
39 |
Correct |
101 ms |
27148 KB |
Output is correct |
40 |
Correct |
12 ms |
23900 KB |
Output is correct |
41 |
Correct |
30 ms |
24076 KB |
Output is correct |
42 |
Correct |
9 ms |
23900 KB |
Output is correct |
43 |
Correct |
29 ms |
24104 KB |
Output is correct |
44 |
Correct |
10 ms |
24144 KB |
Output is correct |
45 |
Correct |
30 ms |
24156 KB |
Output is correct |
46 |
Correct |
77 ms |
27220 KB |
Output is correct |
47 |
Correct |
100 ms |
27216 KB |
Output is correct |
48 |
Correct |
80 ms |
27216 KB |
Output is correct |
49 |
Correct |
99 ms |
27276 KB |
Output is correct |
50 |
Correct |
78 ms |
27216 KB |
Output is correct |
51 |
Correct |
81 ms |
27224 KB |
Output is correct |
52 |
Correct |
78 ms |
27216 KB |
Output is correct |
53 |
Correct |
99 ms |
27288 KB |
Output is correct |
54 |
Correct |
80 ms |
27560 KB |
Output is correct |
55 |
Correct |
77 ms |
27216 KB |
Output is correct |
56 |
Correct |
97 ms |
27292 KB |
Output is correct |
57 |
Correct |
99 ms |
27224 KB |
Output is correct |
58 |
Correct |
81 ms |
27220 KB |
Output is correct |
59 |
Correct |
89 ms |
27228 KB |
Output is correct |
60 |
Correct |
96 ms |
27292 KB |
Output is correct |
61 |
Correct |
99 ms |
23976 KB |
Output is correct |
62 |
Correct |
74 ms |
23972 KB |
Output is correct |
63 |
Correct |
81 ms |
23960 KB |
Output is correct |
64 |
Correct |
81 ms |
23976 KB |
Output is correct |
65 |
Correct |
96 ms |
24040 KB |
Output is correct |