// no matter how hard you work, someone else is working harder
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a), _i = (b); i <= _i; ++ i)
#define FORD(i, b, a) for (int i = (b), _i = (a); i >= _i; -- i)
#define REP(i, b) for (int i = 0, _i = (b); i < _i; ++ i)
#define FORE(i, a) for (auto& i : a)
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define cntbit __builtin_popcountll
#define len(x) (int)x.size()
#define bit(x, i) (((x) >> (i)) & 1)
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
template <typename A, typename B>
inline bool ckmax(A& a, const B& b) {
if (a < b) {
a = b;
return true;
} return false;
}
template <typename A, typename B>
inline bool ckmin(A& a, const B& b) {
if (a > b) {
a = b;
return true;
} return false;
}
inline void fileIO(const string& Task = "") {
ios::sync_with_stdio(false); cin.tie(NULL);
if (fopen((Task + ".inp").c_str(), "r")) {
freopen((Task + ".inp").c_str(), "r", stdin);
freopen((Task + ".out").c_str(), "w", stdout);
}
}
/* Author: Hoang Quoc Viet */
/** END OF TEMPLATE **/
int n, m, k, q;
int dp[80][80][80], cost[80][80][80], tmp[80][80];
int main() {
fileIO("main");
cin >> n >> m;
memset(cost, 0x3f, sizeof cost);
FOR(i, 1, n) cost[0][i][i] = cost[1][i][i] = 0;
FOR(i, 1, m) {
int u, v, t;
cin >> u >> v >> t;
ckmin(cost[1][u][v], t);
}
cin >> k >> q;
ckmin(k, n - 1);
FOR(i, 2, k) {
memset(tmp, 0x3f, sizeof tmp);
FOR(x, 1, n) FOR(y, 1, n) FOR(z, 1, n) {
ckmin(tmp[y][z], cost[i-1][y][x] + cost[1][x][z]);
}
FOR(y, 1, n) FOR(z, 1, n) {
cost[i][y][z] = min(cost[i-1][y][z], tmp[y][z]);
}
}
while (q --) {
int u, v;
cin >> u >> v;
if (cost[k][u][v] >= 1e9) cout << "-1\n";
else cout << cost[k][u][v] << "\n";
}
return 0;
}
/*** VOI VOI VOI important things must be said three times :> ***/
Compilation message
Main.cpp: In function 'void fileIO(const string&)':
Main.cpp:36:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | freopen((Task + ".inp").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:37:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | freopen((Task + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2252 KB |
Output is correct |
2 |
Correct |
1 ms |
2252 KB |
Output is correct |
3 |
Correct |
2 ms |
2252 KB |
Output is correct |
4 |
Correct |
1 ms |
2252 KB |
Output is correct |
5 |
Correct |
1 ms |
2232 KB |
Output is correct |
6 |
Correct |
1 ms |
2252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2236 KB |
Output is correct |
2 |
Correct |
2 ms |
2380 KB |
Output is correct |
3 |
Correct |
3 ms |
2380 KB |
Output is correct |
4 |
Correct |
3 ms |
2368 KB |
Output is correct |
5 |
Correct |
4 ms |
2252 KB |
Output is correct |
6 |
Correct |
3 ms |
2252 KB |
Output is correct |
7 |
Correct |
4 ms |
2344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2252 KB |
Output is correct |
2 |
Correct |
1 ms |
2252 KB |
Output is correct |
3 |
Correct |
2 ms |
2252 KB |
Output is correct |
4 |
Correct |
1 ms |
2252 KB |
Output is correct |
5 |
Correct |
1 ms |
2232 KB |
Output is correct |
6 |
Correct |
1 ms |
2252 KB |
Output is correct |
7 |
Correct |
11 ms |
2392 KB |
Output is correct |
8 |
Correct |
27 ms |
2364 KB |
Output is correct |
9 |
Correct |
3 ms |
2252 KB |
Output is correct |
10 |
Correct |
16 ms |
2244 KB |
Output is correct |
11 |
Correct |
7 ms |
2380 KB |
Output is correct |
12 |
Correct |
20 ms |
2252 KB |
Output is correct |
13 |
Correct |
86 ms |
3020 KB |
Output is correct |
14 |
Correct |
90 ms |
3224 KB |
Output is correct |
15 |
Correct |
104 ms |
3592 KB |
Output is correct |
16 |
Correct |
99 ms |
7912 KB |
Output is correct |
17 |
Correct |
106 ms |
3964 KB |
Output is correct |
18 |
Correct |
87 ms |
3992 KB |
Output is correct |
19 |
Correct |
124 ms |
3388 KB |
Output is correct |
20 |
Correct |
104 ms |
4164 KB |
Output is correct |
21 |
Correct |
88 ms |
3920 KB |
Output is correct |
22 |
Correct |
111 ms |
6340 KB |
Output is correct |
23 |
Correct |
88 ms |
2316 KB |
Output is correct |
24 |
Correct |
116 ms |
4976 KB |
Output is correct |
25 |
Correct |
87 ms |
6128 KB |
Output is correct |
26 |
Correct |
91 ms |
2344 KB |
Output is correct |
27 |
Correct |
97 ms |
2364 KB |
Output is correct |
28 |
Correct |
94 ms |
2324 KB |
Output is correct |
29 |
Correct |
83 ms |
2328 KB |
Output is correct |
30 |
Correct |
90 ms |
6568 KB |
Output is correct |
31 |
Correct |
115 ms |
8228 KB |
Output is correct |
32 |
Correct |
110 ms |
7800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2252 KB |
Output is correct |
2 |
Correct |
1 ms |
2252 KB |
Output is correct |
3 |
Correct |
2 ms |
2252 KB |
Output is correct |
4 |
Correct |
1 ms |
2252 KB |
Output is correct |
5 |
Correct |
1 ms |
2232 KB |
Output is correct |
6 |
Correct |
1 ms |
2252 KB |
Output is correct |
7 |
Correct |
2 ms |
2236 KB |
Output is correct |
8 |
Correct |
2 ms |
2380 KB |
Output is correct |
9 |
Correct |
3 ms |
2380 KB |
Output is correct |
10 |
Correct |
3 ms |
2368 KB |
Output is correct |
11 |
Correct |
4 ms |
2252 KB |
Output is correct |
12 |
Correct |
3 ms |
2252 KB |
Output is correct |
13 |
Correct |
4 ms |
2344 KB |
Output is correct |
14 |
Correct |
11 ms |
2392 KB |
Output is correct |
15 |
Correct |
27 ms |
2364 KB |
Output is correct |
16 |
Correct |
3 ms |
2252 KB |
Output is correct |
17 |
Correct |
16 ms |
2244 KB |
Output is correct |
18 |
Correct |
7 ms |
2380 KB |
Output is correct |
19 |
Correct |
20 ms |
2252 KB |
Output is correct |
20 |
Correct |
86 ms |
3020 KB |
Output is correct |
21 |
Correct |
90 ms |
3224 KB |
Output is correct |
22 |
Correct |
104 ms |
3592 KB |
Output is correct |
23 |
Correct |
99 ms |
7912 KB |
Output is correct |
24 |
Correct |
106 ms |
3964 KB |
Output is correct |
25 |
Correct |
87 ms |
3992 KB |
Output is correct |
26 |
Correct |
124 ms |
3388 KB |
Output is correct |
27 |
Correct |
104 ms |
4164 KB |
Output is correct |
28 |
Correct |
88 ms |
3920 KB |
Output is correct |
29 |
Correct |
111 ms |
6340 KB |
Output is correct |
30 |
Correct |
88 ms |
2316 KB |
Output is correct |
31 |
Correct |
116 ms |
4976 KB |
Output is correct |
32 |
Correct |
87 ms |
6128 KB |
Output is correct |
33 |
Correct |
91 ms |
2344 KB |
Output is correct |
34 |
Correct |
97 ms |
2364 KB |
Output is correct |
35 |
Correct |
94 ms |
2324 KB |
Output is correct |
36 |
Correct |
83 ms |
2328 KB |
Output is correct |
37 |
Correct |
90 ms |
6568 KB |
Output is correct |
38 |
Correct |
115 ms |
8228 KB |
Output is correct |
39 |
Correct |
110 ms |
7800 KB |
Output is correct |
40 |
Correct |
9 ms |
2244 KB |
Output is correct |
41 |
Correct |
27 ms |
2336 KB |
Output is correct |
42 |
Correct |
6 ms |
2372 KB |
Output is correct |
43 |
Correct |
29 ms |
2352 KB |
Output is correct |
44 |
Correct |
6 ms |
2372 KB |
Output is correct |
45 |
Correct |
28 ms |
2344 KB |
Output is correct |
46 |
Correct |
85 ms |
3188 KB |
Output is correct |
47 |
Correct |
109 ms |
5796 KB |
Output is correct |
48 |
Correct |
88 ms |
4072 KB |
Output is correct |
49 |
Correct |
112 ms |
7572 KB |
Output is correct |
50 |
Correct |
94 ms |
6376 KB |
Output is correct |
51 |
Correct |
102 ms |
6696 KB |
Output is correct |
52 |
Correct |
94 ms |
7756 KB |
Output is correct |
53 |
Correct |
119 ms |
6608 KB |
Output is correct |
54 |
Correct |
86 ms |
3220 KB |
Output is correct |
55 |
Correct |
88 ms |
5428 KB |
Output is correct |
56 |
Correct |
127 ms |
8120 KB |
Output is correct |
57 |
Correct |
119 ms |
5028 KB |
Output is correct |
58 |
Correct |
106 ms |
2356 KB |
Output is correct |
59 |
Correct |
118 ms |
2360 KB |
Output is correct |
60 |
Correct |
108 ms |
2316 KB |
Output is correct |
61 |
Correct |
110 ms |
2444 KB |
Output is correct |
62 |
Correct |
105 ms |
2404 KB |
Output is correct |
63 |
Correct |
92 ms |
2352 KB |
Output is correct |
64 |
Correct |
92 ms |
2424 KB |
Output is correct |
65 |
Correct |
110 ms |
5912 KB |
Output is correct |