#include <bits/stdc++.h>
using namespace std;
#define rep(i,s,e) for (int i = s; i <= e; ++i)
#define rrep(i,s,e) for (int i = s; i >= e; --i)
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define all(a) a.begin(), a.end()
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int k, n, m, o; cin >> k >> n >> m >> o;
int d[n/k+1][30][k][k];
memset(d, -1, sizeof(d));
rep (i,0,m-1) {
int a, b, t; cin >> a >> b >> t;
d[a/k][0][a%k][b%k] = t;
}
rep (i,1,29) {
for (int j = 0; j/k+(1<<i) <= n/k; ++j) {
rep (a,0,k-1) {
rep (b,0,k-1) {
rep (c,0,k-1) {
int d1 = d[j/k][i-1][a][b];
int d2 = d[j/k+(1<<(i-1))][i-1][b][c];
if (min(d1,d2)==-1) continue;
if (d[j/k][i][a][c]==-1 || d[j/k][i][a][c]>d1+d2) d[j/k][i][a][c] = d1+d2;
}
}
}
}
}
while (o--) {
int a, b; cin >> a >> b;
int g = b/k-a/k, l = 0;
int ways[k], c = a/k;
memset(ways, -1, 4*k);
ways[a%k] = 0;
while (g) {
int new_ways[k];
memset(new_ways, -1, 4*k);
if (g%2) {
rep (x,0,k-1) {
rep (y,0,k-1) {
if (min(d[c][l][x][y], ways[x])==-1) continue;
if (new_ways[y]==-1 || new_ways[y]>ways[x]+d[c][l][x][y])
new_ways[y] = ways[x]+d[c][l][x][y];
}
}
c += (1<<l);
memcpy(ways, new_ways, 4*k);
}
l++;
g/=2;
}
cout << ways[b%k] << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
7112 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
33 ms |
7092 KB |
Output is correct |
9 |
Correct |
27 ms |
7072 KB |
Output is correct |
10 |
Correct |
14 ms |
6308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
13688 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
5 ms |
504 KB |
Output is correct |
8 |
Correct |
5 ms |
688 KB |
Output is correct |
9 |
Correct |
29 ms |
7188 KB |
Output is correct |
10 |
Correct |
105 ms |
20196 KB |
Output is correct |
11 |
Correct |
62 ms |
13824 KB |
Output is correct |
12 |
Correct |
117 ms |
19192 KB |
Output is correct |
13 |
Correct |
150 ms |
20328 KB |
Output is correct |
14 |
Correct |
57 ms |
12272 KB |
Output is correct |
15 |
Correct |
149 ms |
19064 KB |
Output is correct |
16 |
Correct |
145 ms |
19048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
4 ms |
852 KB |
Output is correct |
9 |
Correct |
3 ms |
724 KB |
Output is correct |
10 |
Correct |
26 ms |
6976 KB |
Output is correct |
11 |
Correct |
52 ms |
13688 KB |
Output is correct |
12 |
Correct |
107 ms |
20196 KB |
Output is correct |
13 |
Correct |
100 ms |
20212 KB |
Output is correct |
14 |
Correct |
105 ms |
19696 KB |
Output is correct |
15 |
Correct |
146 ms |
19020 KB |
Output is correct |
16 |
Correct |
147 ms |
19172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
4 ms |
852 KB |
Output is correct |
9 |
Correct |
3 ms |
724 KB |
Output is correct |
10 |
Correct |
26 ms |
6976 KB |
Output is correct |
11 |
Correct |
52 ms |
13688 KB |
Output is correct |
12 |
Correct |
107 ms |
20196 KB |
Output is correct |
13 |
Correct |
100 ms |
20212 KB |
Output is correct |
14 |
Correct |
105 ms |
19696 KB |
Output is correct |
15 |
Correct |
146 ms |
19020 KB |
Output is correct |
16 |
Correct |
147 ms |
19172 KB |
Output is correct |
17 |
Correct |
51 ms |
13516 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
320 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
320 KB |
Output is correct |
23 |
Correct |
2 ms |
464 KB |
Output is correct |
24 |
Correct |
3 ms |
596 KB |
Output is correct |
25 |
Correct |
5 ms |
980 KB |
Output is correct |
26 |
Correct |
4 ms |
852 KB |
Output is correct |
27 |
Correct |
28 ms |
7004 KB |
Output is correct |
28 |
Correct |
108 ms |
20080 KB |
Output is correct |
29 |
Correct |
105 ms |
20324 KB |
Output is correct |
30 |
Correct |
106 ms |
19748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
7112 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
33 ms |
7092 KB |
Output is correct |
9 |
Correct |
27 ms |
7072 KB |
Output is correct |
10 |
Correct |
14 ms |
6308 KB |
Output is correct |
11 |
Correct |
61 ms |
13688 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
5 ms |
504 KB |
Output is correct |
18 |
Correct |
5 ms |
688 KB |
Output is correct |
19 |
Correct |
29 ms |
7188 KB |
Output is correct |
20 |
Correct |
105 ms |
20196 KB |
Output is correct |
21 |
Correct |
62 ms |
13824 KB |
Output is correct |
22 |
Correct |
117 ms |
19192 KB |
Output is correct |
23 |
Correct |
150 ms |
20328 KB |
Output is correct |
24 |
Correct |
57 ms |
12272 KB |
Output is correct |
25 |
Correct |
149 ms |
19064 KB |
Output is correct |
26 |
Correct |
145 ms |
19048 KB |
Output is correct |
27 |
Correct |
1 ms |
320 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
320 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
4 ms |
852 KB |
Output is correct |
35 |
Correct |
3 ms |
724 KB |
Output is correct |
36 |
Correct |
26 ms |
6976 KB |
Output is correct |
37 |
Correct |
52 ms |
13688 KB |
Output is correct |
38 |
Correct |
107 ms |
20196 KB |
Output is correct |
39 |
Correct |
100 ms |
20212 KB |
Output is correct |
40 |
Correct |
105 ms |
19696 KB |
Output is correct |
41 |
Correct |
146 ms |
19020 KB |
Output is correct |
42 |
Correct |
147 ms |
19172 KB |
Output is correct |
43 |
Correct |
51 ms |
13516 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
320 KB |
Output is correct |
46 |
Correct |
0 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
320 KB |
Output is correct |
49 |
Correct |
2 ms |
464 KB |
Output is correct |
50 |
Correct |
3 ms |
596 KB |
Output is correct |
51 |
Correct |
5 ms |
980 KB |
Output is correct |
52 |
Correct |
4 ms |
852 KB |
Output is correct |
53 |
Correct |
28 ms |
7004 KB |
Output is correct |
54 |
Correct |
108 ms |
20080 KB |
Output is correct |
55 |
Correct |
105 ms |
20324 KB |
Output is correct |
56 |
Correct |
106 ms |
19748 KB |
Output is correct |
57 |
Correct |
189 ms |
27156 KB |
Output is correct |
58 |
Correct |
31 ms |
7064 KB |
Output is correct |
59 |
Correct |
57 ms |
13824 KB |
Output is correct |