#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
const int maxn = 75;
const int oo = 1e9 + 7;
const ll inf = 1e18;
using pii = pair<int, int>;
using piii = pair<int, pair<int, int>>; // {distance, {vertex, road}}
int n, m, k, q, line = 0;
vector<pii> adj[maxn];
int mat[maxn][maxn];
int dis[maxn][maxn][maxn*maxn];
priority_queue<piii, vector<piii>, greater<piii>> pq;
void dijkstra(int u) {
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= line; j++)
dis[u][i][j] = oo;
}
dis[u][u][0] = 0;
pq.push({0, {u, 0}});
while (!pq.empty()) {
piii t = pq.top();
int ver = t.second.first;
int road = t.second.second;
int d = t.first;
pq.pop();
if (dis[u][ver][road] != d || road >= n)
continue;
for (auto [v, j] : adj[ver]) {
if (dis[u][v][road + 1] > dis[u][ver][road] + j) {
dis[u][v][road + 1] = dis[u][ver][road] + j;
pq.push({dis[u][v][road + 1], {v, road + 1}});
}
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++)
mat[i][j] = oo;
}
for (int i = 1; i <= m; i++) {
int u, v, c;
cin >> u >> v >> c;
mat[u][v] = min(mat[u][v], c);
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (mat[i][j] != oo) {
adj[i].push_back({j, mat[i][j]});
// adj[j].push_back({i, mat[i][j]});
line++;
}
}
}
cin >> k >> q;
for (int i = 1; i <= n; i++) {
dijkstra(i);
}
for (int i = 1; i <= q; i++) {
int a, b;
cin >> a >> b;
int res = oo;
for (int j = 0; j <= min(k, n); j++)
res = min(res, dis[a][b][j]);
if (res == oo)
cout << -1 << '\n';
else
cout << res << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
592 KB |
Output is correct |
2 |
Correct |
1 ms |
476 KB |
Output is correct |
3 |
Correct |
1 ms |
592 KB |
Output is correct |
4 |
Correct |
1 ms |
592 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
1 ms |
592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
85 ms |
29036 KB |
Output is correct |
2 |
Correct |
81 ms |
29000 KB |
Output is correct |
3 |
Correct |
87 ms |
29372 KB |
Output is correct |
4 |
Correct |
132 ms |
41032 KB |
Output is correct |
5 |
Correct |
123 ms |
41032 KB |
Output is correct |
6 |
Correct |
221 ms |
83448 KB |
Output is correct |
7 |
Correct |
226 ms |
83472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
592 KB |
Output is correct |
2 |
Correct |
1 ms |
476 KB |
Output is correct |
3 |
Correct |
1 ms |
592 KB |
Output is correct |
4 |
Correct |
1 ms |
592 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
1 ms |
592 KB |
Output is correct |
7 |
Correct |
82 ms |
29244 KB |
Output is correct |
8 |
Correct |
81 ms |
29256 KB |
Output is correct |
9 |
Correct |
127 ms |
41124 KB |
Output is correct |
10 |
Correct |
126 ms |
41040 KB |
Output is correct |
11 |
Correct |
213 ms |
83468 KB |
Output is correct |
12 |
Correct |
234 ms |
83472 KB |
Output is correct |
13 |
Correct |
344 ms |
114692 KB |
Output is correct |
14 |
Correct |
335 ms |
114696 KB |
Output is correct |
15 |
Correct |
329 ms |
114700 KB |
Output is correct |
16 |
Correct |
336 ms |
114696 KB |
Output is correct |
17 |
Correct |
273 ms |
114712 KB |
Output is correct |
18 |
Correct |
301 ms |
114648 KB |
Output is correct |
19 |
Correct |
282 ms |
114760 KB |
Output is correct |
20 |
Correct |
278 ms |
114760 KB |
Output is correct |
21 |
Correct |
296 ms |
114760 KB |
Output is correct |
22 |
Correct |
273 ms |
114768 KB |
Output is correct |
23 |
Correct |
287 ms |
114760 KB |
Output is correct |
24 |
Correct |
273 ms |
114700 KB |
Output is correct |
25 |
Correct |
311 ms |
114776 KB |
Output is correct |
26 |
Correct |
323 ms |
114816 KB |
Output is correct |
27 |
Correct |
320 ms |
114800 KB |
Output is correct |
28 |
Correct |
303 ms |
114760 KB |
Output is correct |
29 |
Correct |
338 ms |
114764 KB |
Output is correct |
30 |
Correct |
331 ms |
114764 KB |
Output is correct |
31 |
Correct |
335 ms |
114728 KB |
Output is correct |
32 |
Correct |
327 ms |
114764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
592 KB |
Output is correct |
2 |
Correct |
1 ms |
476 KB |
Output is correct |
3 |
Correct |
1 ms |
592 KB |
Output is correct |
4 |
Correct |
1 ms |
592 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
1 ms |
592 KB |
Output is correct |
7 |
Correct |
85 ms |
29036 KB |
Output is correct |
8 |
Correct |
81 ms |
29000 KB |
Output is correct |
9 |
Correct |
87 ms |
29372 KB |
Output is correct |
10 |
Correct |
132 ms |
41032 KB |
Output is correct |
11 |
Correct |
123 ms |
41032 KB |
Output is correct |
12 |
Correct |
221 ms |
83448 KB |
Output is correct |
13 |
Correct |
226 ms |
83472 KB |
Output is correct |
14 |
Correct |
82 ms |
29244 KB |
Output is correct |
15 |
Correct |
81 ms |
29256 KB |
Output is correct |
16 |
Correct |
127 ms |
41124 KB |
Output is correct |
17 |
Correct |
126 ms |
41040 KB |
Output is correct |
18 |
Correct |
213 ms |
83468 KB |
Output is correct |
19 |
Correct |
234 ms |
83472 KB |
Output is correct |
20 |
Correct |
344 ms |
114692 KB |
Output is correct |
21 |
Correct |
335 ms |
114696 KB |
Output is correct |
22 |
Correct |
329 ms |
114700 KB |
Output is correct |
23 |
Correct |
336 ms |
114696 KB |
Output is correct |
24 |
Correct |
273 ms |
114712 KB |
Output is correct |
25 |
Correct |
301 ms |
114648 KB |
Output is correct |
26 |
Correct |
282 ms |
114760 KB |
Output is correct |
27 |
Correct |
278 ms |
114760 KB |
Output is correct |
28 |
Correct |
296 ms |
114760 KB |
Output is correct |
29 |
Correct |
273 ms |
114768 KB |
Output is correct |
30 |
Correct |
287 ms |
114760 KB |
Output is correct |
31 |
Correct |
273 ms |
114700 KB |
Output is correct |
32 |
Correct |
311 ms |
114776 KB |
Output is correct |
33 |
Correct |
323 ms |
114816 KB |
Output is correct |
34 |
Correct |
320 ms |
114800 KB |
Output is correct |
35 |
Correct |
303 ms |
114760 KB |
Output is correct |
36 |
Correct |
338 ms |
114764 KB |
Output is correct |
37 |
Correct |
331 ms |
114764 KB |
Output is correct |
38 |
Correct |
335 ms |
114728 KB |
Output is correct |
39 |
Correct |
327 ms |
114764 KB |
Output is correct |
40 |
Correct |
74 ms |
29252 KB |
Output is correct |
41 |
Correct |
79 ms |
29256 KB |
Output is correct |
42 |
Correct |
134 ms |
41040 KB |
Output is correct |
43 |
Correct |
131 ms |
41032 KB |
Output is correct |
44 |
Correct |
225 ms |
83192 KB |
Output is correct |
45 |
Correct |
232 ms |
83204 KB |
Output is correct |
46 |
Correct |
317 ms |
114692 KB |
Output is correct |
47 |
Correct |
318 ms |
114828 KB |
Output is correct |
48 |
Correct |
329 ms |
114696 KB |
Output is correct |
49 |
Correct |
318 ms |
114696 KB |
Output is correct |
50 |
Correct |
278 ms |
114776 KB |
Output is correct |
51 |
Correct |
292 ms |
114760 KB |
Output is correct |
52 |
Correct |
272 ms |
114644 KB |
Output is correct |
53 |
Correct |
275 ms |
114820 KB |
Output is correct |
54 |
Correct |
276 ms |
114760 KB |
Output is correct |
55 |
Correct |
288 ms |
114760 KB |
Output is correct |
56 |
Correct |
274 ms |
114776 KB |
Output is correct |
57 |
Correct |
294 ms |
114760 KB |
Output is correct |
58 |
Correct |
335 ms |
114776 KB |
Output is correct |
59 |
Correct |
345 ms |
114772 KB |
Output is correct |
60 |
Correct |
345 ms |
114920 KB |
Output is correct |
61 |
Correct |
334 ms |
114764 KB |
Output is correct |
62 |
Correct |
332 ms |
114716 KB |
Output is correct |
63 |
Correct |
328 ms |
114764 KB |
Output is correct |
64 |
Correct |
336 ms |
114708 KB |
Output is correct |
65 |
Correct |
355 ms |
114764 KB |
Output is correct |