#include <bits/stdc++.h>
using namespace std;
const int N = 50'000 + 10,
S = 400;
int k, n, m, q;
vector<pair<int, int>> ad[N];
void preSktra(int st, vector<int>& f) {
f = vector<int>(n + 1, 1'000'000'000);
using pii = pair<int, int>;
priority_queue<pii, vector<pii>, greater<>> q;
q.push({f[st] = 0, st});
while (q.size()) {
auto [d, u] = q.top(); q.pop();
if (d != f[u]) continue;
for (const auto& [v, w] : ad[u]) {
if (f[v] > d + w) q.push({f[v] = d + w, v});
}
}
}
int id[N], rvs[N], it;
vector<int> pF[S + 10];
int f[N];
int sktra(int st, int ed) {
int ret = 1'000'000'000;
using pii = pair<int, int>;
priority_queue<pii, vector<pii>, greater<>> q;
q.push({f[st] = 0, st});
vector<int> vt;
while (q.size()) {
auto [d, u] = q.top(); q.pop();
vt.push_back(u);
if (d != f[u]) continue;
if (id[u]) {
ret = min(ret, f[u] + pF[id[u]][ed]);
continue;
}
for (const auto& [v, w] : ad[u]) {
if (f[v] > d + w) q.push({f[v] = d + w, v});
}
}
ret = min(ret, f[ed]);
for (const auto& i : vt) f[i] = 1'000'000'000;
return ret >= 1'000'000'000 ? -1 : ret;
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cin >> k >> n >> m >> q;
for (int i = 1; i <= m; ++i) {
int u, v, w; cin >> u >> v >> w;
ad[u].push_back({v, w});
}
for (int i = 0; i < n; ++i) {
if ((i / k) % S == 0) preSktra(i, pF[id[i] = ++it]);
}
memset(f, 63, sizeof f);
while (q--) {
int a, b; cin >> a >> b;
cout << sktra(a, b) << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
159 ms |
29160 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1884 KB |
Output is correct |
6 |
Correct |
1 ms |
1884 KB |
Output is correct |
7 |
Correct |
3 ms |
1884 KB |
Output is correct |
8 |
Correct |
85 ms |
29020 KB |
Output is correct |
9 |
Correct |
28 ms |
28760 KB |
Output is correct |
10 |
Correct |
12 ms |
26488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
29932 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
4 ms |
1960 KB |
Output is correct |
8 |
Correct |
3 ms |
2036 KB |
Output is correct |
9 |
Correct |
27 ms |
29004 KB |
Output is correct |
10 |
Correct |
221 ms |
31308 KB |
Output is correct |
11 |
Correct |
143 ms |
30036 KB |
Output is correct |
12 |
Correct |
24 ms |
29532 KB |
Output is correct |
13 |
Correct |
153 ms |
15696 KB |
Output is correct |
14 |
Correct |
93 ms |
13648 KB |
Output is correct |
15 |
Correct |
90 ms |
13156 KB |
Output is correct |
16 |
Correct |
85 ms |
13108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1624 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1652 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1884 KB |
Output is correct |
7 |
Correct |
2 ms |
1880 KB |
Output is correct |
8 |
Correct |
11 ms |
2032 KB |
Output is correct |
9 |
Correct |
5 ms |
2136 KB |
Output is correct |
10 |
Correct |
23 ms |
28724 KB |
Output is correct |
11 |
Correct |
141 ms |
29780 KB |
Output is correct |
12 |
Correct |
226 ms |
31316 KB |
Output is correct |
13 |
Correct |
260 ms |
31824 KB |
Output is correct |
14 |
Correct |
207 ms |
30460 KB |
Output is correct |
15 |
Correct |
93 ms |
13136 KB |
Output is correct |
16 |
Correct |
93 ms |
13196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1624 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1652 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1884 KB |
Output is correct |
7 |
Correct |
2 ms |
1880 KB |
Output is correct |
8 |
Correct |
11 ms |
2032 KB |
Output is correct |
9 |
Correct |
5 ms |
2136 KB |
Output is correct |
10 |
Correct |
23 ms |
28724 KB |
Output is correct |
11 |
Correct |
141 ms |
29780 KB |
Output is correct |
12 |
Correct |
226 ms |
31316 KB |
Output is correct |
13 |
Correct |
260 ms |
31824 KB |
Output is correct |
14 |
Correct |
207 ms |
30460 KB |
Output is correct |
15 |
Correct |
93 ms |
13136 KB |
Output is correct |
16 |
Correct |
93 ms |
13196 KB |
Output is correct |
17 |
Correct |
201 ms |
29808 KB |
Output is correct |
18 |
Correct |
1 ms |
1628 KB |
Output is correct |
19 |
Correct |
1 ms |
1628 KB |
Output is correct |
20 |
Correct |
1 ms |
1628 KB |
Output is correct |
21 |
Correct |
1 ms |
1628 KB |
Output is correct |
22 |
Correct |
1 ms |
1628 KB |
Output is correct |
23 |
Correct |
10 ms |
1932 KB |
Output is correct |
24 |
Correct |
39 ms |
1956 KB |
Output is correct |
25 |
Correct |
140 ms |
2012 KB |
Output is correct |
26 |
Correct |
75 ms |
2000 KB |
Output is correct |
27 |
Correct |
41 ms |
28808 KB |
Output is correct |
28 |
Correct |
349 ms |
31432 KB |
Output is correct |
29 |
Correct |
373 ms |
31916 KB |
Output is correct |
30 |
Correct |
226 ms |
30668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
159 ms |
29160 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1884 KB |
Output is correct |
6 |
Correct |
1 ms |
1884 KB |
Output is correct |
7 |
Correct |
3 ms |
1884 KB |
Output is correct |
8 |
Correct |
85 ms |
29020 KB |
Output is correct |
9 |
Correct |
28 ms |
28760 KB |
Output is correct |
10 |
Correct |
12 ms |
26488 KB |
Output is correct |
11 |
Correct |
86 ms |
29932 KB |
Output is correct |
12 |
Correct |
1 ms |
1628 KB |
Output is correct |
13 |
Correct |
1 ms |
1628 KB |
Output is correct |
14 |
Correct |
1 ms |
1628 KB |
Output is correct |
15 |
Correct |
1 ms |
1628 KB |
Output is correct |
16 |
Correct |
1 ms |
1628 KB |
Output is correct |
17 |
Correct |
4 ms |
1960 KB |
Output is correct |
18 |
Correct |
3 ms |
2036 KB |
Output is correct |
19 |
Correct |
27 ms |
29004 KB |
Output is correct |
20 |
Correct |
221 ms |
31308 KB |
Output is correct |
21 |
Correct |
143 ms |
30036 KB |
Output is correct |
22 |
Correct |
24 ms |
29532 KB |
Output is correct |
23 |
Correct |
153 ms |
15696 KB |
Output is correct |
24 |
Correct |
93 ms |
13648 KB |
Output is correct |
25 |
Correct |
90 ms |
13156 KB |
Output is correct |
26 |
Correct |
85 ms |
13108 KB |
Output is correct |
27 |
Correct |
1 ms |
1624 KB |
Output is correct |
28 |
Correct |
1 ms |
1628 KB |
Output is correct |
29 |
Correct |
1 ms |
1652 KB |
Output is correct |
30 |
Correct |
1 ms |
1628 KB |
Output is correct |
31 |
Correct |
1 ms |
1628 KB |
Output is correct |
32 |
Correct |
1 ms |
1884 KB |
Output is correct |
33 |
Correct |
2 ms |
1880 KB |
Output is correct |
34 |
Correct |
11 ms |
2032 KB |
Output is correct |
35 |
Correct |
5 ms |
2136 KB |
Output is correct |
36 |
Correct |
23 ms |
28724 KB |
Output is correct |
37 |
Correct |
141 ms |
29780 KB |
Output is correct |
38 |
Correct |
226 ms |
31316 KB |
Output is correct |
39 |
Correct |
260 ms |
31824 KB |
Output is correct |
40 |
Correct |
207 ms |
30460 KB |
Output is correct |
41 |
Correct |
93 ms |
13136 KB |
Output is correct |
42 |
Correct |
93 ms |
13196 KB |
Output is correct |
43 |
Correct |
201 ms |
29808 KB |
Output is correct |
44 |
Correct |
1 ms |
1628 KB |
Output is correct |
45 |
Correct |
1 ms |
1628 KB |
Output is correct |
46 |
Correct |
1 ms |
1628 KB |
Output is correct |
47 |
Correct |
1 ms |
1628 KB |
Output is correct |
48 |
Correct |
1 ms |
1628 KB |
Output is correct |
49 |
Correct |
10 ms |
1932 KB |
Output is correct |
50 |
Correct |
39 ms |
1956 KB |
Output is correct |
51 |
Correct |
140 ms |
2012 KB |
Output is correct |
52 |
Correct |
75 ms |
2000 KB |
Output is correct |
53 |
Correct |
41 ms |
28808 KB |
Output is correct |
54 |
Correct |
349 ms |
31432 KB |
Output is correct |
55 |
Correct |
373 ms |
31916 KB |
Output is correct |
56 |
Correct |
226 ms |
30668 KB |
Output is correct |
57 |
Correct |
1051 ms |
33232 KB |
Output is correct |
58 |
Correct |
84 ms |
29064 KB |
Output is correct |
59 |
Correct |
345 ms |
30036 KB |
Output is correct |