#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int maxn = 5e4 + 5;
int K, N, M, Q, B[maxn];
vector<ll> ans, dist(maxn);
vector<pii> g1[maxn], g2[maxn];
void D(int s, int l, int r) {
for(int i=0; i<N; i++) dist[i] = 1e18;
dist[s] = 0;
priority_queue<pll, vector<pll>, greater<pll> > pq;
pq.push({ 0, s });
while(!pq.empty()) {
auto [d, u] = pq.top(); pq.pop();
if(d != dist[u]) continue;
for(auto &[v, w] : g1[u]) {
if(B[v] < l || B[v] > r) continue;
if(dist[v] > dist[u] + w) {
dist[v] = d + w;
pq.push({ d + w, v });
}
}
}
pq.push({ 0, s });
while(!pq.empty()) {
auto [d, u] = pq.top(); pq.pop();
if(d != dist[u]) continue;
for(auto &[v, w] : g2[u]) {
if(B[v] < l || B[v] > r) continue;
if(dist[v] > dist[u] + w) {
dist[v] = d + w;
pq.push({ d + w, v });
}
}
}
}
void f(int l, int r, vector<array<int, 3> > &qus) {
if(l > r || qus.empty()) return ;
int tm = (l + r) / 2;
vector<array<int, 3> > L, R;
for(int i=tm*K; i<min(N, tm * K + K); i++) {
D(i, l, r);
for(auto &[a, b, id] : qus) if(B[a] <= tm && tm <= B[b]) ans[id] = min(ans[id], dist[a] + dist[b]);
}
for(auto i=0; i<qus.size(); i++) {
if(B[qus[i][1]] < tm) L.push_back(qus[i]);
if(B[qus[i][0]] > tm) R.push_back(qus[i]);
}
f(l, tm-1, L);
f(tm+1, r, R);
}
signed main() {
ios_base::sync_with_stdio(false);
cout.tie(0); cin.tie(0);
cin >> K >> N >> M >> Q;
for(int i=0; i<N; i++) B[i] = i / K;
ans.resize(Q, 1e18);
vector<array<int, 3> > qus;
for(int i=0; i<M; i++) {
int a, b, t;
cin >> a >> b >> t;
g1[a].push_back({ b, t });
g2[b].push_back({ a, t });
}
for(int i=0; i<Q; i++) {
int a, b; cin >> a >> b;
if(B[a] == B[b]) ans[i] = -1;
else qus.push_back({ a, b, i });
}
f(0, B[N-1], qus);
for(auto x : ans) cout << (x >= 1e18 ? -1 : x) << '\n';
return 0;
}
Compilation message
toll.cpp: In function 'void f(int, int, std::vector<std::array<int, 3> >&)':
toll.cpp:58:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for(auto i=0; i<qus.size(); i++) {
| ~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
6924 KB |
Output is correct |
2 |
Correct |
1 ms |
3164 KB |
Output is correct |
3 |
Correct |
1 ms |
3164 KB |
Output is correct |
4 |
Correct |
1 ms |
3164 KB |
Output is correct |
5 |
Correct |
1 ms |
3164 KB |
Output is correct |
6 |
Correct |
1 ms |
3164 KB |
Output is correct |
7 |
Correct |
1 ms |
3164 KB |
Output is correct |
8 |
Correct |
36 ms |
6748 KB |
Output is correct |
9 |
Correct |
22 ms |
6748 KB |
Output is correct |
10 |
Correct |
7 ms |
3724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
6924 KB |
Output is correct |
2 |
Correct |
1 ms |
3160 KB |
Output is correct |
3 |
Correct |
1 ms |
3148 KB |
Output is correct |
4 |
Correct |
1 ms |
3164 KB |
Output is correct |
5 |
Correct |
1 ms |
3164 KB |
Output is correct |
6 |
Correct |
1 ms |
3164 KB |
Output is correct |
7 |
Correct |
3 ms |
3676 KB |
Output is correct |
8 |
Correct |
4 ms |
3676 KB |
Output is correct |
9 |
Correct |
20 ms |
7772 KB |
Output is correct |
10 |
Correct |
64 ms |
10836 KB |
Output is correct |
11 |
Correct |
38 ms |
8528 KB |
Output is correct |
12 |
Correct |
23 ms |
7968 KB |
Output is correct |
13 |
Correct |
79 ms |
11088 KB |
Output is correct |
14 |
Correct |
39 ms |
8028 KB |
Output is correct |
15 |
Correct |
41 ms |
7252 KB |
Output is correct |
16 |
Correct |
45 ms |
7248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3160 KB |
Output is correct |
2 |
Correct |
1 ms |
3164 KB |
Output is correct |
3 |
Correct |
1 ms |
3164 KB |
Output is correct |
4 |
Correct |
1 ms |
3164 KB |
Output is correct |
5 |
Correct |
1 ms |
3164 KB |
Output is correct |
6 |
Correct |
1 ms |
3164 KB |
Output is correct |
7 |
Correct |
2 ms |
3160 KB |
Output is correct |
8 |
Correct |
4 ms |
3420 KB |
Output is correct |
9 |
Correct |
3 ms |
3320 KB |
Output is correct |
10 |
Correct |
14 ms |
7260 KB |
Output is correct |
11 |
Correct |
46 ms |
8020 KB |
Output is correct |
12 |
Correct |
86 ms |
10420 KB |
Output is correct |
13 |
Correct |
86 ms |
11092 KB |
Output is correct |
14 |
Correct |
75 ms |
9304 KB |
Output is correct |
15 |
Correct |
64 ms |
7004 KB |
Output is correct |
16 |
Correct |
66 ms |
7004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3160 KB |
Output is correct |
2 |
Correct |
1 ms |
3164 KB |
Output is correct |
3 |
Correct |
1 ms |
3164 KB |
Output is correct |
4 |
Correct |
1 ms |
3164 KB |
Output is correct |
5 |
Correct |
1 ms |
3164 KB |
Output is correct |
6 |
Correct |
1 ms |
3164 KB |
Output is correct |
7 |
Correct |
2 ms |
3160 KB |
Output is correct |
8 |
Correct |
4 ms |
3420 KB |
Output is correct |
9 |
Correct |
3 ms |
3320 KB |
Output is correct |
10 |
Correct |
14 ms |
7260 KB |
Output is correct |
11 |
Correct |
46 ms |
8020 KB |
Output is correct |
12 |
Correct |
86 ms |
10420 KB |
Output is correct |
13 |
Correct |
86 ms |
11092 KB |
Output is correct |
14 |
Correct |
75 ms |
9304 KB |
Output is correct |
15 |
Correct |
64 ms |
7004 KB |
Output is correct |
16 |
Correct |
66 ms |
7004 KB |
Output is correct |
17 |
Correct |
56 ms |
8020 KB |
Output is correct |
18 |
Correct |
1 ms |
3164 KB |
Output is correct |
19 |
Correct |
1 ms |
3164 KB |
Output is correct |
20 |
Correct |
1 ms |
3164 KB |
Output is correct |
21 |
Correct |
1 ms |
3164 KB |
Output is correct |
22 |
Correct |
1 ms |
3164 KB |
Output is correct |
23 |
Correct |
2 ms |
3420 KB |
Output is correct |
24 |
Correct |
2 ms |
3420 KB |
Output is correct |
25 |
Correct |
5 ms |
3416 KB |
Output is correct |
26 |
Correct |
4 ms |
3416 KB |
Output is correct |
27 |
Correct |
21 ms |
7260 KB |
Output is correct |
28 |
Correct |
114 ms |
10632 KB |
Output is correct |
29 |
Correct |
128 ms |
11344 KB |
Output is correct |
30 |
Correct |
89 ms |
9300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
6924 KB |
Output is correct |
2 |
Correct |
1 ms |
3164 KB |
Output is correct |
3 |
Correct |
1 ms |
3164 KB |
Output is correct |
4 |
Correct |
1 ms |
3164 KB |
Output is correct |
5 |
Correct |
1 ms |
3164 KB |
Output is correct |
6 |
Correct |
1 ms |
3164 KB |
Output is correct |
7 |
Correct |
1 ms |
3164 KB |
Output is correct |
8 |
Correct |
36 ms |
6748 KB |
Output is correct |
9 |
Correct |
22 ms |
6748 KB |
Output is correct |
10 |
Correct |
7 ms |
3724 KB |
Output is correct |
11 |
Correct |
36 ms |
6924 KB |
Output is correct |
12 |
Correct |
1 ms |
3160 KB |
Output is correct |
13 |
Correct |
1 ms |
3148 KB |
Output is correct |
14 |
Correct |
1 ms |
3164 KB |
Output is correct |
15 |
Correct |
1 ms |
3164 KB |
Output is correct |
16 |
Correct |
1 ms |
3164 KB |
Output is correct |
17 |
Correct |
3 ms |
3676 KB |
Output is correct |
18 |
Correct |
4 ms |
3676 KB |
Output is correct |
19 |
Correct |
20 ms |
7772 KB |
Output is correct |
20 |
Correct |
64 ms |
10836 KB |
Output is correct |
21 |
Correct |
38 ms |
8528 KB |
Output is correct |
22 |
Correct |
23 ms |
7968 KB |
Output is correct |
23 |
Correct |
79 ms |
11088 KB |
Output is correct |
24 |
Correct |
39 ms |
8028 KB |
Output is correct |
25 |
Correct |
41 ms |
7252 KB |
Output is correct |
26 |
Correct |
45 ms |
7248 KB |
Output is correct |
27 |
Correct |
1 ms |
3160 KB |
Output is correct |
28 |
Correct |
1 ms |
3164 KB |
Output is correct |
29 |
Correct |
1 ms |
3164 KB |
Output is correct |
30 |
Correct |
1 ms |
3164 KB |
Output is correct |
31 |
Correct |
1 ms |
3164 KB |
Output is correct |
32 |
Correct |
1 ms |
3164 KB |
Output is correct |
33 |
Correct |
2 ms |
3160 KB |
Output is correct |
34 |
Correct |
4 ms |
3420 KB |
Output is correct |
35 |
Correct |
3 ms |
3320 KB |
Output is correct |
36 |
Correct |
14 ms |
7260 KB |
Output is correct |
37 |
Correct |
46 ms |
8020 KB |
Output is correct |
38 |
Correct |
86 ms |
10420 KB |
Output is correct |
39 |
Correct |
86 ms |
11092 KB |
Output is correct |
40 |
Correct |
75 ms |
9304 KB |
Output is correct |
41 |
Correct |
64 ms |
7004 KB |
Output is correct |
42 |
Correct |
66 ms |
7004 KB |
Output is correct |
43 |
Correct |
56 ms |
8020 KB |
Output is correct |
44 |
Correct |
1 ms |
3164 KB |
Output is correct |
45 |
Correct |
1 ms |
3164 KB |
Output is correct |
46 |
Correct |
1 ms |
3164 KB |
Output is correct |
47 |
Correct |
1 ms |
3164 KB |
Output is correct |
48 |
Correct |
1 ms |
3164 KB |
Output is correct |
49 |
Correct |
2 ms |
3420 KB |
Output is correct |
50 |
Correct |
2 ms |
3420 KB |
Output is correct |
51 |
Correct |
5 ms |
3416 KB |
Output is correct |
52 |
Correct |
4 ms |
3416 KB |
Output is correct |
53 |
Correct |
21 ms |
7260 KB |
Output is correct |
54 |
Correct |
114 ms |
10632 KB |
Output is correct |
55 |
Correct |
128 ms |
11344 KB |
Output is correct |
56 |
Correct |
89 ms |
9300 KB |
Output is correct |
57 |
Correct |
225 ms |
12624 KB |
Output is correct |
58 |
Correct |
26 ms |
7792 KB |
Output is correct |
59 |
Correct |
77 ms |
8796 KB |
Output is correct |