#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair <int, int>
#define fi first
#define se second
#define mp make_pair
const int NM = 50000, INF = 1e18;
struct matrix{
int M, N, grid[5][5];
void init(int x, int y, int val){
M = x, N = y;
for (int i = 0; i < M; i++)
for (int j = 0; j < N; j++)
grid[i][j] = val;
}
matrix operator * (matrix b){
matrix a = *this;
matrix c;
c.init(a.M, b.N, +INF);
for (int i = 0; i < c.M; i++)
for (int j = 0; j < c.N; j++)
for (int k = 0; k < a.N; k++)
c.grid[i][j] = min(c.grid[i][j], a.grid[i][k]+b.grid[k][j]);
return c;
}
};
int K, N, M, O, nLayer;
map <pii, int> d;
matrix A[NM+5];
matrix st[4*NM+5];
int Layer(int a){
return a/K;
}
int dist(int a, int b){
if (a == b) return 0;
if (d.count(mp(a, b)) == 0) return +INF;
return d[mp(a, b)];
}
matrix iden(int k){
matrix a;
a.init(k, k, +INF);
for (int i = 0; i < k; i++)
a.grid[i][i] = 0;
return a;
}
void build(int id, int l, int r){
st[id].init(K, K, 0);
if (l == r){
st[id] = A[l];
return;
}
int mid = (l+r)/2;
build(2*id, l, mid);
build(2*id+1, mid+1, r);
st[id] = st[2*id]*st[2*id+1];
}
matrix get(int id, int l, int r, int u, int v){
if (u > v || v < l || u > r) return iden(K);
if (l >= u && r <= v) return st[id];
int mid = (l+r)/2;
return get(2*id, l, mid, u, v)*get(2*id+1, mid+1, r, u, v);
}
void solveQuery(int a, int b){
if (Layer(a) >= Layer(b)){
cout << "-1\n";
return;
}
int l = Layer(a), r = Layer(b);
matrix C; C.init(1, K, +INF);
for (int i = 0; i < K; i++)
C.grid[0][i] = dist(a, (l+1)*K+i);
C = C*get(1, 0, nLayer-2, l+1, r-1);
int ans = C.grid[0][b%K];
if (ans >= +INF) ans = -1;
cout << ans << "\n";
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> K >> N >> M >> O;
nLayer = N/K;
if (N%K > 0) nLayer++;
for (int i = 0; i < nLayer; i++)
A[i].init(K, K, +INF);
while (M--){
int a, b, t;
cin >> a >> b >> t;
d[mp(a, b)] = t;
int k = Layer(a);
A[k].grid[a%K][b%K] = t;
}
build(1, 0, nLayer-2);
while (O--){
int a, b;
cin >> a >> b;
solveQuery(a, b);
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
41872 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
2 ms |
980 KB |
Output is correct |
6 |
Correct |
4 ms |
980 KB |
Output is correct |
7 |
Correct |
2 ms |
980 KB |
Output is correct |
8 |
Correct |
68 ms |
41804 KB |
Output is correct |
9 |
Correct |
68 ms |
41676 KB |
Output is correct |
10 |
Correct |
33 ms |
38736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
89 ms |
25500 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
9 ms |
1132 KB |
Output is correct |
8 |
Correct |
10 ms |
852 KB |
Output is correct |
9 |
Correct |
63 ms |
41872 KB |
Output is correct |
10 |
Correct |
121 ms |
20584 KB |
Output is correct |
11 |
Correct |
92 ms |
25932 KB |
Output is correct |
12 |
Correct |
78 ms |
16868 KB |
Output is correct |
13 |
Correct |
143 ms |
14600 KB |
Output is correct |
14 |
Correct |
69 ms |
15060 KB |
Output is correct |
15 |
Correct |
59 ms |
9804 KB |
Output is correct |
16 |
Correct |
65 ms |
9904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
2 ms |
980 KB |
Output is correct |
7 |
Correct |
2 ms |
724 KB |
Output is correct |
8 |
Correct |
4 ms |
852 KB |
Output is correct |
9 |
Correct |
2 ms |
724 KB |
Output is correct |
10 |
Correct |
48 ms |
41772 KB |
Output is correct |
11 |
Correct |
72 ms |
25512 KB |
Output is correct |
12 |
Correct |
100 ms |
20496 KB |
Output is correct |
13 |
Correct |
118 ms |
21432 KB |
Output is correct |
14 |
Correct |
81 ms |
19008 KB |
Output is correct |
15 |
Correct |
50 ms |
9812 KB |
Output is correct |
16 |
Correct |
48 ms |
9764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
2 ms |
980 KB |
Output is correct |
7 |
Correct |
2 ms |
724 KB |
Output is correct |
8 |
Correct |
4 ms |
852 KB |
Output is correct |
9 |
Correct |
2 ms |
724 KB |
Output is correct |
10 |
Correct |
48 ms |
41772 KB |
Output is correct |
11 |
Correct |
72 ms |
25512 KB |
Output is correct |
12 |
Correct |
100 ms |
20496 KB |
Output is correct |
13 |
Correct |
118 ms |
21432 KB |
Output is correct |
14 |
Correct |
81 ms |
19008 KB |
Output is correct |
15 |
Correct |
50 ms |
9812 KB |
Output is correct |
16 |
Correct |
48 ms |
9764 KB |
Output is correct |
17 |
Correct |
78 ms |
25460 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
328 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
4 ms |
980 KB |
Output is correct |
24 |
Correct |
6 ms |
852 KB |
Output is correct |
25 |
Correct |
11 ms |
836 KB |
Output is correct |
26 |
Correct |
8 ms |
724 KB |
Output is correct |
27 |
Correct |
53 ms |
41772 KB |
Output is correct |
28 |
Correct |
106 ms |
20440 KB |
Output is correct |
29 |
Correct |
147 ms |
21388 KB |
Output is correct |
30 |
Correct |
87 ms |
19008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
41872 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
2 ms |
980 KB |
Output is correct |
6 |
Correct |
4 ms |
980 KB |
Output is correct |
7 |
Correct |
2 ms |
980 KB |
Output is correct |
8 |
Correct |
68 ms |
41804 KB |
Output is correct |
9 |
Correct |
68 ms |
41676 KB |
Output is correct |
10 |
Correct |
33 ms |
38736 KB |
Output is correct |
11 |
Correct |
89 ms |
25500 KB |
Output is correct |
12 |
Correct |
1 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
324 KB |
Output is correct |
17 |
Correct |
9 ms |
1132 KB |
Output is correct |
18 |
Correct |
10 ms |
852 KB |
Output is correct |
19 |
Correct |
63 ms |
41872 KB |
Output is correct |
20 |
Correct |
121 ms |
20584 KB |
Output is correct |
21 |
Correct |
92 ms |
25932 KB |
Output is correct |
22 |
Correct |
78 ms |
16868 KB |
Output is correct |
23 |
Correct |
143 ms |
14600 KB |
Output is correct |
24 |
Correct |
69 ms |
15060 KB |
Output is correct |
25 |
Correct |
59 ms |
9804 KB |
Output is correct |
26 |
Correct |
65 ms |
9904 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
2 ms |
980 KB |
Output is correct |
33 |
Correct |
2 ms |
724 KB |
Output is correct |
34 |
Correct |
4 ms |
852 KB |
Output is correct |
35 |
Correct |
2 ms |
724 KB |
Output is correct |
36 |
Correct |
48 ms |
41772 KB |
Output is correct |
37 |
Correct |
72 ms |
25512 KB |
Output is correct |
38 |
Correct |
100 ms |
20496 KB |
Output is correct |
39 |
Correct |
118 ms |
21432 KB |
Output is correct |
40 |
Correct |
81 ms |
19008 KB |
Output is correct |
41 |
Correct |
50 ms |
9812 KB |
Output is correct |
42 |
Correct |
48 ms |
9764 KB |
Output is correct |
43 |
Correct |
78 ms |
25460 KB |
Output is correct |
44 |
Correct |
0 ms |
340 KB |
Output is correct |
45 |
Correct |
0 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
328 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
4 ms |
980 KB |
Output is correct |
50 |
Correct |
6 ms |
852 KB |
Output is correct |
51 |
Correct |
11 ms |
836 KB |
Output is correct |
52 |
Correct |
8 ms |
724 KB |
Output is correct |
53 |
Correct |
53 ms |
41772 KB |
Output is correct |
54 |
Correct |
106 ms |
20440 KB |
Output is correct |
55 |
Correct |
147 ms |
21388 KB |
Output is correct |
56 |
Correct |
87 ms |
19008 KB |
Output is correct |
57 |
Correct |
215 ms |
22492 KB |
Output is correct |
58 |
Correct |
73 ms |
41880 KB |
Output is correct |
59 |
Correct |
113 ms |
25844 KB |
Output is correct |