#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pll;
#define fastio ios::sync_with_stdio(false), cin.tie(0)
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define lowbit(x) x&-x
const int maxn = 5e4 + 5;
const int INF = 1e9;
const int sqrtn = 500;
int k, n, m, o;
int len;
vector<pll> adj[maxn];
struct node{
vector<vector<int>> dist;
void init(){
dist.resize(k);
for(int i = 0; i < k; i++){
dist[i].resize(k, INF);
dist[i][i] = 0;
}
}
node(){
dist = vector<vector<int>>(k, vector<int>(k, INF));
}
node(vector<vector<int>> _dist) : dist(_dist){}
} st[maxn * 4];
node add(int m, node a, node b){
vector<vector<int>> ndist(k, vector<int>(k, INF));
for(int i = 0; i < k; i++){
for(int j = 0; j < k; j++){
for(int pos = 0; pos < k; pos++){
for(auto [x, w] : adj[m * k + pos]){
ndist[i][j] = min(ndist[i][j], a.dist[i][pos] + b.dist[x - m * k - k][j] + w);
}
}
}
}
return node(ndist);
}
void build(int v = 1, int l = 0, int r = len){
if(l == r){
st[v].init();
return;
}
int m = (l + r) / 2;
build(v * 2, l, m);
build(v * 2 + 1, m + 1, r);
// cout<<"combine : "<<l<<" "<<m<<" "<<r<<"\n";
st[v] = add(m, st[v * 2], st[v * 2 + 1]);
}
node qry(int l, int r, int v = 1, int L = 0, int R = len){
if(l <= L && r >= R) return st[v];
int m = (L + R) / 2;
if(r <= m) return qry(l, r, v * 2, L, m);
else if(l > m) return qry(l, r, v * 2 + 1, m + 1, R);
else return add(m, qry(l, r, v * 2, L, m), qry(l, r, v * 2 + 1, m + 1, R));
}
int main(void){
fastio;
cin>>k>>n>>m>>o;
len = n / k;
for(int i = 0; i < m; i++){
int a, b, c;
cin>>a>>b>>c;
adj[a].eb(b, c);
}
build();
for(int i = 0; i < o; i++){
int a, b;
cin>>a>>b;
node mat = qry(a / k, b / k);
cout<<(mat.dist[a % k][b % k] < INF ? mat.dist[a % k][b % k] : -1)<<"\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
14932 KB |
Output is correct |
2 |
Correct |
3 ms |
6236 KB |
Output is correct |
3 |
Correct |
4 ms |
6236 KB |
Output is correct |
4 |
Correct |
3 ms |
6324 KB |
Output is correct |
5 |
Correct |
6 ms |
6396 KB |
Output is correct |
6 |
Correct |
6 ms |
6744 KB |
Output is correct |
7 |
Correct |
5 ms |
6488 KB |
Output is correct |
8 |
Correct |
75 ms |
14932 KB |
Output is correct |
9 |
Correct |
63 ms |
14956 KB |
Output is correct |
10 |
Correct |
51 ms |
12636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
15624 KB |
Output is correct |
2 |
Correct |
3 ms |
6236 KB |
Output is correct |
3 |
Correct |
3 ms |
6236 KB |
Output is correct |
4 |
Correct |
3 ms |
6236 KB |
Output is correct |
5 |
Correct |
4 ms |
6236 KB |
Output is correct |
6 |
Correct |
4 ms |
6236 KB |
Output is correct |
7 |
Correct |
14 ms |
6492 KB |
Output is correct |
8 |
Correct |
20 ms |
6492 KB |
Output is correct |
9 |
Correct |
49 ms |
15032 KB |
Output is correct |
10 |
Correct |
86 ms |
16464 KB |
Output is correct |
11 |
Correct |
60 ms |
15696 KB |
Output is correct |
12 |
Correct |
75 ms |
15444 KB |
Output is correct |
13 |
Correct |
82 ms |
14420 KB |
Output is correct |
14 |
Correct |
43 ms |
12624 KB |
Output is correct |
15 |
Correct |
54 ms |
11856 KB |
Output is correct |
16 |
Correct |
55 ms |
12016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6232 KB |
Output is correct |
2 |
Correct |
3 ms |
6236 KB |
Output is correct |
3 |
Correct |
3 ms |
6236 KB |
Output is correct |
4 |
Correct |
3 ms |
6236 KB |
Output is correct |
5 |
Correct |
3 ms |
6236 KB |
Output is correct |
6 |
Correct |
4 ms |
6492 KB |
Output is correct |
7 |
Correct |
4 ms |
6492 KB |
Output is correct |
8 |
Correct |
6 ms |
6492 KB |
Output is correct |
9 |
Correct |
5 ms |
6384 KB |
Output is correct |
10 |
Correct |
36 ms |
14708 KB |
Output is correct |
11 |
Correct |
39 ms |
15440 KB |
Output is correct |
12 |
Correct |
49 ms |
16208 KB |
Output is correct |
13 |
Correct |
54 ms |
16944 KB |
Output is correct |
14 |
Correct |
44 ms |
15696 KB |
Output is correct |
15 |
Correct |
34 ms |
11856 KB |
Output is correct |
16 |
Correct |
43 ms |
11872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6232 KB |
Output is correct |
2 |
Correct |
3 ms |
6236 KB |
Output is correct |
3 |
Correct |
3 ms |
6236 KB |
Output is correct |
4 |
Correct |
3 ms |
6236 KB |
Output is correct |
5 |
Correct |
3 ms |
6236 KB |
Output is correct |
6 |
Correct |
4 ms |
6492 KB |
Output is correct |
7 |
Correct |
4 ms |
6492 KB |
Output is correct |
8 |
Correct |
6 ms |
6492 KB |
Output is correct |
9 |
Correct |
5 ms |
6384 KB |
Output is correct |
10 |
Correct |
36 ms |
14708 KB |
Output is correct |
11 |
Correct |
39 ms |
15440 KB |
Output is correct |
12 |
Correct |
49 ms |
16208 KB |
Output is correct |
13 |
Correct |
54 ms |
16944 KB |
Output is correct |
14 |
Correct |
44 ms |
15696 KB |
Output is correct |
15 |
Correct |
34 ms |
11856 KB |
Output is correct |
16 |
Correct |
43 ms |
11872 KB |
Output is correct |
17 |
Correct |
51 ms |
15616 KB |
Output is correct |
18 |
Correct |
4 ms |
6236 KB |
Output is correct |
19 |
Correct |
3 ms |
6236 KB |
Output is correct |
20 |
Correct |
4 ms |
6328 KB |
Output is correct |
21 |
Correct |
3 ms |
6236 KB |
Output is correct |
22 |
Correct |
3 ms |
6236 KB |
Output is correct |
23 |
Correct |
9 ms |
6492 KB |
Output is correct |
24 |
Correct |
12 ms |
6540 KB |
Output is correct |
25 |
Correct |
30 ms |
6492 KB |
Output is correct |
26 |
Correct |
20 ms |
6744 KB |
Output is correct |
27 |
Correct |
41 ms |
14932 KB |
Output is correct |
28 |
Correct |
69 ms |
16380 KB |
Output is correct |
29 |
Correct |
72 ms |
16884 KB |
Output is correct |
30 |
Correct |
62 ms |
15788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
14932 KB |
Output is correct |
2 |
Correct |
3 ms |
6236 KB |
Output is correct |
3 |
Correct |
4 ms |
6236 KB |
Output is correct |
4 |
Correct |
3 ms |
6324 KB |
Output is correct |
5 |
Correct |
6 ms |
6396 KB |
Output is correct |
6 |
Correct |
6 ms |
6744 KB |
Output is correct |
7 |
Correct |
5 ms |
6488 KB |
Output is correct |
8 |
Correct |
75 ms |
14932 KB |
Output is correct |
9 |
Correct |
63 ms |
14956 KB |
Output is correct |
10 |
Correct |
51 ms |
12636 KB |
Output is correct |
11 |
Correct |
61 ms |
15624 KB |
Output is correct |
12 |
Correct |
3 ms |
6236 KB |
Output is correct |
13 |
Correct |
3 ms |
6236 KB |
Output is correct |
14 |
Correct |
3 ms |
6236 KB |
Output is correct |
15 |
Correct |
4 ms |
6236 KB |
Output is correct |
16 |
Correct |
4 ms |
6236 KB |
Output is correct |
17 |
Correct |
14 ms |
6492 KB |
Output is correct |
18 |
Correct |
20 ms |
6492 KB |
Output is correct |
19 |
Correct |
49 ms |
15032 KB |
Output is correct |
20 |
Correct |
86 ms |
16464 KB |
Output is correct |
21 |
Correct |
60 ms |
15696 KB |
Output is correct |
22 |
Correct |
75 ms |
15444 KB |
Output is correct |
23 |
Correct |
82 ms |
14420 KB |
Output is correct |
24 |
Correct |
43 ms |
12624 KB |
Output is correct |
25 |
Correct |
54 ms |
11856 KB |
Output is correct |
26 |
Correct |
55 ms |
12016 KB |
Output is correct |
27 |
Correct |
3 ms |
6232 KB |
Output is correct |
28 |
Correct |
3 ms |
6236 KB |
Output is correct |
29 |
Correct |
3 ms |
6236 KB |
Output is correct |
30 |
Correct |
3 ms |
6236 KB |
Output is correct |
31 |
Correct |
3 ms |
6236 KB |
Output is correct |
32 |
Correct |
4 ms |
6492 KB |
Output is correct |
33 |
Correct |
4 ms |
6492 KB |
Output is correct |
34 |
Correct |
6 ms |
6492 KB |
Output is correct |
35 |
Correct |
5 ms |
6384 KB |
Output is correct |
36 |
Correct |
36 ms |
14708 KB |
Output is correct |
37 |
Correct |
39 ms |
15440 KB |
Output is correct |
38 |
Correct |
49 ms |
16208 KB |
Output is correct |
39 |
Correct |
54 ms |
16944 KB |
Output is correct |
40 |
Correct |
44 ms |
15696 KB |
Output is correct |
41 |
Correct |
34 ms |
11856 KB |
Output is correct |
42 |
Correct |
43 ms |
11872 KB |
Output is correct |
43 |
Correct |
51 ms |
15616 KB |
Output is correct |
44 |
Correct |
4 ms |
6236 KB |
Output is correct |
45 |
Correct |
3 ms |
6236 KB |
Output is correct |
46 |
Correct |
4 ms |
6328 KB |
Output is correct |
47 |
Correct |
3 ms |
6236 KB |
Output is correct |
48 |
Correct |
3 ms |
6236 KB |
Output is correct |
49 |
Correct |
9 ms |
6492 KB |
Output is correct |
50 |
Correct |
12 ms |
6540 KB |
Output is correct |
51 |
Correct |
30 ms |
6492 KB |
Output is correct |
52 |
Correct |
20 ms |
6744 KB |
Output is correct |
53 |
Correct |
41 ms |
14932 KB |
Output is correct |
54 |
Correct |
69 ms |
16380 KB |
Output is correct |
55 |
Correct |
72 ms |
16884 KB |
Output is correct |
56 |
Correct |
62 ms |
15788 KB |
Output is correct |
57 |
Correct |
192 ms |
18256 KB |
Output is correct |
58 |
Correct |
66 ms |
14928 KB |
Output is correct |
59 |
Correct |
82 ms |
15892 KB |
Output is correct |