#include <bits/stdc++.h>
#define int long long
#define F first
#define S second
#define T int tc; cin >> tc; while(tc--)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
const int inf = 1e18;
const int N = 1e6 + 5;
const int M = 505;
const int LOG = 32;
const int CHAR = 26;
const int mod = 1e9 + 7;
const int mod2 = 998244353;
const float pi = atan(1) * 4;
const int dx[] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy[] = {0, -1, 1, -1, 1, 0, -1, 1};
const char d[] = {'E', 'N', 'S', 'W'};
int n, m, k, q;
vector<pair<int, int>> adj[100];
priority_queue<pair<pair<int, int>, int>> pq;
int g[100][100], dist[100][100][100], ans[100][100];
void dij(int node) {
for(int i = 1; i <= n; i++)
for(int j = 0; j <= n; j++)
dist[node][i][j] = inf;
dist[node][node][0] = 0;
pq.push({{0, 0}, node});
while(!pq.empty()) {
int u = pq.top().S;
int w = -pq.top().F.F;
int y = -pq.top().F.S;
pq.pop();
for(auto x: adj[u]) {
if (dist[node][x.F][y + 1] > dist[node][u][y] + x.S) {
dist[node][x.F][y + 1] = dist[node][u][y] + x.S;
pq.push({{-dist[node][x.F][y + 1], -y - 1}, x.F});
}
}
}
}
main() {
IOS
cin >> n >> m;
for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) g[i][j] = inf;
for(int i = 1; i <= m; i++) {
int u, v, w;
cin >> u >> v >> w;
g[u][v] = min(g[u][v], w);
}
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
if (g[i][j] != inf) adj[i].push_back({j, g[i][j]});
cin >> k >> q;
k = min(k, n);
for(int i = 1; i <= n; i++) {
dij(i);
for(int j = 1; j <= n; j++) {
ans[i][j] = inf;
for(int y = 0; y <= k; y++)
ans[i][j] = min(ans[i][j], dist[i][j][y]);
if (ans[i][j] == inf) ans[i][j] = -1;
}
}
while(q--) {
int u, v;
cin >> u >> v;
cout << ans[u][v] << '\n';
}
}
Compilation message
Main.cpp: In function 'void dij(long long int)':
Main.cpp:32:13: warning: unused variable 'w' [-Wunused-variable]
32 | int w = -pq.top().F.F;
| ^
Main.cpp: At global scope:
Main.cpp:44:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
44 | main() {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
89 ms |
4588 KB |
Output is correct |
2 |
Correct |
92 ms |
4556 KB |
Output is correct |
3 |
Correct |
96 ms |
4668 KB |
Output is correct |
4 |
Correct |
170 ms |
4660 KB |
Output is correct |
5 |
Correct |
180 ms |
4740 KB |
Output is correct |
6 |
Correct |
315 ms |
4884 KB |
Output is correct |
7 |
Correct |
311 ms |
4900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
93 ms |
4752 KB |
Output is correct |
8 |
Correct |
106 ms |
4880 KB |
Output is correct |
9 |
Correct |
166 ms |
4588 KB |
Output is correct |
10 |
Correct |
161 ms |
4548 KB |
Output is correct |
11 |
Correct |
275 ms |
4952 KB |
Output is correct |
12 |
Correct |
288 ms |
4868 KB |
Output is correct |
13 |
Correct |
445 ms |
4940 KB |
Output is correct |
14 |
Correct |
436 ms |
5108 KB |
Output is correct |
15 |
Correct |
443 ms |
4972 KB |
Output is correct |
16 |
Correct |
432 ms |
5056 KB |
Output is correct |
17 |
Correct |
386 ms |
4788 KB |
Output is correct |
18 |
Correct |
373 ms |
4812 KB |
Output is correct |
19 |
Correct |
366 ms |
4848 KB |
Output is correct |
20 |
Correct |
382 ms |
4848 KB |
Output is correct |
21 |
Correct |
379 ms |
4732 KB |
Output is correct |
22 |
Correct |
389 ms |
4724 KB |
Output is correct |
23 |
Correct |
390 ms |
4716 KB |
Output is correct |
24 |
Correct |
375 ms |
4732 KB |
Output is correct |
25 |
Correct |
412 ms |
4724 KB |
Output is correct |
26 |
Correct |
408 ms |
4716 KB |
Output is correct |
27 |
Correct |
426 ms |
4744 KB |
Output is correct |
28 |
Correct |
405 ms |
4720 KB |
Output is correct |
29 |
Correct |
467 ms |
5124 KB |
Output is correct |
30 |
Correct |
468 ms |
5204 KB |
Output is correct |
31 |
Correct |
451 ms |
5132 KB |
Output is correct |
32 |
Correct |
450 ms |
5272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
89 ms |
4588 KB |
Output is correct |
8 |
Correct |
92 ms |
4556 KB |
Output is correct |
9 |
Correct |
96 ms |
4668 KB |
Output is correct |
10 |
Correct |
170 ms |
4660 KB |
Output is correct |
11 |
Correct |
180 ms |
4740 KB |
Output is correct |
12 |
Correct |
315 ms |
4884 KB |
Output is correct |
13 |
Correct |
311 ms |
4900 KB |
Output is correct |
14 |
Correct |
93 ms |
4752 KB |
Output is correct |
15 |
Correct |
106 ms |
4880 KB |
Output is correct |
16 |
Correct |
166 ms |
4588 KB |
Output is correct |
17 |
Correct |
161 ms |
4548 KB |
Output is correct |
18 |
Correct |
275 ms |
4952 KB |
Output is correct |
19 |
Correct |
288 ms |
4868 KB |
Output is correct |
20 |
Correct |
445 ms |
4940 KB |
Output is correct |
21 |
Correct |
436 ms |
5108 KB |
Output is correct |
22 |
Correct |
443 ms |
4972 KB |
Output is correct |
23 |
Correct |
432 ms |
5056 KB |
Output is correct |
24 |
Correct |
386 ms |
4788 KB |
Output is correct |
25 |
Correct |
373 ms |
4812 KB |
Output is correct |
26 |
Correct |
366 ms |
4848 KB |
Output is correct |
27 |
Correct |
382 ms |
4848 KB |
Output is correct |
28 |
Correct |
379 ms |
4732 KB |
Output is correct |
29 |
Correct |
389 ms |
4724 KB |
Output is correct |
30 |
Correct |
390 ms |
4716 KB |
Output is correct |
31 |
Correct |
375 ms |
4732 KB |
Output is correct |
32 |
Correct |
412 ms |
4724 KB |
Output is correct |
33 |
Correct |
408 ms |
4716 KB |
Output is correct |
34 |
Correct |
426 ms |
4744 KB |
Output is correct |
35 |
Correct |
405 ms |
4720 KB |
Output is correct |
36 |
Correct |
467 ms |
5124 KB |
Output is correct |
37 |
Correct |
468 ms |
5204 KB |
Output is correct |
38 |
Correct |
451 ms |
5132 KB |
Output is correct |
39 |
Correct |
450 ms |
5272 KB |
Output is correct |
40 |
Correct |
83 ms |
4556 KB |
Output is correct |
41 |
Correct |
80 ms |
4640 KB |
Output is correct |
42 |
Correct |
157 ms |
4616 KB |
Output is correct |
43 |
Correct |
157 ms |
4636 KB |
Output is correct |
44 |
Correct |
291 ms |
5040 KB |
Output is correct |
45 |
Correct |
299 ms |
5004 KB |
Output is correct |
46 |
Correct |
447 ms |
11112 KB |
Output is correct |
47 |
Correct |
454 ms |
11112 KB |
Output is correct |
48 |
Correct |
444 ms |
11076 KB |
Output is correct |
49 |
Correct |
450 ms |
11168 KB |
Output is correct |
50 |
Correct |
347 ms |
11124 KB |
Output is correct |
51 |
Correct |
341 ms |
10988 KB |
Output is correct |
52 |
Correct |
347 ms |
11024 KB |
Output is correct |
53 |
Correct |
347 ms |
11076 KB |
Output is correct |
54 |
Correct |
397 ms |
10932 KB |
Output is correct |
55 |
Correct |
393 ms |
10924 KB |
Output is correct |
56 |
Correct |
398 ms |
10984 KB |
Output is correct |
57 |
Correct |
383 ms |
10956 KB |
Output is correct |
58 |
Correct |
435 ms |
11088 KB |
Output is correct |
59 |
Correct |
447 ms |
11104 KB |
Output is correct |
60 |
Correct |
468 ms |
11248 KB |
Output is correct |
61 |
Correct |
436 ms |
11156 KB |
Output is correct |
62 |
Correct |
478 ms |
11236 KB |
Output is correct |
63 |
Correct |
480 ms |
11328 KB |
Output is correct |
64 |
Correct |
478 ms |
11224 KB |
Output is correct |
65 |
Correct |
483 ms |
11168 KB |
Output is correct |