#include<bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e18;
const int MAX_K = 10;
int K;
struct M{
int dist[MAX_K][MAX_K];
M(){
for(int i = 0; i<K; i++){
for(int j = 0; j<K; j++){
dist[i][j] = INF;
}
}
}
void sh(){
/*for(int i = 0; i<K; i++){
for(int j=0; j<K; j++){
cout<<dist[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;*/
}
};
M e(){
M res;
for(int i= 0; i<K; i++){
for(int j = 0; j<K; j++){
if(i==j){
res.dist[i][j] = 0;
}
else{
res.dist[i][j] = INF;
}
}
}
return res;
}
M combine(M& lh, M&rh){
M res;
for(int mid= 0; mid<K; mid++){
for(int a = 0; a<K; a++){
for(int b = 0; b<K; b++){
res.dist[a][b] = min(res.dist[a][b], lh.dist[a][mid] + rh.dist[mid][b]);
}
}
}
return res;
}
struct Tree{
vector<M> tree;
void build(vector<M>& v, int lt, int rt, int t){
if(lt == rt){
tree[t] = v[lt];
}
else{
int mid = (lt+rt)/2;
build(v, lt, mid, t*2);
build(v, mid+1, rt, t*2+1);
tree[t] = combine(tree[t*2], tree[t*2+1]);
}
//cout<<"lt "<<lt<<" rt "<<rt<<endl;
tree[t].sh();
}
M get(int l, int r, int lt, int rt, int t){
if(r<lt || rt<l){
return e();
}
else if(l<=lt && rt<=r){
return tree[t];
}
else{
int mid = (lt+rt)/2;
M lh = get(l, r, lt, mid, t*2);
M rh = get(l, r, mid+1, rt, t*2+1);
return combine(lh, rh);
}
}
void sh(){
}
};
signed main(){
int n, m, o;
cin>>K>>n>>m>>o;
int l = (n+K-2)/K;
vector<M> v(l);
for(int i = 0; i<m; i++){
int a,b, t;
cin>>a>>b>>t;
//cout<<"a is "<<a<<endl;
v[a/K].dist[a%K][b%K] = t;
}
Tree tr;
tr.tree.resize(4*l +1);
tr.build(v, 0, l-1, 1);
tr.sh();
for(int i = 0; i<o; i++){
int a, b;
cin>>a>>b;
int res = tr.get(a/K, (b/K)-1, 0, l-1, 1).dist[a%K][b%K];
if(res==INF){
cout<<-1<<endl;
}
else{
cout<<res<<endl;
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
97 ms |
197004 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
4188 KB |
Output is correct |
6 |
Correct |
4 ms |
4188 KB |
Output is correct |
7 |
Correct |
5 ms |
4188 KB |
Output is correct |
8 |
Correct |
99 ms |
197076 KB |
Output is correct |
9 |
Correct |
96 ms |
196948 KB |
Output is correct |
10 |
Correct |
66 ms |
196292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
94 ms |
99920 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
22 ms |
4444 KB |
Output is correct |
8 |
Correct |
22 ms |
2544 KB |
Output is correct |
9 |
Correct |
85 ms |
196904 KB |
Output is correct |
10 |
Correct |
114 ms |
67984 KB |
Output is correct |
11 |
Correct |
103 ms |
100368 KB |
Output is correct |
12 |
Correct |
81 ms |
66980 KB |
Output is correct |
13 |
Correct |
97 ms |
26352 KB |
Output is correct |
14 |
Correct |
64 ms |
41036 KB |
Output is correct |
15 |
Correct |
58 ms |
24960 KB |
Output is correct |
16 |
Correct |
58 ms |
25012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
4188 KB |
Output is correct |
7 |
Correct |
2 ms |
2396 KB |
Output is correct |
8 |
Correct |
3 ms |
1116 KB |
Output is correct |
9 |
Correct |
3 ms |
1372 KB |
Output is correct |
10 |
Correct |
60 ms |
196948 KB |
Output is correct |
11 |
Correct |
68 ms |
99720 KB |
Output is correct |
12 |
Correct |
88 ms |
67748 KB |
Output is correct |
13 |
Correct |
95 ms |
67924 KB |
Output is correct |
14 |
Correct |
76 ms |
67492 KB |
Output is correct |
15 |
Correct |
46 ms |
24912 KB |
Output is correct |
16 |
Correct |
47 ms |
24996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
4188 KB |
Output is correct |
7 |
Correct |
2 ms |
2396 KB |
Output is correct |
8 |
Correct |
3 ms |
1116 KB |
Output is correct |
9 |
Correct |
3 ms |
1372 KB |
Output is correct |
10 |
Correct |
60 ms |
196948 KB |
Output is correct |
11 |
Correct |
68 ms |
99720 KB |
Output is correct |
12 |
Correct |
88 ms |
67748 KB |
Output is correct |
13 |
Correct |
95 ms |
67924 KB |
Output is correct |
14 |
Correct |
76 ms |
67492 KB |
Output is correct |
15 |
Correct |
46 ms |
24912 KB |
Output is correct |
16 |
Correct |
47 ms |
24996 KB |
Output is correct |
17 |
Correct |
82 ms |
100120 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
9 ms |
4436 KB |
Output is correct |
24 |
Correct |
9 ms |
2396 KB |
Output is correct |
25 |
Correct |
14 ms |
1352 KB |
Output is correct |
26 |
Correct |
12 ms |
1372 KB |
Output is correct |
27 |
Correct |
69 ms |
196880 KB |
Output is correct |
28 |
Correct |
97 ms |
67868 KB |
Output is correct |
29 |
Correct |
106 ms |
68112 KB |
Output is correct |
30 |
Correct |
85 ms |
67472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
97 ms |
197004 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
4188 KB |
Output is correct |
6 |
Correct |
4 ms |
4188 KB |
Output is correct |
7 |
Correct |
5 ms |
4188 KB |
Output is correct |
8 |
Correct |
99 ms |
197076 KB |
Output is correct |
9 |
Correct |
96 ms |
196948 KB |
Output is correct |
10 |
Correct |
66 ms |
196292 KB |
Output is correct |
11 |
Correct |
94 ms |
99920 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
22 ms |
4444 KB |
Output is correct |
18 |
Correct |
22 ms |
2544 KB |
Output is correct |
19 |
Correct |
85 ms |
196904 KB |
Output is correct |
20 |
Correct |
114 ms |
67984 KB |
Output is correct |
21 |
Correct |
103 ms |
100368 KB |
Output is correct |
22 |
Correct |
81 ms |
66980 KB |
Output is correct |
23 |
Correct |
97 ms |
26352 KB |
Output is correct |
24 |
Correct |
64 ms |
41036 KB |
Output is correct |
25 |
Correct |
58 ms |
24960 KB |
Output is correct |
26 |
Correct |
58 ms |
25012 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
2 ms |
4188 KB |
Output is correct |
33 |
Correct |
2 ms |
2396 KB |
Output is correct |
34 |
Correct |
3 ms |
1116 KB |
Output is correct |
35 |
Correct |
3 ms |
1372 KB |
Output is correct |
36 |
Correct |
60 ms |
196948 KB |
Output is correct |
37 |
Correct |
68 ms |
99720 KB |
Output is correct |
38 |
Correct |
88 ms |
67748 KB |
Output is correct |
39 |
Correct |
95 ms |
67924 KB |
Output is correct |
40 |
Correct |
76 ms |
67492 KB |
Output is correct |
41 |
Correct |
46 ms |
24912 KB |
Output is correct |
42 |
Correct |
47 ms |
24996 KB |
Output is correct |
43 |
Correct |
82 ms |
100120 KB |
Output is correct |
44 |
Correct |
0 ms |
344 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
9 ms |
4436 KB |
Output is correct |
50 |
Correct |
9 ms |
2396 KB |
Output is correct |
51 |
Correct |
14 ms |
1352 KB |
Output is correct |
52 |
Correct |
12 ms |
1372 KB |
Output is correct |
53 |
Correct |
69 ms |
196880 KB |
Output is correct |
54 |
Correct |
97 ms |
67868 KB |
Output is correct |
55 |
Correct |
106 ms |
68112 KB |
Output is correct |
56 |
Correct |
85 ms |
67472 KB |
Output is correct |
57 |
Correct |
172 ms |
52704 KB |
Output is correct |
58 |
Correct |
96 ms |
196948 KB |
Output is correct |
59 |
Correct |
119 ms |
100108 KB |
Output is correct |