#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int> pi;
#define f first
#define s second
#define FAST ios_base::sync_with_stdio(0); cin.tie(0);
typedef pair<int,pi> pii;
const int maxn = 50010;
const int INF = INT_MAX/2;
//2^16
int dist[maxn][18][5][5];
int k, n, e, q;
int32_t main() {
FAST
cin >> k >> n >> e >> q;
for (int i =0;i<=n/k;i++) {
for (int j = 0; j <17;j++) {
for (int x = 0; x < k;x++) {
for (int y=0; y < k; y++) {
dist[i][j][x][y] = INF;
}
}
}
}
for (int i =0;i<e;i++) {
int a,b,c; cin >> a >> b >> c;
dist[a/k][0][a % k][b % k] = c;
}
for (int b = 1; b < 17; b++) {
for (int i =0; i <= n; i++) {
if (i*k + (1 << b)*k > n) continue;
for (int mid = 0; mid < k; mid++) {
for (int x = 0; x < k; x++) {
for (int y = 0; y < k; y++) {
int newi = i + (1 << (b-1));
int newd = dist[i][b-1][x][mid] + dist[newi][b-1][mid][y];
dist[i][b][x][y] = min(dist[i][b][x][y], newd) ;
}
}
}
}
}
int bestprev[5];
int best[5];
for (int qq = 0; qq < q; qq++) {
int a, b; cin >> a >> b;
int curbuc = a/k;
int finalbuc = b/k;
memset(best,0,sizeof best);
for (int x=0;x<k;x++) bestprev[x] = INF;
bestprev[a%k] = 0;
for (int k2 = 16; k2 >= 0; k2--) {
if (curbuc + (1 << k2) > finalbuc) continue;
for (int y = 0; y < k; y++) {
best[y] = INF;
for (int x = 0; x< k;x++) {
best[y] = min(best[y], bestprev[x] + dist[curbuc][k2][x][y]);
}
}
swap(bestprev,best);
curbuc += (1 << k2);
}
int ans = bestprev[b%k];
if (ans >= INF) cout << "-1\n";
else cout << ans << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
144 ms |
177644 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
3 ms |
3948 KB |
Output is correct |
6 |
Correct |
3 ms |
3948 KB |
Output is correct |
7 |
Correct |
3 ms |
3948 KB |
Output is correct |
8 |
Correct |
131 ms |
177388 KB |
Output is correct |
9 |
Correct |
133 ms |
177388 KB |
Output is correct |
10 |
Correct |
115 ms |
176620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
129 ms |
90092 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
6 ms |
3948 KB |
Output is correct |
8 |
Correct |
6 ms |
2284 KB |
Output is correct |
9 |
Correct |
131 ms |
177388 KB |
Output is correct |
10 |
Correct |
131 ms |
61396 KB |
Output is correct |
11 |
Correct |
133 ms |
90368 KB |
Output is correct |
12 |
Correct |
103 ms |
60396 KB |
Output is correct |
13 |
Correct |
87 ms |
23916 KB |
Output is correct |
14 |
Correct |
73 ms |
37100 KB |
Output is correct |
15 |
Correct |
76 ms |
22764 KB |
Output is correct |
16 |
Correct |
61 ms |
22764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
3 ms |
3820 KB |
Output is correct |
7 |
Correct |
2 ms |
2156 KB |
Output is correct |
8 |
Correct |
3 ms |
1132 KB |
Output is correct |
9 |
Correct |
3 ms |
1260 KB |
Output is correct |
10 |
Correct |
137 ms |
177260 KB |
Output is correct |
11 |
Correct |
123 ms |
89964 KB |
Output is correct |
12 |
Correct |
124 ms |
61292 KB |
Output is correct |
13 |
Correct |
124 ms |
61416 KB |
Output is correct |
14 |
Correct |
108 ms |
61036 KB |
Output is correct |
15 |
Correct |
55 ms |
22636 KB |
Output is correct |
16 |
Correct |
59 ms |
22636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
3 ms |
3820 KB |
Output is correct |
7 |
Correct |
2 ms |
2156 KB |
Output is correct |
8 |
Correct |
3 ms |
1132 KB |
Output is correct |
9 |
Correct |
3 ms |
1260 KB |
Output is correct |
10 |
Correct |
137 ms |
177260 KB |
Output is correct |
11 |
Correct |
123 ms |
89964 KB |
Output is correct |
12 |
Correct |
124 ms |
61292 KB |
Output is correct |
13 |
Correct |
124 ms |
61416 KB |
Output is correct |
14 |
Correct |
108 ms |
61036 KB |
Output is correct |
15 |
Correct |
55 ms |
22636 KB |
Output is correct |
16 |
Correct |
59 ms |
22636 KB |
Output is correct |
17 |
Correct |
143 ms |
89964 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
4 ms |
3948 KB |
Output is correct |
24 |
Correct |
3 ms |
2156 KB |
Output is correct |
25 |
Correct |
4 ms |
1132 KB |
Output is correct |
26 |
Correct |
3 ms |
1388 KB |
Output is correct |
27 |
Correct |
139 ms |
177388 KB |
Output is correct |
28 |
Correct |
123 ms |
61292 KB |
Output is correct |
29 |
Correct |
122 ms |
61548 KB |
Output is correct |
30 |
Correct |
110 ms |
61052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
144 ms |
177644 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
3 ms |
3948 KB |
Output is correct |
6 |
Correct |
3 ms |
3948 KB |
Output is correct |
7 |
Correct |
3 ms |
3948 KB |
Output is correct |
8 |
Correct |
131 ms |
177388 KB |
Output is correct |
9 |
Correct |
133 ms |
177388 KB |
Output is correct |
10 |
Correct |
115 ms |
176620 KB |
Output is correct |
11 |
Correct |
129 ms |
90092 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
6 ms |
3948 KB |
Output is correct |
18 |
Correct |
6 ms |
2284 KB |
Output is correct |
19 |
Correct |
131 ms |
177388 KB |
Output is correct |
20 |
Correct |
131 ms |
61396 KB |
Output is correct |
21 |
Correct |
133 ms |
90368 KB |
Output is correct |
22 |
Correct |
103 ms |
60396 KB |
Output is correct |
23 |
Correct |
87 ms |
23916 KB |
Output is correct |
24 |
Correct |
73 ms |
37100 KB |
Output is correct |
25 |
Correct |
76 ms |
22764 KB |
Output is correct |
26 |
Correct |
61 ms |
22764 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
3 ms |
3820 KB |
Output is correct |
33 |
Correct |
2 ms |
2156 KB |
Output is correct |
34 |
Correct |
3 ms |
1132 KB |
Output is correct |
35 |
Correct |
3 ms |
1260 KB |
Output is correct |
36 |
Correct |
137 ms |
177260 KB |
Output is correct |
37 |
Correct |
123 ms |
89964 KB |
Output is correct |
38 |
Correct |
124 ms |
61292 KB |
Output is correct |
39 |
Correct |
124 ms |
61416 KB |
Output is correct |
40 |
Correct |
108 ms |
61036 KB |
Output is correct |
41 |
Correct |
55 ms |
22636 KB |
Output is correct |
42 |
Correct |
59 ms |
22636 KB |
Output is correct |
43 |
Correct |
143 ms |
89964 KB |
Output is correct |
44 |
Correct |
1 ms |
364 KB |
Output is correct |
45 |
Correct |
1 ms |
364 KB |
Output is correct |
46 |
Correct |
1 ms |
364 KB |
Output is correct |
47 |
Correct |
1 ms |
364 KB |
Output is correct |
48 |
Correct |
1 ms |
364 KB |
Output is correct |
49 |
Correct |
4 ms |
3948 KB |
Output is correct |
50 |
Correct |
3 ms |
2156 KB |
Output is correct |
51 |
Correct |
4 ms |
1132 KB |
Output is correct |
52 |
Correct |
3 ms |
1388 KB |
Output is correct |
53 |
Correct |
139 ms |
177388 KB |
Output is correct |
54 |
Correct |
123 ms |
61292 KB |
Output is correct |
55 |
Correct |
122 ms |
61548 KB |
Output is correct |
56 |
Correct |
110 ms |
61052 KB |
Output is correct |
57 |
Correct |
145 ms |
47724 KB |
Output is correct |
58 |
Correct |
134 ms |
177500 KB |
Output is correct |
59 |
Correct |
139 ms |
90220 KB |
Output is correct |