#include <bits/stdc++.h>
using namespace std;
vector<vector<pair<int,int>>> adjlist;
int k,n,num;
int INF = 1e9;
pair<int,int> toCoord(int x){
return {x/k,x%k};
}
vector<vector<int>> combine(vector<vector<int>> a, vector<vector<int>> b, int m){
//cout<<m<<endl;
//x - index of rightmost connector
vector<vector<int>> ans(k,vector<int>(k,INF));
for (int i = 0; i<k; i++){
for (int j = 0; j<k; j++){
//get from a[i][_] to b[_][j]
int mindist = INF;
for (int x = 0; x<k; x++){
if (a[i][x]==INF){continue;}
for (auto v : adjlist[m*k+x]){
//cout<<v.first<<" "<<v.second<<endl;
int node = (v.first)%k;
int dist = v.second;
//cout<<node<<endl;
if (b[node][j]==INF){continue;}
mindist=min(mindist,a[i][x]+b[node][j]+dist);
}
}
ans[i][j]=mindist;
}
}
return ans;
}
struct Node{
int s,e,m;
vector<vector<int>> v;
Node *l, *r;
Node(int _s, int _e){
s=_s;e=_e;
if (s==e){
v.resize(k,vector<int>(k,INF));
for (int i = 0; i<k; i++){
v[i][i]=0;
}
return;
}
m = (s+e)/2;
l = new Node(s,m);
r = new Node(m+1,e);
v = combine(l->v,r->v,m);
}
vector<vector<int>> query(int a, int b){
if (a==s&&b==e){
return v;
}
if (b<=m){
return l->query(a,b);
} else if (m<a){
return r->query(a,b);
} else {
return combine(l->query(a,m),r->query(m+1,b),m);
}
}
};
int main(){
//cin.tie(0);
//ios_base::sync_with_stdio(0);
int m,o;
cin>>k>>n>>m>>o;
adjlist.resize(n);
for (int i = 0; i<m; i++){
int a,b,t;
cin>>a>>b>>t;
adjlist[a].push_back({b,t});
}
num=(n+k-1)/k;
Node *root = new Node(0,num-1);
for (int i = 0; i<o; i++){
int a,b;
cin>>a>>b;
if (a==b){
cout<<0<<'\n';
continue;
}
auto ai = toCoord(a);
auto bi = toCoord(b);
if (ai.first>=bi.first){
cout<<-1<<'\n';
continue;
}
auto q = root->query(ai.first,bi.first);
int ans = q[ai.second][bi.second];
if (ans==INF){
cout<<-1<<'\n';
} else {
cout<<ans<<'\n';
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
169 ms |
15736 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
10 ms |
640 KB |
Output is correct |
6 |
Correct |
11 ms |
640 KB |
Output is correct |
7 |
Correct |
11 ms |
640 KB |
Output is correct |
8 |
Correct |
172 ms |
16620 KB |
Output is correct |
9 |
Correct |
166 ms |
16504 KB |
Output is correct |
10 |
Correct |
96 ms |
14216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
208 ms |
12536 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
256 KB |
Output is correct |
7 |
Correct |
42 ms |
768 KB |
Output is correct |
8 |
Correct |
48 ms |
760 KB |
Output is correct |
9 |
Correct |
152 ms |
16616 KB |
Output is correct |
10 |
Correct |
308 ms |
13956 KB |
Output is correct |
11 |
Correct |
217 ms |
14200 KB |
Output is correct |
12 |
Correct |
192 ms |
12152 KB |
Output is correct |
13 |
Correct |
318 ms |
10080 KB |
Output is correct |
14 |
Correct |
162 ms |
8824 KB |
Output is correct |
15 |
Correct |
177 ms |
7672 KB |
Output is correct |
16 |
Correct |
176 ms |
7672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
256 KB |
Output is correct |
6 |
Correct |
6 ms |
640 KB |
Output is correct |
7 |
Correct |
8 ms |
640 KB |
Output is correct |
8 |
Correct |
13 ms |
640 KB |
Output is correct |
9 |
Correct |
10 ms |
640 KB |
Output is correct |
10 |
Correct |
100 ms |
16376 KB |
Output is correct |
11 |
Correct |
155 ms |
13976 KB |
Output is correct |
12 |
Correct |
202 ms |
13700 KB |
Output is correct |
13 |
Correct |
227 ms |
14200 KB |
Output is correct |
14 |
Correct |
176 ms |
13048 KB |
Output is correct |
15 |
Correct |
125 ms |
7544 KB |
Output is correct |
16 |
Correct |
122 ms |
7544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
256 KB |
Output is correct |
6 |
Correct |
6 ms |
640 KB |
Output is correct |
7 |
Correct |
8 ms |
640 KB |
Output is correct |
8 |
Correct |
13 ms |
640 KB |
Output is correct |
9 |
Correct |
10 ms |
640 KB |
Output is correct |
10 |
Correct |
100 ms |
16376 KB |
Output is correct |
11 |
Correct |
155 ms |
13976 KB |
Output is correct |
12 |
Correct |
202 ms |
13700 KB |
Output is correct |
13 |
Correct |
227 ms |
14200 KB |
Output is correct |
14 |
Correct |
176 ms |
13048 KB |
Output is correct |
15 |
Correct |
125 ms |
7544 KB |
Output is correct |
16 |
Correct |
122 ms |
7544 KB |
Output is correct |
17 |
Correct |
173 ms |
14072 KB |
Output is correct |
18 |
Correct |
5 ms |
256 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
256 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
20 ms |
640 KB |
Output is correct |
24 |
Correct |
22 ms |
640 KB |
Output is correct |
25 |
Correct |
72 ms |
640 KB |
Output is correct |
26 |
Correct |
44 ms |
640 KB |
Output is correct |
27 |
Correct |
120 ms |
16504 KB |
Output is correct |
28 |
Correct |
248 ms |
13816 KB |
Output is correct |
29 |
Correct |
285 ms |
14328 KB |
Output is correct |
30 |
Correct |
211 ms |
13048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
169 ms |
15736 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
10 ms |
640 KB |
Output is correct |
6 |
Correct |
11 ms |
640 KB |
Output is correct |
7 |
Correct |
11 ms |
640 KB |
Output is correct |
8 |
Correct |
172 ms |
16620 KB |
Output is correct |
9 |
Correct |
166 ms |
16504 KB |
Output is correct |
10 |
Correct |
96 ms |
14216 KB |
Output is correct |
11 |
Correct |
208 ms |
12536 KB |
Output is correct |
12 |
Correct |
5 ms |
256 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
256 KB |
Output is correct |
17 |
Correct |
42 ms |
768 KB |
Output is correct |
18 |
Correct |
48 ms |
760 KB |
Output is correct |
19 |
Correct |
152 ms |
16616 KB |
Output is correct |
20 |
Correct |
308 ms |
13956 KB |
Output is correct |
21 |
Correct |
217 ms |
14200 KB |
Output is correct |
22 |
Correct |
192 ms |
12152 KB |
Output is correct |
23 |
Correct |
318 ms |
10080 KB |
Output is correct |
24 |
Correct |
162 ms |
8824 KB |
Output is correct |
25 |
Correct |
177 ms |
7672 KB |
Output is correct |
26 |
Correct |
176 ms |
7672 KB |
Output is correct |
27 |
Correct |
5 ms |
256 KB |
Output is correct |
28 |
Correct |
5 ms |
256 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
4 ms |
256 KB |
Output is correct |
32 |
Correct |
6 ms |
640 KB |
Output is correct |
33 |
Correct |
8 ms |
640 KB |
Output is correct |
34 |
Correct |
13 ms |
640 KB |
Output is correct |
35 |
Correct |
10 ms |
640 KB |
Output is correct |
36 |
Correct |
100 ms |
16376 KB |
Output is correct |
37 |
Correct |
155 ms |
13976 KB |
Output is correct |
38 |
Correct |
202 ms |
13700 KB |
Output is correct |
39 |
Correct |
227 ms |
14200 KB |
Output is correct |
40 |
Correct |
176 ms |
13048 KB |
Output is correct |
41 |
Correct |
125 ms |
7544 KB |
Output is correct |
42 |
Correct |
122 ms |
7544 KB |
Output is correct |
43 |
Correct |
173 ms |
14072 KB |
Output is correct |
44 |
Correct |
5 ms |
256 KB |
Output is correct |
45 |
Correct |
5 ms |
384 KB |
Output is correct |
46 |
Correct |
5 ms |
384 KB |
Output is correct |
47 |
Correct |
5 ms |
256 KB |
Output is correct |
48 |
Correct |
5 ms |
384 KB |
Output is correct |
49 |
Correct |
20 ms |
640 KB |
Output is correct |
50 |
Correct |
22 ms |
640 KB |
Output is correct |
51 |
Correct |
72 ms |
640 KB |
Output is correct |
52 |
Correct |
44 ms |
640 KB |
Output is correct |
53 |
Correct |
120 ms |
16504 KB |
Output is correct |
54 |
Correct |
248 ms |
13816 KB |
Output is correct |
55 |
Correct |
285 ms |
14328 KB |
Output is correct |
56 |
Correct |
211 ms |
13048 KB |
Output is correct |
57 |
Correct |
602 ms |
14992 KB |
Output is correct |
58 |
Correct |
167 ms |
16632 KB |
Output is correct |
59 |
Correct |
237 ms |
14328 KB |
Output is correct |