#include <bits/stdc++.h>
#define MAXN 50001
#define INF 100000000000000
#define ii pair<int, int>
#define iii pair<ii, int>
#define ll long long
using namespace std;
ll dist[MAXN][5][5];
ll ans[MAXN];
int K, N, M, O;
ll weights[5][5][MAXN];
void dnc(int l, int r, vector<iii> q){
if( l == r ){
for( auto x : q ) ans[x.second] = -1;
return;
}
int m = (l+r)/2;
for( int i{0} ; i < K ; ++i ) for( int j{0} ; j < K ; ++j ){
if( i == j ) dist[m][i][j] = 0;//, dist[m+1][i][j] = 0;
else dist[m][i][j] = INF;//, dist[m+1][i][j] = INF;
}
for( int lev{m-1} ; lev >= l ; --lev ){
for( int i{0} ; i < K ; ++i ) for( int k{0} ; k < K ; ++k ){
dist[lev][i][k] = INF;
for( int j{0} ; j < K ; ++j ){
dist[lev][i][k] = min(dist[lev+1][j][k]+weights[i][j][lev], dist[lev][i][k]);
}
}
}
for( int lev{m+1} ; lev <= r ; ++lev ){
for( int i{0} ; i < K ; ++i ) for( int k{0} ; k < K ; ++k ){
dist[lev][i][k] = INF;
for( int j{0} ; j < K ; ++j ){
dist[lev][i][k] = min(dist[lev-1][i][j]+weights[j][k][lev-1], dist[lev][i][k]);
}
}
}
vector<iii> lft, rght;
for( auto x : q ){
int a = x.first.first, b = x.first.second;
int id = x.second;
int l1 = a/K, l2 = b/K;
a %= K; b %= K;
if( l1 <= m && l2 >= m ){
ans[id] = INF;
for( int c{0} ; c < K ; ++c ) ans[id] = min(ans[id], dist[l1][a][c]+dist[l2][c][b]);
}
else if( l2 <= m ) lft.push_back(x);
else rght.push_back(x);
}
dnc(l, m, lft);
dnc(m+1, r, rght);
}
int main(){
cin>>K>>N>>M>>O;
for( int i{0} ; i < K ; ++i ) for( int j{0} ; j < K ; ++j ) for( int k{0} ; k < (N-1)/K ; ++k ) weights[i][j][k] = INF;
for( int i{1} ; i <= M ; ++i ){ //multi edges??
int u, v, w;
cin>>u>>v>>w;
int lev = u/K;
u %= K; v %= K;
weights[u][v][lev] = w;
}
vector<iii> queries;
for( int i{1} ; i <= O ; ++i ){
int u, v;
cin>>u>>v;
queries.push_back(iii(ii(u, v), i));
}
dnc(0, (N-1)/K, queries);
for( int i{1} ; i <= O ; ++i ){
if( ans[i] < INF ) cout<<ans[i]<<'\n';
else cout<<"-1\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
11844 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
464 KB |
Output is correct |
7 |
Correct |
1 ms |
580 KB |
Output is correct |
8 |
Correct |
42 ms |
11888 KB |
Output is correct |
9 |
Correct |
39 ms |
11760 KB |
Output is correct |
10 |
Correct |
12 ms |
11012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
8192 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
5 ms |
1172 KB |
Output is correct |
8 |
Correct |
5 ms |
1172 KB |
Output is correct |
9 |
Correct |
38 ms |
11884 KB |
Output is correct |
10 |
Correct |
86 ms |
7584 KB |
Output is correct |
11 |
Correct |
66 ms |
8324 KB |
Output is correct |
12 |
Correct |
53 ms |
6668 KB |
Output is correct |
13 |
Correct |
92 ms |
5304 KB |
Output is correct |
14 |
Correct |
55 ms |
4624 KB |
Output is correct |
15 |
Correct |
51 ms |
4120 KB |
Output is correct |
16 |
Correct |
52 ms |
4044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
308 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
3 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
34 ms |
11216 KB |
Output is correct |
11 |
Correct |
63 ms |
7484 KB |
Output is correct |
12 |
Correct |
81 ms |
6884 KB |
Output is correct |
13 |
Correct |
91 ms |
7136 KB |
Output is correct |
14 |
Correct |
70 ms |
6496 KB |
Output is correct |
15 |
Correct |
53 ms |
3872 KB |
Output is correct |
16 |
Correct |
51 ms |
3880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
308 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
3 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
34 ms |
11216 KB |
Output is correct |
11 |
Correct |
63 ms |
7484 KB |
Output is correct |
12 |
Correct |
81 ms |
6884 KB |
Output is correct |
13 |
Correct |
91 ms |
7136 KB |
Output is correct |
14 |
Correct |
70 ms |
6496 KB |
Output is correct |
15 |
Correct |
53 ms |
3872 KB |
Output is correct |
16 |
Correct |
51 ms |
3880 KB |
Output is correct |
17 |
Correct |
66 ms |
7736 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
596 KB |
Output is correct |
24 |
Correct |
3 ms |
596 KB |
Output is correct |
25 |
Correct |
4 ms |
724 KB |
Output is correct |
26 |
Correct |
3 ms |
596 KB |
Output is correct |
27 |
Correct |
37 ms |
11436 KB |
Output is correct |
28 |
Correct |
83 ms |
7128 KB |
Output is correct |
29 |
Correct |
121 ms |
7396 KB |
Output is correct |
30 |
Correct |
72 ms |
6776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
11844 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
464 KB |
Output is correct |
7 |
Correct |
1 ms |
580 KB |
Output is correct |
8 |
Correct |
42 ms |
11888 KB |
Output is correct |
9 |
Correct |
39 ms |
11760 KB |
Output is correct |
10 |
Correct |
12 ms |
11012 KB |
Output is correct |
11 |
Correct |
61 ms |
8192 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
5 ms |
1172 KB |
Output is correct |
18 |
Correct |
5 ms |
1172 KB |
Output is correct |
19 |
Correct |
38 ms |
11884 KB |
Output is correct |
20 |
Correct |
86 ms |
7584 KB |
Output is correct |
21 |
Correct |
66 ms |
8324 KB |
Output is correct |
22 |
Correct |
53 ms |
6668 KB |
Output is correct |
23 |
Correct |
92 ms |
5304 KB |
Output is correct |
24 |
Correct |
55 ms |
4624 KB |
Output is correct |
25 |
Correct |
51 ms |
4120 KB |
Output is correct |
26 |
Correct |
52 ms |
4044 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
308 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
0 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
3 ms |
468 KB |
Output is correct |
35 |
Correct |
2 ms |
468 KB |
Output is correct |
36 |
Correct |
34 ms |
11216 KB |
Output is correct |
37 |
Correct |
63 ms |
7484 KB |
Output is correct |
38 |
Correct |
81 ms |
6884 KB |
Output is correct |
39 |
Correct |
91 ms |
7136 KB |
Output is correct |
40 |
Correct |
70 ms |
6496 KB |
Output is correct |
41 |
Correct |
53 ms |
3872 KB |
Output is correct |
42 |
Correct |
51 ms |
3880 KB |
Output is correct |
43 |
Correct |
66 ms |
7736 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
0 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
0 ms |
340 KB |
Output is correct |
49 |
Correct |
2 ms |
596 KB |
Output is correct |
50 |
Correct |
3 ms |
596 KB |
Output is correct |
51 |
Correct |
4 ms |
724 KB |
Output is correct |
52 |
Correct |
3 ms |
596 KB |
Output is correct |
53 |
Correct |
37 ms |
11436 KB |
Output is correct |
54 |
Correct |
83 ms |
7128 KB |
Output is correct |
55 |
Correct |
121 ms |
7396 KB |
Output is correct |
56 |
Correct |
72 ms |
6776 KB |
Output is correct |
57 |
Correct |
126 ms |
8132 KB |
Output is correct |
58 |
Correct |
40 ms |
11784 KB |
Output is correct |
59 |
Correct |
66 ms |
8196 KB |
Output is correct |