#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair < int , int > pii;
const int maxn = 5e4 + 10;
const ll inf = 1e10;
#define pb push_back
#define endl '\n'
#define dokme(x) cout << x , exit(0)
#define migmig ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ms(x , y) memset(x , y , sizeof x)
#define fuck(x) for(int i = 0 ; i < 5 ; i ++)for(int j = 0 ; j < 5 ; j ++)x[i][j] = inf;
int k , n , m , q;
struct node{
ll dist[5][5];
friend node operator +(node l , node r){
node v;
fuck(v.dist);
for(int i = 0 ; i < 5 ; i ++)for(int k = 0 ; k < 5 ; k ++)for(int j = 0 ; j < 5 ; j ++)
v.dist[i][k] = min(v.dist[i][k] , l.dist[i][j] + r.dist[j][k]);
return v;
}
}seg[maxn<<2];
vector < pii > adj[maxn];
#define lc (v << 1)
#define rc (lc | 1)
#define mid ((l + r) >> 1)
void build(int v = 1 , int l = 0 , int r = (n + k - 1)/k){
if(r - l == 1){
fuck(seg[v].dist);
for(int s = 0 ; s < k ; s ++)
for(auto u : adj[l * k + s])
seg[v].dist[s][u.first%k] = u.second;
return;
}
build(lc , l , mid);
build(rc , mid , r);
seg[v] = seg[lc] + seg[rc];
}
node get(int L , int R , int v = 1 , int l = 0 , int r = (n + k - 1)/k){
if(L <= l and r <= R)
return seg[v];
if(L < mid and R <= mid)
return get(L , R , lc , l , mid);
if(L >= mid)
return get(L , R , rc , mid , r);
return get(L , R , lc , l , mid) + get(L , R , rc , mid , r);
}
int32_t main(){
migmig;
cin >> k >> n >> m >> q;
for(int i = 1 ; i <= m ; i ++){
int u , v , w;
cin >> u >> v >> w;
adj[u].pb({v , w});
}
build();
while(q --){
int u , v;
cin >> u >> v;
if(u > v or u/k == v/k){
cout << -1 << endl;
continue;
}
if(u == v){
cout << 0 << endl;
continue;
}
auto res = get(u/k , v/k);
cout << ((res.dist[u%k][v%k] == inf) ? -1 : res.dist[u%k][v%k]) << endl;
}
return(0);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
28740 KB |
Output is correct |
2 |
Correct |
1 ms |
1484 KB |
Output is correct |
3 |
Correct |
1 ms |
1484 KB |
Output is correct |
4 |
Correct |
1 ms |
1484 KB |
Output is correct |
5 |
Correct |
3 ms |
1868 KB |
Output is correct |
6 |
Correct |
3 ms |
1868 KB |
Output is correct |
7 |
Correct |
3 ms |
1868 KB |
Output is correct |
8 |
Correct |
77 ms |
28740 KB |
Output is correct |
9 |
Correct |
86 ms |
28612 KB |
Output is correct |
10 |
Correct |
55 ms |
27160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
15868 KB |
Output is correct |
2 |
Correct |
1 ms |
1484 KB |
Output is correct |
3 |
Correct |
1 ms |
1484 KB |
Output is correct |
4 |
Correct |
1 ms |
1484 KB |
Output is correct |
5 |
Correct |
1 ms |
1484 KB |
Output is correct |
6 |
Correct |
1 ms |
1484 KB |
Output is correct |
7 |
Correct |
17 ms |
1952 KB |
Output is correct |
8 |
Correct |
12 ms |
1868 KB |
Output is correct |
9 |
Correct |
63 ms |
28716 KB |
Output is correct |
10 |
Correct |
92 ms |
11584 KB |
Output is correct |
11 |
Correct |
67 ms |
15852 KB |
Output is correct |
12 |
Correct |
52 ms |
10436 KB |
Output is correct |
13 |
Correct |
64 ms |
7460 KB |
Output is correct |
14 |
Correct |
45 ms |
9540 KB |
Output is correct |
15 |
Correct |
40 ms |
6012 KB |
Output is correct |
16 |
Correct |
35 ms |
5964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1484 KB |
Output is correct |
2 |
Correct |
1 ms |
1484 KB |
Output is correct |
3 |
Correct |
1 ms |
1484 KB |
Output is correct |
4 |
Correct |
1 ms |
1484 KB |
Output is correct |
5 |
Correct |
1 ms |
1484 KB |
Output is correct |
6 |
Correct |
2 ms |
1868 KB |
Output is correct |
7 |
Correct |
2 ms |
1612 KB |
Output is correct |
8 |
Correct |
3 ms |
1612 KB |
Output is correct |
9 |
Correct |
2 ms |
1612 KB |
Output is correct |
10 |
Correct |
43 ms |
28580 KB |
Output is correct |
11 |
Correct |
50 ms |
15776 KB |
Output is correct |
12 |
Correct |
62 ms |
11460 KB |
Output is correct |
13 |
Correct |
65 ms |
11760 KB |
Output is correct |
14 |
Correct |
50 ms |
11044 KB |
Output is correct |
15 |
Correct |
31 ms |
5872 KB |
Output is correct |
16 |
Correct |
39 ms |
5900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1484 KB |
Output is correct |
2 |
Correct |
1 ms |
1484 KB |
Output is correct |
3 |
Correct |
1 ms |
1484 KB |
Output is correct |
4 |
Correct |
1 ms |
1484 KB |
Output is correct |
5 |
Correct |
1 ms |
1484 KB |
Output is correct |
6 |
Correct |
2 ms |
1868 KB |
Output is correct |
7 |
Correct |
2 ms |
1612 KB |
Output is correct |
8 |
Correct |
3 ms |
1612 KB |
Output is correct |
9 |
Correct |
2 ms |
1612 KB |
Output is correct |
10 |
Correct |
43 ms |
28580 KB |
Output is correct |
11 |
Correct |
50 ms |
15776 KB |
Output is correct |
12 |
Correct |
62 ms |
11460 KB |
Output is correct |
13 |
Correct |
65 ms |
11760 KB |
Output is correct |
14 |
Correct |
50 ms |
11044 KB |
Output is correct |
15 |
Correct |
31 ms |
5872 KB |
Output is correct |
16 |
Correct |
39 ms |
5900 KB |
Output is correct |
17 |
Correct |
57 ms |
15900 KB |
Output is correct |
18 |
Correct |
1 ms |
1484 KB |
Output is correct |
19 |
Correct |
1 ms |
1484 KB |
Output is correct |
20 |
Correct |
1 ms |
1484 KB |
Output is correct |
21 |
Correct |
1 ms |
1484 KB |
Output is correct |
22 |
Correct |
1 ms |
1484 KB |
Output is correct |
23 |
Correct |
7 ms |
1940 KB |
Output is correct |
24 |
Correct |
8 ms |
1736 KB |
Output is correct |
25 |
Correct |
6 ms |
1616 KB |
Output is correct |
26 |
Correct |
7 ms |
1612 KB |
Output is correct |
27 |
Correct |
59 ms |
28652 KB |
Output is correct |
28 |
Correct |
95 ms |
11488 KB |
Output is correct |
29 |
Correct |
85 ms |
11804 KB |
Output is correct |
30 |
Correct |
59 ms |
11116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
28740 KB |
Output is correct |
2 |
Correct |
1 ms |
1484 KB |
Output is correct |
3 |
Correct |
1 ms |
1484 KB |
Output is correct |
4 |
Correct |
1 ms |
1484 KB |
Output is correct |
5 |
Correct |
3 ms |
1868 KB |
Output is correct |
6 |
Correct |
3 ms |
1868 KB |
Output is correct |
7 |
Correct |
3 ms |
1868 KB |
Output is correct |
8 |
Correct |
77 ms |
28740 KB |
Output is correct |
9 |
Correct |
86 ms |
28612 KB |
Output is correct |
10 |
Correct |
55 ms |
27160 KB |
Output is correct |
11 |
Correct |
67 ms |
15868 KB |
Output is correct |
12 |
Correct |
1 ms |
1484 KB |
Output is correct |
13 |
Correct |
1 ms |
1484 KB |
Output is correct |
14 |
Correct |
1 ms |
1484 KB |
Output is correct |
15 |
Correct |
1 ms |
1484 KB |
Output is correct |
16 |
Correct |
1 ms |
1484 KB |
Output is correct |
17 |
Correct |
17 ms |
1952 KB |
Output is correct |
18 |
Correct |
12 ms |
1868 KB |
Output is correct |
19 |
Correct |
63 ms |
28716 KB |
Output is correct |
20 |
Correct |
92 ms |
11584 KB |
Output is correct |
21 |
Correct |
67 ms |
15852 KB |
Output is correct |
22 |
Correct |
52 ms |
10436 KB |
Output is correct |
23 |
Correct |
64 ms |
7460 KB |
Output is correct |
24 |
Correct |
45 ms |
9540 KB |
Output is correct |
25 |
Correct |
40 ms |
6012 KB |
Output is correct |
26 |
Correct |
35 ms |
5964 KB |
Output is correct |
27 |
Correct |
1 ms |
1484 KB |
Output is correct |
28 |
Correct |
1 ms |
1484 KB |
Output is correct |
29 |
Correct |
1 ms |
1484 KB |
Output is correct |
30 |
Correct |
1 ms |
1484 KB |
Output is correct |
31 |
Correct |
1 ms |
1484 KB |
Output is correct |
32 |
Correct |
2 ms |
1868 KB |
Output is correct |
33 |
Correct |
2 ms |
1612 KB |
Output is correct |
34 |
Correct |
3 ms |
1612 KB |
Output is correct |
35 |
Correct |
2 ms |
1612 KB |
Output is correct |
36 |
Correct |
43 ms |
28580 KB |
Output is correct |
37 |
Correct |
50 ms |
15776 KB |
Output is correct |
38 |
Correct |
62 ms |
11460 KB |
Output is correct |
39 |
Correct |
65 ms |
11760 KB |
Output is correct |
40 |
Correct |
50 ms |
11044 KB |
Output is correct |
41 |
Correct |
31 ms |
5872 KB |
Output is correct |
42 |
Correct |
39 ms |
5900 KB |
Output is correct |
43 |
Correct |
57 ms |
15900 KB |
Output is correct |
44 |
Correct |
1 ms |
1484 KB |
Output is correct |
45 |
Correct |
1 ms |
1484 KB |
Output is correct |
46 |
Correct |
1 ms |
1484 KB |
Output is correct |
47 |
Correct |
1 ms |
1484 KB |
Output is correct |
48 |
Correct |
1 ms |
1484 KB |
Output is correct |
49 |
Correct |
7 ms |
1940 KB |
Output is correct |
50 |
Correct |
8 ms |
1736 KB |
Output is correct |
51 |
Correct |
6 ms |
1616 KB |
Output is correct |
52 |
Correct |
7 ms |
1612 KB |
Output is correct |
53 |
Correct |
59 ms |
28652 KB |
Output is correct |
54 |
Correct |
95 ms |
11488 KB |
Output is correct |
55 |
Correct |
85 ms |
11804 KB |
Output is correct |
56 |
Correct |
59 ms |
11116 KB |
Output is correct |
57 |
Correct |
107 ms |
10696 KB |
Output is correct |
58 |
Correct |
76 ms |
28640 KB |
Output is correct |
59 |
Correct |
78 ms |
15940 KB |
Output is correct |