#include <bits/extc++.h>
using namespace std;
typedef long long ll;
#define int ll
int k,n,m,q;
vector<vector<int>> roads[50005],rev[50005],query[200005];
int best[50005],ans[10005];
bool check[50005];
void update(int node, int l, int r, int a, int b, int ind){
int mid = (l+r)/2;
if(a/k<=mid and mid<=b/k){
query[node].push_back({a,b,ind});
}
else if(b/k<=mid){
update(node*2,l,mid,a,b,ind);
}
else{
update(node*2+1,mid+1,r,a,b,ind);
}
}
void walk(int node, int l, int r){
int mid = (l+r)/2;
if(l!=r){
walk(node*2,l,mid);
walk(node*2+1,mid+1,r);
}
int lef=l*k,rit=r*k+k-1;
for(int i=0;i<=k-1;i++){
for(int j=lef;j<=rit;j++){
best[j] = 2e9;
check[j] = false;
}
priority_queue<vector<int>,vector<vector<int>>,greater<vector<int>>> pq;
best[mid*k+i] = 0;
pq.push({0,mid*k+i});
while(pq.size()>0){
while(pq.size()>0 and check[pq.top()[1]]){
pq.pop();
}
if(pq.size()==0){
break;
}
int x = pq.top()[1];
pq.pop();
check[x] = true;
for(vector<int> j:roads[x]){
if(j[0]<=rit and best[x]+j[1]<best[j[0]]){
best[j[0]] = best[x]+j[1];
pq.push({best[j[0]],j[0]});
}
}
}
pq.push({0,mid*k+i});
check[mid*k+i] = false;
while(pq.size()>0){
while(pq.size()>0 and check[pq.top()[1]]){
pq.pop();
}
if(pq.size()==0){
break;
}
int x = pq.top()[1];
pq.pop();
check[x] = true;
for(vector<int> j:rev[x]){
if(j[0]>=lef and best[x]+j[1]<best[j[0]]){
best[j[0]] = best[x]+j[1];
pq.push({best[j[0]],j[0]});
}
}
}
for(vector<int> j:query[node]){
ans[j[2]] = min(ans[j[2]],best[j[0]]+best[j[1]]);
}
}
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> k >> n >> m >> q;
for(int i=1;i<=m;i++){
int a,b,c;
cin >> a >> b >> c;
roads[a].push_back({b,c});
rev[b].push_back({a,c});
}
for(int i=1;i<=q;i++){
int a,b;
cin >> a >> b;
update(1,0,n/k,a,b,i);
ans[i] = 2e9;
}
walk(1,0,n/k);
for(int i=1;i<=q;i++){
if(ans[i]==2e9){
cout << -1 << "\n";
}
else{
cout << ans[i] << "\n";
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
143 ms |
14796 KB |
Output is correct |
2 |
Correct |
4 ms |
7320 KB |
Output is correct |
3 |
Correct |
4 ms |
7364 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
5 ms |
7580 KB |
Output is correct |
6 |
Correct |
5 ms |
7508 KB |
Output is correct |
7 |
Correct |
6 ms |
7576 KB |
Output is correct |
8 |
Correct |
103 ms |
14708 KB |
Output is correct |
9 |
Correct |
82 ms |
14488 KB |
Output is correct |
10 |
Correct |
21 ms |
8532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
372 ms |
20388 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Correct |
5 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7376 KB |
Output is correct |
6 |
Correct |
6 ms |
7380 KB |
Output is correct |
7 |
Correct |
9 ms |
8312 KB |
Output is correct |
8 |
Correct |
11 ms |
8404 KB |
Output is correct |
9 |
Correct |
100 ms |
15656 KB |
Output is correct |
10 |
Correct |
729 ms |
28808 KB |
Output is correct |
11 |
Correct |
410 ms |
22748 KB |
Output is correct |
12 |
Correct |
205 ms |
19452 KB |
Output is correct |
13 |
Correct |
1123 ms |
34084 KB |
Output is correct |
14 |
Correct |
444 ms |
21768 KB |
Output is correct |
15 |
Correct |
509 ms |
18608 KB |
Output is correct |
16 |
Correct |
483 ms |
18676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7376 KB |
Output is correct |
4 |
Correct |
4 ms |
7304 KB |
Output is correct |
5 |
Correct |
5 ms |
7380 KB |
Output is correct |
6 |
Correct |
5 ms |
7508 KB |
Output is correct |
7 |
Correct |
8 ms |
7608 KB |
Output is correct |
8 |
Correct |
19 ms |
8148 KB |
Output is correct |
9 |
Correct |
17 ms |
7892 KB |
Output is correct |
10 |
Correct |
89 ms |
14772 KB |
Output is correct |
11 |
Correct |
414 ms |
21144 KB |
Output is correct |
12 |
Correct |
751 ms |
28120 KB |
Output is correct |
13 |
Correct |
855 ms |
31192 KB |
Output is correct |
14 |
Correct |
610 ms |
24320 KB |
Output is correct |
15 |
Correct |
536 ms |
18452 KB |
Output is correct |
16 |
Correct |
481 ms |
18576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7376 KB |
Output is correct |
4 |
Correct |
4 ms |
7304 KB |
Output is correct |
5 |
Correct |
5 ms |
7380 KB |
Output is correct |
6 |
Correct |
5 ms |
7508 KB |
Output is correct |
7 |
Correct |
8 ms |
7608 KB |
Output is correct |
8 |
Correct |
19 ms |
8148 KB |
Output is correct |
9 |
Correct |
17 ms |
7892 KB |
Output is correct |
10 |
Correct |
89 ms |
14772 KB |
Output is correct |
11 |
Correct |
414 ms |
21144 KB |
Output is correct |
12 |
Correct |
751 ms |
28120 KB |
Output is correct |
13 |
Correct |
855 ms |
31192 KB |
Output is correct |
14 |
Correct |
610 ms |
24320 KB |
Output is correct |
15 |
Correct |
536 ms |
18452 KB |
Output is correct |
16 |
Correct |
481 ms |
18576 KB |
Output is correct |
17 |
Correct |
428 ms |
21488 KB |
Output is correct |
18 |
Correct |
4 ms |
7376 KB |
Output is correct |
19 |
Correct |
4 ms |
7352 KB |
Output is correct |
20 |
Correct |
5 ms |
7380 KB |
Output is correct |
21 |
Correct |
4 ms |
7380 KB |
Output is correct |
22 |
Correct |
4 ms |
7380 KB |
Output is correct |
23 |
Correct |
6 ms |
7764 KB |
Output is correct |
24 |
Correct |
10 ms |
7892 KB |
Output is correct |
25 |
Correct |
19 ms |
8276 KB |
Output is correct |
26 |
Correct |
15 ms |
8020 KB |
Output is correct |
27 |
Correct |
103 ms |
15004 KB |
Output is correct |
28 |
Correct |
725 ms |
28504 KB |
Output is correct |
29 |
Correct |
842 ms |
31340 KB |
Output is correct |
30 |
Correct |
662 ms |
24644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
143 ms |
14796 KB |
Output is correct |
2 |
Correct |
4 ms |
7320 KB |
Output is correct |
3 |
Correct |
4 ms |
7364 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
5 ms |
7580 KB |
Output is correct |
6 |
Correct |
5 ms |
7508 KB |
Output is correct |
7 |
Correct |
6 ms |
7576 KB |
Output is correct |
8 |
Correct |
103 ms |
14708 KB |
Output is correct |
9 |
Correct |
82 ms |
14488 KB |
Output is correct |
10 |
Correct |
21 ms |
8532 KB |
Output is correct |
11 |
Correct |
372 ms |
20388 KB |
Output is correct |
12 |
Correct |
4 ms |
7252 KB |
Output is correct |
13 |
Correct |
5 ms |
7380 KB |
Output is correct |
14 |
Correct |
4 ms |
7380 KB |
Output is correct |
15 |
Correct |
4 ms |
7376 KB |
Output is correct |
16 |
Correct |
6 ms |
7380 KB |
Output is correct |
17 |
Correct |
9 ms |
8312 KB |
Output is correct |
18 |
Correct |
11 ms |
8404 KB |
Output is correct |
19 |
Correct |
100 ms |
15656 KB |
Output is correct |
20 |
Correct |
729 ms |
28808 KB |
Output is correct |
21 |
Correct |
410 ms |
22748 KB |
Output is correct |
22 |
Correct |
205 ms |
19452 KB |
Output is correct |
23 |
Correct |
1123 ms |
34084 KB |
Output is correct |
24 |
Correct |
444 ms |
21768 KB |
Output is correct |
25 |
Correct |
509 ms |
18608 KB |
Output is correct |
26 |
Correct |
483 ms |
18676 KB |
Output is correct |
27 |
Correct |
5 ms |
7380 KB |
Output is correct |
28 |
Correct |
4 ms |
7380 KB |
Output is correct |
29 |
Correct |
4 ms |
7376 KB |
Output is correct |
30 |
Correct |
4 ms |
7304 KB |
Output is correct |
31 |
Correct |
5 ms |
7380 KB |
Output is correct |
32 |
Correct |
5 ms |
7508 KB |
Output is correct |
33 |
Correct |
8 ms |
7608 KB |
Output is correct |
34 |
Correct |
19 ms |
8148 KB |
Output is correct |
35 |
Correct |
17 ms |
7892 KB |
Output is correct |
36 |
Correct |
89 ms |
14772 KB |
Output is correct |
37 |
Correct |
414 ms |
21144 KB |
Output is correct |
38 |
Correct |
751 ms |
28120 KB |
Output is correct |
39 |
Correct |
855 ms |
31192 KB |
Output is correct |
40 |
Correct |
610 ms |
24320 KB |
Output is correct |
41 |
Correct |
536 ms |
18452 KB |
Output is correct |
42 |
Correct |
481 ms |
18576 KB |
Output is correct |
43 |
Correct |
428 ms |
21488 KB |
Output is correct |
44 |
Correct |
4 ms |
7376 KB |
Output is correct |
45 |
Correct |
4 ms |
7352 KB |
Output is correct |
46 |
Correct |
5 ms |
7380 KB |
Output is correct |
47 |
Correct |
4 ms |
7380 KB |
Output is correct |
48 |
Correct |
4 ms |
7380 KB |
Output is correct |
49 |
Correct |
6 ms |
7764 KB |
Output is correct |
50 |
Correct |
10 ms |
7892 KB |
Output is correct |
51 |
Correct |
19 ms |
8276 KB |
Output is correct |
52 |
Correct |
15 ms |
8020 KB |
Output is correct |
53 |
Correct |
103 ms |
15004 KB |
Output is correct |
54 |
Correct |
725 ms |
28504 KB |
Output is correct |
55 |
Correct |
842 ms |
31340 KB |
Output is correct |
56 |
Correct |
662 ms |
24644 KB |
Output is correct |
57 |
Correct |
1367 ms |
35212 KB |
Output is correct |
58 |
Correct |
94 ms |
15784 KB |
Output is correct |
59 |
Correct |
407 ms |
22948 KB |
Output is correct |