#include<bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i=a; i<=b; i++)
#define trav(x, a) for(auto &x:a)
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define nl '\n'
#define pb push_back
typedef vector<int> vi;
typedef pair<int, int> pii;
int n, m, k, q;
vector<pii> adj[50006];
vector<pii> radj[50006];
int lx[10006];
int rx[10006];
int ans[10006];
map<pii, int> edge;
vector<vi> calc(int l, int r, vi &qu){
vector<vi> vec(k*(r-l+1), vi(2*k));
if(r-l==1){
rep(i, 0, k-1){
rep(j, 0, k-1){
int t=edge[{l*k+i, r*k+j}];
vec[i][k+j]=vec[k+j][i]=(t?t:1e9);
}
}
trav(i, qu){
if(edge[{lx[i], rx[i]}]) ans[i]=edge[{lx[i], rx[i]}];
else ans[i]=1e9;
}
return vec;
}
int m=(l+r)/2;
vi lq, rq;
trav(i, qu){
if(rx[i]/k<m) lq.pb(i);
if(lx[i]/k>m) rq.pb(i);
}
vector<vi> lvec=calc(l, m, lq);
vector<vi> rvec=calc(m, r, rq);
trav(i, qu){
if(rx[i]/k<m || lx[i]/k>m) continue;
if(rx[i]/k==m){
ans[i]=lvec[lx[i]-l*k][k+rx[i]%k]; continue;
}
if(lx[i]/k==m){
ans[i]=rvec[rx[i]-m*k][lx[i]%k]; continue;
}
ans[i]=1e9;
rep(j, 0, k-1){
ans[i]=min(ans[i], lvec[lx[i]-l*k][k+j]+rvec[rx[i]-m*k][j]);
}
}
rep(i, l*k, m*k+k-1){
rep(j, 0, k-1){
vec[i-l*k][j]=lvec[i-l*k][j];
}
}
rep(i, m*k, r*k+k-1){
rep(j, k, 2*k-1){
vec[i-l*k][j]=rvec[i-m*k][j];
}
}
rep(i, (m+1)*k, r*k+k-1){
rep(j, 0, k-1){
vec[i-l*k][j]=1e9;
trav(p, radj[i]) vec[i-l*k][j]=min(vec[i-l*k][j], vec[p.f-l*k][j]+p.s);
}
}
for(int i=m*k-1; i>=l*k; i--){
rep(j, 0, k-1){
vec[i-l*k][j+k]=1e9;
trav(p, adj[i]) vec[i-l*k][j+k]=min(vec[i-l*k][j+k], vec[p.f-l*k][j+k]+p.s);
}
}
return vec;
}
int main(){
cin.tie(0)->sync_with_stdio(0);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
cin >> k >> n >> m >> q;
rep(i, 1, m){
int a, b, t; cin >> a >> b >> t;
adj[a].pb({b, t});
radj[b].pb({a, t});
edge[{a, b}]=edge[{b, a}]=t;
}
vi qu;
rep(i, 1, q){
cin >> lx[i] >> rx[i];
qu.pb(i);
}
calc(0, (n-1)/k, qu);
rep(i, 1, q) cout << (ans[i]==1e9?-1:(lx[i]/k==rx[i]/k?-1:ans[i])) << nl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
185 ms |
20488 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 |
4 ms |
3020 KB |
Output is correct |
6 |
Correct |
5 ms |
3000 KB |
Output is correct |
7 |
Correct |
5 ms |
3020 KB |
Output is correct |
8 |
Correct |
180 ms |
20488 KB |
Output is correct |
9 |
Correct |
189 ms |
20120 KB |
Output is correct |
10 |
Correct |
100 ms |
14216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
342 ms |
26324 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 |
8 ms |
3196 KB |
Output is correct |
8 |
Correct |
9 ms |
3404 KB |
Output is correct |
9 |
Correct |
189 ms |
21252 KB |
Output is correct |
10 |
Correct |
505 ms |
35496 KB |
Output is correct |
11 |
Correct |
344 ms |
28388 KB |
Output is correct |
12 |
Correct |
317 ms |
29468 KB |
Output is correct |
13 |
Correct |
575 ms |
35436 KB |
Output is correct |
14 |
Correct |
309 ms |
23400 KB |
Output is correct |
15 |
Correct |
304 ms |
27048 KB |
Output is correct |
16 |
Correct |
314 ms |
27028 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 |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
4 ms |
3020 KB |
Output is correct |
7 |
Correct |
5 ms |
3148 KB |
Output is correct |
8 |
Correct |
11 ms |
3628 KB |
Output is correct |
9 |
Correct |
8 ms |
3532 KB |
Output is correct |
10 |
Correct |
186 ms |
21060 KB |
Output is correct |
11 |
Correct |
329 ms |
27732 KB |
Output is correct |
12 |
Correct |
489 ms |
35328 KB |
Output is correct |
13 |
Correct |
552 ms |
36956 KB |
Output is correct |
14 |
Correct |
419 ms |
32820 KB |
Output is correct |
15 |
Correct |
305 ms |
26888 KB |
Output is correct |
16 |
Correct |
307 ms |
26944 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 |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
4 ms |
3020 KB |
Output is correct |
7 |
Correct |
5 ms |
3148 KB |
Output is correct |
8 |
Correct |
11 ms |
3628 KB |
Output is correct |
9 |
Correct |
8 ms |
3532 KB |
Output is correct |
10 |
Correct |
186 ms |
21060 KB |
Output is correct |
11 |
Correct |
329 ms |
27732 KB |
Output is correct |
12 |
Correct |
489 ms |
35328 KB |
Output is correct |
13 |
Correct |
552 ms |
36956 KB |
Output is correct |
14 |
Correct |
419 ms |
32820 KB |
Output is correct |
15 |
Correct |
305 ms |
26888 KB |
Output is correct |
16 |
Correct |
307 ms |
26944 KB |
Output is correct |
17 |
Correct |
320 ms |
27900 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 |
2676 KB |
Output is correct |
21 |
Correct |
2 ms |
2636 KB |
Output is correct |
22 |
Correct |
2 ms |
2636 KB |
Output is correct |
23 |
Correct |
5 ms |
3148 KB |
Output is correct |
24 |
Correct |
6 ms |
3196 KB |
Output is correct |
25 |
Correct |
10 ms |
3660 KB |
Output is correct |
26 |
Correct |
9 ms |
3660 KB |
Output is correct |
27 |
Correct |
176 ms |
21160 KB |
Output is correct |
28 |
Correct |
495 ms |
35236 KB |
Output is correct |
29 |
Correct |
545 ms |
37084 KB |
Output is correct |
30 |
Correct |
417 ms |
32904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
185 ms |
20488 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 |
4 ms |
3020 KB |
Output is correct |
6 |
Correct |
5 ms |
3000 KB |
Output is correct |
7 |
Correct |
5 ms |
3020 KB |
Output is correct |
8 |
Correct |
180 ms |
20488 KB |
Output is correct |
9 |
Correct |
189 ms |
20120 KB |
Output is correct |
10 |
Correct |
100 ms |
14216 KB |
Output is correct |
11 |
Correct |
342 ms |
26324 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 |
8 ms |
3196 KB |
Output is correct |
18 |
Correct |
9 ms |
3404 KB |
Output is correct |
19 |
Correct |
189 ms |
21252 KB |
Output is correct |
20 |
Correct |
505 ms |
35496 KB |
Output is correct |
21 |
Correct |
344 ms |
28388 KB |
Output is correct |
22 |
Correct |
317 ms |
29468 KB |
Output is correct |
23 |
Correct |
575 ms |
35436 KB |
Output is correct |
24 |
Correct |
309 ms |
23400 KB |
Output is correct |
25 |
Correct |
304 ms |
27048 KB |
Output is correct |
26 |
Correct |
314 ms |
27028 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 |
2 ms |
2636 KB |
Output is correct |
32 |
Correct |
4 ms |
3020 KB |
Output is correct |
33 |
Correct |
5 ms |
3148 KB |
Output is correct |
34 |
Correct |
11 ms |
3628 KB |
Output is correct |
35 |
Correct |
8 ms |
3532 KB |
Output is correct |
36 |
Correct |
186 ms |
21060 KB |
Output is correct |
37 |
Correct |
329 ms |
27732 KB |
Output is correct |
38 |
Correct |
489 ms |
35328 KB |
Output is correct |
39 |
Correct |
552 ms |
36956 KB |
Output is correct |
40 |
Correct |
419 ms |
32820 KB |
Output is correct |
41 |
Correct |
305 ms |
26888 KB |
Output is correct |
42 |
Correct |
307 ms |
26944 KB |
Output is correct |
43 |
Correct |
320 ms |
27900 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 |
2676 KB |
Output is correct |
47 |
Correct |
2 ms |
2636 KB |
Output is correct |
48 |
Correct |
2 ms |
2636 KB |
Output is correct |
49 |
Correct |
5 ms |
3148 KB |
Output is correct |
50 |
Correct |
6 ms |
3196 KB |
Output is correct |
51 |
Correct |
10 ms |
3660 KB |
Output is correct |
52 |
Correct |
9 ms |
3660 KB |
Output is correct |
53 |
Correct |
176 ms |
21160 KB |
Output is correct |
54 |
Correct |
495 ms |
35236 KB |
Output is correct |
55 |
Correct |
545 ms |
37084 KB |
Output is correct |
56 |
Correct |
417 ms |
32904 KB |
Output is correct |
57 |
Correct |
723 ms |
47112 KB |
Output is correct |
58 |
Correct |
187 ms |
21380 KB |
Output is correct |
59 |
Correct |
335 ms |
28412 KB |
Output is correct |