#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=5e4;
int k, n, m, q;
vector<ar<int,2>> g[N], rg[N];
ll dst[20][N+5][5]; int dst_mask[N+5];
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 |
52 ms |
39236 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
3 ms |
3148 KB |
Output is correct |
6 |
Correct |
3 ms |
3148 KB |
Output is correct |
7 |
Correct |
3 ms |
3148 KB |
Output is correct |
8 |
Correct |
52 ms |
39200 KB |
Output is correct |
9 |
Correct |
50 ms |
39008 KB |
Output is correct |
10 |
Correct |
22 ms |
36132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
37272 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
2 ms |
2636 KB |
Output is correct |
7 |
Correct |
5 ms |
3276 KB |
Output is correct |
8 |
Correct |
6 ms |
3276 KB |
Output is correct |
9 |
Correct |
52 ms |
39264 KB |
Output is correct |
10 |
Correct |
127 ms |
38392 KB |
Output is correct |
11 |
Correct |
91 ms |
37316 KB |
Output is correct |
12 |
Correct |
84 ms |
36340 KB |
Output is correct |
13 |
Correct |
133 ms |
24644 KB |
Output is correct |
14 |
Correct |
73 ms |
23720 KB |
Output is correct |
15 |
Correct |
80 ms |
22084 KB |
Output is correct |
16 |
Correct |
76 ms |
21960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
3 ms |
2636 KB |
Output is correct |
6 |
Correct |
3 ms |
3148 KB |
Output is correct |
7 |
Correct |
3 ms |
3148 KB |
Output is correct |
8 |
Correct |
6 ms |
3276 KB |
Output is correct |
9 |
Correct |
4 ms |
3148 KB |
Output is correct |
10 |
Correct |
48 ms |
39140 KB |
Output is correct |
11 |
Correct |
79 ms |
37188 KB |
Output is correct |
12 |
Correct |
151 ms |
38208 KB |
Output is correct |
13 |
Correct |
128 ms |
38724 KB |
Output is correct |
14 |
Correct |
104 ms |
37572 KB |
Output is correct |
15 |
Correct |
74 ms |
21832 KB |
Output is correct |
16 |
Correct |
74 ms |
21792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
3 ms |
2636 KB |
Output is correct |
6 |
Correct |
3 ms |
3148 KB |
Output is correct |
7 |
Correct |
3 ms |
3148 KB |
Output is correct |
8 |
Correct |
6 ms |
3276 KB |
Output is correct |
9 |
Correct |
4 ms |
3148 KB |
Output is correct |
10 |
Correct |
48 ms |
39140 KB |
Output is correct |
11 |
Correct |
79 ms |
37188 KB |
Output is correct |
12 |
Correct |
151 ms |
38208 KB |
Output is correct |
13 |
Correct |
128 ms |
38724 KB |
Output is correct |
14 |
Correct |
104 ms |
37572 KB |
Output is correct |
15 |
Correct |
74 ms |
21832 KB |
Output is correct |
16 |
Correct |
74 ms |
21792 KB |
Output is correct |
17 |
Correct |
79 ms |
37264 KB |
Output is correct |
18 |
Correct |
2 ms |
2636 KB |
Output is correct |
19 |
Correct |
2 ms |
2636 KB |
Output is correct |
20 |
Correct |
2 ms |
2636 KB |
Output is correct |
21 |
Correct |
2 ms |
2636 KB |
Output is correct |
22 |
Correct |
2 ms |
2636 KB |
Output is correct |
23 |
Correct |
3 ms |
3148 KB |
Output is correct |
24 |
Correct |
4 ms |
3148 KB |
Output is correct |
25 |
Correct |
7 ms |
3148 KB |
Output is correct |
26 |
Correct |
5 ms |
3148 KB |
Output is correct |
27 |
Correct |
51 ms |
39176 KB |
Output is correct |
28 |
Correct |
129 ms |
38316 KB |
Output is correct |
29 |
Correct |
130 ms |
38732 KB |
Output is correct |
30 |
Correct |
103 ms |
37572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
39236 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
3 ms |
3148 KB |
Output is correct |
6 |
Correct |
3 ms |
3148 KB |
Output is correct |
7 |
Correct |
3 ms |
3148 KB |
Output is correct |
8 |
Correct |
52 ms |
39200 KB |
Output is correct |
9 |
Correct |
50 ms |
39008 KB |
Output is correct |
10 |
Correct |
22 ms |
36132 KB |
Output is correct |
11 |
Correct |
81 ms |
37272 KB |
Output is correct |
12 |
Correct |
2 ms |
2636 KB |
Output is correct |
13 |
Correct |
2 ms |
2636 KB |
Output is correct |
14 |
Correct |
2 ms |
2636 KB |
Output is correct |
15 |
Correct |
2 ms |
2636 KB |
Output is correct |
16 |
Correct |
2 ms |
2636 KB |
Output is correct |
17 |
Correct |
5 ms |
3276 KB |
Output is correct |
18 |
Correct |
6 ms |
3276 KB |
Output is correct |
19 |
Correct |
52 ms |
39264 KB |
Output is correct |
20 |
Correct |
127 ms |
38392 KB |
Output is correct |
21 |
Correct |
91 ms |
37316 KB |
Output is correct |
22 |
Correct |
84 ms |
36340 KB |
Output is correct |
23 |
Correct |
133 ms |
24644 KB |
Output is correct |
24 |
Correct |
73 ms |
23720 KB |
Output is correct |
25 |
Correct |
80 ms |
22084 KB |
Output is correct |
26 |
Correct |
76 ms |
21960 KB |
Output is correct |
27 |
Correct |
2 ms |
2636 KB |
Output is correct |
28 |
Correct |
2 ms |
2636 KB |
Output is correct |
29 |
Correct |
2 ms |
2636 KB |
Output is correct |
30 |
Correct |
2 ms |
2636 KB |
Output is correct |
31 |
Correct |
3 ms |
2636 KB |
Output is correct |
32 |
Correct |
3 ms |
3148 KB |
Output is correct |
33 |
Correct |
3 ms |
3148 KB |
Output is correct |
34 |
Correct |
6 ms |
3276 KB |
Output is correct |
35 |
Correct |
4 ms |
3148 KB |
Output is correct |
36 |
Correct |
48 ms |
39140 KB |
Output is correct |
37 |
Correct |
79 ms |
37188 KB |
Output is correct |
38 |
Correct |
151 ms |
38208 KB |
Output is correct |
39 |
Correct |
128 ms |
38724 KB |
Output is correct |
40 |
Correct |
104 ms |
37572 KB |
Output is correct |
41 |
Correct |
74 ms |
21832 KB |
Output is correct |
42 |
Correct |
74 ms |
21792 KB |
Output is correct |
43 |
Correct |
79 ms |
37264 KB |
Output is correct |
44 |
Correct |
2 ms |
2636 KB |
Output is correct |
45 |
Correct |
2 ms |
2636 KB |
Output is correct |
46 |
Correct |
2 ms |
2636 KB |
Output is correct |
47 |
Correct |
2 ms |
2636 KB |
Output is correct |
48 |
Correct |
2 ms |
2636 KB |
Output is correct |
49 |
Correct |
3 ms |
3148 KB |
Output is correct |
50 |
Correct |
4 ms |
3148 KB |
Output is correct |
51 |
Correct |
7 ms |
3148 KB |
Output is correct |
52 |
Correct |
5 ms |
3148 KB |
Output is correct |
53 |
Correct |
51 ms |
39176 KB |
Output is correct |
54 |
Correct |
129 ms |
38316 KB |
Output is correct |
55 |
Correct |
130 ms |
38732 KB |
Output is correct |
56 |
Correct |
103 ms |
37572 KB |
Output is correct |
57 |
Correct |
188 ms |
37572 KB |
Output is correct |
58 |
Correct |
57 ms |
39236 KB |
Output is correct |
59 |
Correct |
91 ms |
37344 KB |
Output is correct |