#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ar array
using namespace std;
typedef long double ld;
typedef long long ll;
const int inf=2e9+9;
const ll linf=4e18+18;
const int N=5e5;
int k, n, m, q;
vector<ar<int,2>> g[N], rg[N];
ll dst[20][N][5]; int dst_mask[N];
void build_dst(int h, int l, int r) {
int m=(l+r)/2;
for (int i=m*k; i<r*k; ++i) dst_mask[i]^=(1<<h);
for (int k0=0; k0<k; ++k0) {
dst[h][m*k+k0][k0]=0;
for (int i=m*k+k0-1; i>=l*k; --i) {
dst[h][i][k0]=inf;
for (auto &[u,t] : g[i]) {
if (u>m*k+k0) continue;
dst[h][i][k0]=min(dst[h][i][k0],dst[h][u][k0]+t);
}
}
for (int i=m*k+k0+1; i<r*k; ++i) {
dst[h][i][k0]=inf;
for (auto &[u,t] : rg[i]) {
if (u<m*k+k0) continue;
dst[h][i][k0]=min(dst[h][i][k0],dst[h][u][k0]+t);
}
}
}
if (l+1<r) {
build_dst(h+1,l,m);
build_dst(h+1,m,r);
}
}
void solve() {
cin >> k >> n >> m >> q;
for (int i=0; i<m; ++i) {
int a, b, t; cin >> a >> b >> t;
g[a].push_back({b,t});
rg[b].push_back({a,t});
}
build_dst(0,0,(n+k-1)/k);
while(q--) {
int a, b; cin >> a >> b;
if (a/k==b/k) {
cout << "-1\n";
continue;
}
int h=__builtin_ctz(dst_mask[a]^dst_mask[b]);
ll ans=inf;
for (int i=0; i<k; ++i) ans=min(ans,dst[h][a][i]+dst[h][b][i]);
cout << (ans<inf?ans:-1) << "\n";
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#ifdef disastah
cout << "Output\n\n";
#endif
int tt=1;
// cin >> tt;
while (tt--) {
solve();
cout << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
60520 KB |
Output is correct |
2 |
Correct |
15 ms |
23844 KB |
Output is correct |
3 |
Correct |
15 ms |
23744 KB |
Output is correct |
4 |
Correct |
15 ms |
23840 KB |
Output is correct |
5 |
Correct |
15 ms |
24268 KB |
Output is correct |
6 |
Correct |
16 ms |
24280 KB |
Output is correct |
7 |
Correct |
16 ms |
24396 KB |
Output is correct |
8 |
Correct |
67 ms |
60540 KB |
Output is correct |
9 |
Correct |
68 ms |
60392 KB |
Output is correct |
10 |
Correct |
35 ms |
57436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
58536 KB |
Output is correct |
2 |
Correct |
14 ms |
23848 KB |
Output is correct |
3 |
Correct |
15 ms |
23820 KB |
Output is correct |
4 |
Correct |
15 ms |
23724 KB |
Output is correct |
5 |
Correct |
15 ms |
23828 KB |
Output is correct |
6 |
Correct |
15 ms |
23756 KB |
Output is correct |
7 |
Correct |
19 ms |
24396 KB |
Output is correct |
8 |
Correct |
19 ms |
24396 KB |
Output is correct |
9 |
Correct |
69 ms |
60420 KB |
Output is correct |
10 |
Correct |
162 ms |
59552 KB |
Output is correct |
11 |
Correct |
97 ms |
58564 KB |
Output is correct |
12 |
Correct |
111 ms |
57572 KB |
Output is correct |
13 |
Correct |
143 ms |
45844 KB |
Output is correct |
14 |
Correct |
85 ms |
44864 KB |
Output is correct |
15 |
Correct |
89 ms |
43060 KB |
Output is correct |
16 |
Correct |
88 ms |
42980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23756 KB |
Output is correct |
2 |
Correct |
15 ms |
23840 KB |
Output is correct |
3 |
Correct |
14 ms |
23828 KB |
Output is correct |
4 |
Correct |
14 ms |
23800 KB |
Output is correct |
5 |
Correct |
15 ms |
23736 KB |
Output is correct |
6 |
Correct |
16 ms |
24268 KB |
Output is correct |
7 |
Correct |
16 ms |
24352 KB |
Output is correct |
8 |
Correct |
17 ms |
24380 KB |
Output is correct |
9 |
Correct |
20 ms |
24268 KB |
Output is correct |
10 |
Correct |
65 ms |
60508 KB |
Output is correct |
11 |
Correct |
90 ms |
58484 KB |
Output is correct |
12 |
Correct |
144 ms |
59480 KB |
Output is correct |
13 |
Correct |
161 ms |
59972 KB |
Output is correct |
14 |
Correct |
121 ms |
58736 KB |
Output is correct |
15 |
Correct |
90 ms |
43068 KB |
Output is correct |
16 |
Correct |
92 ms |
43112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
23756 KB |
Output is correct |
2 |
Correct |
15 ms |
23840 KB |
Output is correct |
3 |
Correct |
14 ms |
23828 KB |
Output is correct |
4 |
Correct |
14 ms |
23800 KB |
Output is correct |
5 |
Correct |
15 ms |
23736 KB |
Output is correct |
6 |
Correct |
16 ms |
24268 KB |
Output is correct |
7 |
Correct |
16 ms |
24352 KB |
Output is correct |
8 |
Correct |
17 ms |
24380 KB |
Output is correct |
9 |
Correct |
20 ms |
24268 KB |
Output is correct |
10 |
Correct |
65 ms |
60508 KB |
Output is correct |
11 |
Correct |
90 ms |
58484 KB |
Output is correct |
12 |
Correct |
144 ms |
59480 KB |
Output is correct |
13 |
Correct |
161 ms |
59972 KB |
Output is correct |
14 |
Correct |
121 ms |
58736 KB |
Output is correct |
15 |
Correct |
90 ms |
43068 KB |
Output is correct |
16 |
Correct |
92 ms |
43112 KB |
Output is correct |
17 |
Correct |
92 ms |
58488 KB |
Output is correct |
18 |
Correct |
14 ms |
23756 KB |
Output is correct |
19 |
Correct |
14 ms |
23756 KB |
Output is correct |
20 |
Correct |
14 ms |
23768 KB |
Output is correct |
21 |
Correct |
15 ms |
23716 KB |
Output is correct |
22 |
Correct |
14 ms |
23828 KB |
Output is correct |
23 |
Correct |
17 ms |
24340 KB |
Output is correct |
24 |
Correct |
18 ms |
24296 KB |
Output is correct |
25 |
Correct |
18 ms |
24304 KB |
Output is correct |
26 |
Correct |
18 ms |
24288 KB |
Output is correct |
27 |
Correct |
85 ms |
60484 KB |
Output is correct |
28 |
Correct |
141 ms |
59544 KB |
Output is correct |
29 |
Correct |
160 ms |
60020 KB |
Output is correct |
30 |
Correct |
141 ms |
58796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
60520 KB |
Output is correct |
2 |
Correct |
15 ms |
23844 KB |
Output is correct |
3 |
Correct |
15 ms |
23744 KB |
Output is correct |
4 |
Correct |
15 ms |
23840 KB |
Output is correct |
5 |
Correct |
15 ms |
24268 KB |
Output is correct |
6 |
Correct |
16 ms |
24280 KB |
Output is correct |
7 |
Correct |
16 ms |
24396 KB |
Output is correct |
8 |
Correct |
67 ms |
60540 KB |
Output is correct |
9 |
Correct |
68 ms |
60392 KB |
Output is correct |
10 |
Correct |
35 ms |
57436 KB |
Output is correct |
11 |
Correct |
101 ms |
58536 KB |
Output is correct |
12 |
Correct |
14 ms |
23848 KB |
Output is correct |
13 |
Correct |
15 ms |
23820 KB |
Output is correct |
14 |
Correct |
15 ms |
23724 KB |
Output is correct |
15 |
Correct |
15 ms |
23828 KB |
Output is correct |
16 |
Correct |
15 ms |
23756 KB |
Output is correct |
17 |
Correct |
19 ms |
24396 KB |
Output is correct |
18 |
Correct |
19 ms |
24396 KB |
Output is correct |
19 |
Correct |
69 ms |
60420 KB |
Output is correct |
20 |
Correct |
162 ms |
59552 KB |
Output is correct |
21 |
Correct |
97 ms |
58564 KB |
Output is correct |
22 |
Correct |
111 ms |
57572 KB |
Output is correct |
23 |
Correct |
143 ms |
45844 KB |
Output is correct |
24 |
Correct |
85 ms |
44864 KB |
Output is correct |
25 |
Correct |
89 ms |
43060 KB |
Output is correct |
26 |
Correct |
88 ms |
42980 KB |
Output is correct |
27 |
Correct |
14 ms |
23756 KB |
Output is correct |
28 |
Correct |
15 ms |
23840 KB |
Output is correct |
29 |
Correct |
14 ms |
23828 KB |
Output is correct |
30 |
Correct |
14 ms |
23800 KB |
Output is correct |
31 |
Correct |
15 ms |
23736 KB |
Output is correct |
32 |
Correct |
16 ms |
24268 KB |
Output is correct |
33 |
Correct |
16 ms |
24352 KB |
Output is correct |
34 |
Correct |
17 ms |
24380 KB |
Output is correct |
35 |
Correct |
20 ms |
24268 KB |
Output is correct |
36 |
Correct |
65 ms |
60508 KB |
Output is correct |
37 |
Correct |
90 ms |
58484 KB |
Output is correct |
38 |
Correct |
144 ms |
59480 KB |
Output is correct |
39 |
Correct |
161 ms |
59972 KB |
Output is correct |
40 |
Correct |
121 ms |
58736 KB |
Output is correct |
41 |
Correct |
90 ms |
43068 KB |
Output is correct |
42 |
Correct |
92 ms |
43112 KB |
Output is correct |
43 |
Correct |
92 ms |
58488 KB |
Output is correct |
44 |
Correct |
14 ms |
23756 KB |
Output is correct |
45 |
Correct |
14 ms |
23756 KB |
Output is correct |
46 |
Correct |
14 ms |
23768 KB |
Output is correct |
47 |
Correct |
15 ms |
23716 KB |
Output is correct |
48 |
Correct |
14 ms |
23828 KB |
Output is correct |
49 |
Correct |
17 ms |
24340 KB |
Output is correct |
50 |
Correct |
18 ms |
24296 KB |
Output is correct |
51 |
Correct |
18 ms |
24304 KB |
Output is correct |
52 |
Correct |
18 ms |
24288 KB |
Output is correct |
53 |
Correct |
85 ms |
60484 KB |
Output is correct |
54 |
Correct |
141 ms |
59544 KB |
Output is correct |
55 |
Correct |
160 ms |
60020 KB |
Output is correct |
56 |
Correct |
141 ms |
58796 KB |
Output is correct |
57 |
Correct |
198 ms |
62248 KB |
Output is correct |
58 |
Correct |
69 ms |
61356 KB |
Output is correct |
59 |
Correct |
97 ms |
60248 KB |
Output is correct |