//Then
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
#define int long long
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair <int, int>;
mt19937_64 Rand(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 80 + 1;
//const int Mod = 1e9 + 7;
const ll inf = 0x3f3f3f3f3f3f3f3f;
int n, k, q, m;
int c[maxN][maxN];
struct TEdge{
int u, i, w;
};
struct TPQ{
int lab, u, i;
bool operator < (const TPQ &other) const{
return lab > other.lab;
}
};
vector <TEdge> adj[maxN][maxN];
ll d[maxN][maxN][maxN];
void Dijkstra(int s){
memset(d[s], 0x3f, sizeof(d[s]));
priority_queue <TPQ> PQ;
d[s][s][0] = 0;
PQ.push({0, s, 0});
while (!PQ.empty()){
auto r = PQ.top(); PQ.pop();
if (d[s][r.u][r.i] != r.lab) continue;
for (auto i: adj[r.u][r.i]){
if (d[s][i.u][i.i] > d[s][r.u][r.i] + i.w){
d[s][i.u][i.i] = d[s][r.u][r.i] + i.w;
PQ.push({d[s][i.u][i.i], i.u, i.i});
}
}
}
for (int i = 1; i <= n; ++i){
for (int j = 1; j <= n; ++j){
d[s][i][j] = min(d[s][i][j], d[s][i][j - 1]);
}
}
}
void Init(){
cin >> n >> m;
memset(c, 0x3f, sizeof(c));
for(int i = 1; i <= m; ++i){
int u, v, w;
cin >> u >> v >> w;
c[u][v] = min(c[u][v], w);
}
cin >> k >> q;
k = min(k, n);
for (int i = 1; i <= n; ++i){
for (int j = 1; j <= n; ++j){
if (c[i][j] == inf) continue;
for (int l = 0; l < n; ++l){
adj[i][l].pb({j, l + 1, c[i][j]});
}
}
}
for (int i = 1; i <= n; ++i){
Dijkstra(i);
}
while (q--){
int u, v;
cin >> u >> v;
cout << (d[u][v][k] == inf ? -1 : d[u][v][k]) << "\n";
}
}
#define debug
#define taskname "test"
signed main(){
faster
if (fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
freopen(taskname".out", "w", stdout);
}
int tt = 1;
//cin >> tt;
while (tt--){
Init();
}
if (fopen("timeout.txt", "r")){
ofstream timeout("timeout.txt");
timeout << signed(double(clock()) / CLOCKS_PER_SEC * 1000);
timeout.close();
#ifndef debug
cerr << "Time elapsed: " << signed(double(clock()) / CLOCKS_PER_SEC * 1000) << "ms\n";
#endif // debug
}
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:88:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
88 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:89:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | freopen(taskname".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
852 KB |
Output is correct |
2 |
Correct |
1 ms |
852 KB |
Output is correct |
3 |
Correct |
1 ms |
872 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
852 KB |
Output is correct |
6 |
Correct |
1 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
89 ms |
5256 KB |
Output is correct |
2 |
Correct |
85 ms |
5232 KB |
Output is correct |
3 |
Correct |
80 ms |
5260 KB |
Output is correct |
4 |
Correct |
103 ms |
6828 KB |
Output is correct |
5 |
Correct |
106 ms |
6732 KB |
Output is correct |
6 |
Correct |
215 ms |
12304 KB |
Output is correct |
7 |
Correct |
210 ms |
12244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
852 KB |
Output is correct |
2 |
Correct |
1 ms |
852 KB |
Output is correct |
3 |
Correct |
1 ms |
872 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
852 KB |
Output is correct |
6 |
Correct |
1 ms |
852 KB |
Output is correct |
7 |
Correct |
80 ms |
5472 KB |
Output is correct |
8 |
Correct |
90 ms |
5384 KB |
Output is correct |
9 |
Correct |
97 ms |
6804 KB |
Output is correct |
10 |
Correct |
98 ms |
6840 KB |
Output is correct |
11 |
Correct |
208 ms |
12248 KB |
Output is correct |
12 |
Correct |
235 ms |
12232 KB |
Output is correct |
13 |
Correct |
372 ms |
22688 KB |
Output is correct |
14 |
Correct |
366 ms |
22920 KB |
Output is correct |
15 |
Correct |
386 ms |
22760 KB |
Output is correct |
16 |
Correct |
371 ms |
22836 KB |
Output is correct |
17 |
Correct |
343 ms |
22328 KB |
Output is correct |
18 |
Correct |
314 ms |
22336 KB |
Output is correct |
19 |
Correct |
312 ms |
22368 KB |
Output is correct |
20 |
Correct |
313 ms |
22352 KB |
Output is correct |
21 |
Correct |
343 ms |
22256 KB |
Output is correct |
22 |
Correct |
337 ms |
22224 KB |
Output is correct |
23 |
Correct |
339 ms |
22220 KB |
Output is correct |
24 |
Correct |
336 ms |
22196 KB |
Output is correct |
25 |
Correct |
332 ms |
22320 KB |
Output is correct |
26 |
Correct |
336 ms |
22336 KB |
Output is correct |
27 |
Correct |
331 ms |
22320 KB |
Output is correct |
28 |
Correct |
343 ms |
22340 KB |
Output is correct |
29 |
Correct |
393 ms |
22844 KB |
Output is correct |
30 |
Correct |
404 ms |
22840 KB |
Output is correct |
31 |
Correct |
407 ms |
23116 KB |
Output is correct |
32 |
Correct |
437 ms |
22804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
852 KB |
Output is correct |
2 |
Correct |
1 ms |
852 KB |
Output is correct |
3 |
Correct |
1 ms |
872 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
852 KB |
Output is correct |
6 |
Correct |
1 ms |
852 KB |
Output is correct |
7 |
Correct |
89 ms |
5256 KB |
Output is correct |
8 |
Correct |
85 ms |
5232 KB |
Output is correct |
9 |
Correct |
80 ms |
5260 KB |
Output is correct |
10 |
Correct |
103 ms |
6828 KB |
Output is correct |
11 |
Correct |
106 ms |
6732 KB |
Output is correct |
12 |
Correct |
215 ms |
12304 KB |
Output is correct |
13 |
Correct |
210 ms |
12244 KB |
Output is correct |
14 |
Correct |
80 ms |
5472 KB |
Output is correct |
15 |
Correct |
90 ms |
5384 KB |
Output is correct |
16 |
Correct |
97 ms |
6804 KB |
Output is correct |
17 |
Correct |
98 ms |
6840 KB |
Output is correct |
18 |
Correct |
208 ms |
12248 KB |
Output is correct |
19 |
Correct |
235 ms |
12232 KB |
Output is correct |
20 |
Correct |
372 ms |
22688 KB |
Output is correct |
21 |
Correct |
366 ms |
22920 KB |
Output is correct |
22 |
Correct |
386 ms |
22760 KB |
Output is correct |
23 |
Correct |
371 ms |
22836 KB |
Output is correct |
24 |
Correct |
343 ms |
22328 KB |
Output is correct |
25 |
Correct |
314 ms |
22336 KB |
Output is correct |
26 |
Correct |
312 ms |
22368 KB |
Output is correct |
27 |
Correct |
313 ms |
22352 KB |
Output is correct |
28 |
Correct |
343 ms |
22256 KB |
Output is correct |
29 |
Correct |
337 ms |
22224 KB |
Output is correct |
30 |
Correct |
339 ms |
22220 KB |
Output is correct |
31 |
Correct |
336 ms |
22196 KB |
Output is correct |
32 |
Correct |
332 ms |
22320 KB |
Output is correct |
33 |
Correct |
336 ms |
22336 KB |
Output is correct |
34 |
Correct |
331 ms |
22320 KB |
Output is correct |
35 |
Correct |
343 ms |
22340 KB |
Output is correct |
36 |
Correct |
393 ms |
22844 KB |
Output is correct |
37 |
Correct |
404 ms |
22840 KB |
Output is correct |
38 |
Correct |
407 ms |
23116 KB |
Output is correct |
39 |
Correct |
437 ms |
22804 KB |
Output is correct |
40 |
Correct |
74 ms |
5348 KB |
Output is correct |
41 |
Correct |
77 ms |
5320 KB |
Output is correct |
42 |
Correct |
100 ms |
6896 KB |
Output is correct |
43 |
Correct |
107 ms |
6792 KB |
Output is correct |
44 |
Correct |
219 ms |
12376 KB |
Output is correct |
45 |
Correct |
218 ms |
12288 KB |
Output is correct |
46 |
Correct |
368 ms |
22836 KB |
Output is correct |
47 |
Correct |
382 ms |
22748 KB |
Output is correct |
48 |
Correct |
374 ms |
22956 KB |
Output is correct |
49 |
Correct |
371 ms |
22616 KB |
Output is correct |
50 |
Correct |
296 ms |
22264 KB |
Output is correct |
51 |
Correct |
321 ms |
22164 KB |
Output is correct |
52 |
Correct |
308 ms |
22188 KB |
Output is correct |
53 |
Correct |
317 ms |
22148 KB |
Output is correct |
54 |
Correct |
329 ms |
22156 KB |
Output is correct |
55 |
Correct |
341 ms |
21568 KB |
Output is correct |
56 |
Correct |
329 ms |
21096 KB |
Output is correct |
57 |
Correct |
328 ms |
20936 KB |
Output is correct |
58 |
Correct |
330 ms |
21024 KB |
Output is correct |
59 |
Correct |
350 ms |
20900 KB |
Output is correct |
60 |
Correct |
310 ms |
20920 KB |
Output is correct |
61 |
Correct |
315 ms |
20852 KB |
Output is correct |
62 |
Correct |
416 ms |
21220 KB |
Output is correct |
63 |
Correct |
401 ms |
21168 KB |
Output is correct |
64 |
Correct |
398 ms |
21264 KB |
Output is correct |
65 |
Correct |
393 ms |
21232 KB |
Output is correct |