//Dost SEFEROĞLU
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define ff first
#define ss second
#define sp << " " <<
#define vi vector<int>
#define all(xx) xx.begin(),xx.end()
#define ps(xxx) cout << (xxx) << endl;
const int N = 5e4+1,inf = 1e9,K = 5;
int k,n,m,o;
struct Matrix {
int c[K][K];
Matrix() {
for (int i=0;i<k;i++) {
for (int j=0;j<k;j++) c[i][j] = inf;
}
}
};
Matrix up[N][16];
Matrix merge(const Matrix& m1,const Matrix& m2) {
Matrix r;
for (int i=0;i<k;i++) {
for (int j=0;j<k;j++) {
for (int jj=0;jj<k;jj++) {
r.c[i][j] = min(r.c[i][j],m1.c[i][jj]+m2.c[jj][j]);
}
}
}
return r;
}
void solve() {
cin >> k >> n >> m >> o;
for (int i=0;i<=n/k;i++) for (int j=0;j<16;j++) up[i][j] = Matrix();
for (int i=1;i<=m;i++) {
int a,b,t;
cin >> a >> b >> t;
up[a/k][0].c[a%k][b%k] = t;
}
for (int i=1;i<16;i++) {
for (int j=0;j+(1<<(i-1))<n/k;j++) {
up[j][i] = merge(up[j][i-1],up[j+(1<<(i-1))][i-1]);
}
}
Matrix M;
while (o--) {
int a,b;
cin >> a >> b;
if (a/k == b/k) {
cout << ((a == b) ? 0 : -1) << endl;
continue;
}
int l1 = a/k,l2 = b/k;
M = up[l1][0];
l1++;
assert(l1 <= l2);
for (int i=15;i>=0;i--) {
if (l1+(1<<i) <= l2) {
M = merge(M,up[l1][i]);
l1+=(1<<i);
}
}
cout << (M.c[a%k][b%k] == inf ? -1 : M.c[a%k][b%k]) << '\n';
}
}
signed main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#ifdef Dodi
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t = 1;
//cin >> t;
while (t --> 0) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
97 ms |
157780 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
4700 KB |
Output is correct |
6 |
Correct |
3 ms |
4700 KB |
Output is correct |
7 |
Correct |
3 ms |
4696 KB |
Output is correct |
8 |
Correct |
115 ms |
157832 KB |
Output is correct |
9 |
Correct |
96 ms |
157780 KB |
Output is correct |
10 |
Correct |
82 ms |
156936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
78672 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
4 ms |
4696 KB |
Output is correct |
8 |
Correct |
4 ms |
2652 KB |
Output is correct |
9 |
Correct |
91 ms |
158028 KB |
Output is correct |
10 |
Correct |
64 ms |
54868 KB |
Output is correct |
11 |
Correct |
63 ms |
80464 KB |
Output is correct |
12 |
Correct |
50 ms |
53840 KB |
Output is correct |
13 |
Correct |
45 ms |
21584 KB |
Output is correct |
14 |
Correct |
35 ms |
33112 KB |
Output is correct |
15 |
Correct |
39 ms |
20384 KB |
Output is correct |
16 |
Correct |
33 ms |
20256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
4700 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2648 KB |
Output is correct |
10 |
Correct |
91 ms |
157724 KB |
Output is correct |
11 |
Correct |
60 ms |
80208 KB |
Output is correct |
12 |
Correct |
62 ms |
54612 KB |
Output is correct |
13 |
Correct |
55 ms |
54868 KB |
Output is correct |
14 |
Correct |
49 ms |
54352 KB |
Output is correct |
15 |
Correct |
26 ms |
20304 KB |
Output is correct |
16 |
Correct |
27 ms |
20312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
4700 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
2 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2648 KB |
Output is correct |
10 |
Correct |
91 ms |
157724 KB |
Output is correct |
11 |
Correct |
60 ms |
80208 KB |
Output is correct |
12 |
Correct |
62 ms |
54612 KB |
Output is correct |
13 |
Correct |
55 ms |
54868 KB |
Output is correct |
14 |
Correct |
49 ms |
54352 KB |
Output is correct |
15 |
Correct |
26 ms |
20304 KB |
Output is correct |
16 |
Correct |
27 ms |
20312 KB |
Output is correct |
17 |
Correct |
57 ms |
80212 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
3 ms |
4696 KB |
Output is correct |
24 |
Correct |
2 ms |
2648 KB |
Output is correct |
25 |
Correct |
4 ms |
2652 KB |
Output is correct |
26 |
Correct |
3 ms |
2652 KB |
Output is correct |
27 |
Correct |
91 ms |
157744 KB |
Output is correct |
28 |
Correct |
61 ms |
54804 KB |
Output is correct |
29 |
Correct |
63 ms |
54864 KB |
Output is correct |
30 |
Correct |
70 ms |
54468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
97 ms |
157780 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
4700 KB |
Output is correct |
6 |
Correct |
3 ms |
4700 KB |
Output is correct |
7 |
Correct |
3 ms |
4696 KB |
Output is correct |
8 |
Correct |
115 ms |
157832 KB |
Output is correct |
9 |
Correct |
96 ms |
157780 KB |
Output is correct |
10 |
Correct |
82 ms |
156936 KB |
Output is correct |
11 |
Correct |
69 ms |
78672 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
4 ms |
4696 KB |
Output is correct |
18 |
Correct |
4 ms |
2652 KB |
Output is correct |
19 |
Correct |
91 ms |
158028 KB |
Output is correct |
20 |
Correct |
64 ms |
54868 KB |
Output is correct |
21 |
Correct |
63 ms |
80464 KB |
Output is correct |
22 |
Correct |
50 ms |
53840 KB |
Output is correct |
23 |
Correct |
45 ms |
21584 KB |
Output is correct |
24 |
Correct |
35 ms |
33112 KB |
Output is correct |
25 |
Correct |
39 ms |
20384 KB |
Output is correct |
26 |
Correct |
33 ms |
20256 KB |
Output is correct |
27 |
Correct |
1 ms |
600 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
2 ms |
4700 KB |
Output is correct |
33 |
Correct |
2 ms |
2652 KB |
Output is correct |
34 |
Correct |
2 ms |
2652 KB |
Output is correct |
35 |
Correct |
2 ms |
2648 KB |
Output is correct |
36 |
Correct |
91 ms |
157724 KB |
Output is correct |
37 |
Correct |
60 ms |
80208 KB |
Output is correct |
38 |
Correct |
62 ms |
54612 KB |
Output is correct |
39 |
Correct |
55 ms |
54868 KB |
Output is correct |
40 |
Correct |
49 ms |
54352 KB |
Output is correct |
41 |
Correct |
26 ms |
20304 KB |
Output is correct |
42 |
Correct |
27 ms |
20312 KB |
Output is correct |
43 |
Correct |
57 ms |
80212 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
344 KB |
Output is correct |
49 |
Correct |
3 ms |
4696 KB |
Output is correct |
50 |
Correct |
2 ms |
2648 KB |
Output is correct |
51 |
Correct |
4 ms |
2652 KB |
Output is correct |
52 |
Correct |
3 ms |
2652 KB |
Output is correct |
53 |
Correct |
91 ms |
157744 KB |
Output is correct |
54 |
Correct |
61 ms |
54804 KB |
Output is correct |
55 |
Correct |
63 ms |
54864 KB |
Output is correct |
56 |
Correct |
70 ms |
54468 KB |
Output is correct |
57 |
Correct |
69 ms |
42768 KB |
Output is correct |
58 |
Correct |
95 ms |
157908 KB |
Output is correct |
59 |
Correct |
81 ms |
80468 KB |
Output is correct |