#include<bits/stdc++.h>
using namespace std;
#define cIO ios_base::sync_with_stdio(0);cin.tie(0)
#define fileIO(x) ifstream fin(string(x)+".in"); ofstream fout(string(x)+".out")
#define cont(container, object) (container.find(object)!=container.end())
#define sz(x) (int) (x).size()
#define ll long long
#define v vector
const int max_log = 17;
const int INF = INT_MAX / 3;
v<v<v<int>>> jmp;
int K, N, M, O;
//if WA use ll
int main()
{
//everything 0-indexed
fileIO("toll");
cin >> K >> N >> M >> O;
jmp= v<v<v<int>>>(N+6, v<v<int>>(max_log, v<int>(K, INF)));//[node][2^depth]=vector of costs
for (int i = 0; i <= M - 1; i++)
{
int a, b, t; cin >> a >> b >> t;
jmp[a][0][b%K] = t;
}
//debug
/*for (int i = 0; i <= N - 1; i++)
{
cout << i << ": ";
for (int j = 0; j <= K - 1; j++)
{
if (jmp[i][0][j] == INF) cout << "NUL ";
else cout << jmp[i][0][j] << " ";
}
cout << "\n";
}*/
for (int dep=1; dep<=max_log; dep++)
for (int i = 0; i <= N - 1; i++)
{
int mid_start = (i / K + (1<<(dep-1))) * K, mid_end = mid_start + 4;
int res_start= (i / K + (1 << (dep))) * K, res_end = res_start + 4;
if (res_start >= N) continue;
for (int x = 0; x <= K - 1; x++)//x is the index of the middle
{
for (int y = 0; y <= K - 1; y++)//y is the index of the result
{
jmp[i][dep][y] = min(jmp[i][dep][y],
jmp[i][dep - 1][x] + jmp[mid_start + x][dep - 1][y]);
}
//jmp should not be above INF or something is wrong
}
}
//answer queries
for (int q = 0; q <= O - 1; q++)
{
int a, b; cin >> a >> b;
int tot_jumps = (int)(b / K) - a / K;
if (tot_jumps == 0)
{
cout << "-1\n";
continue;
}
int b_pos = b % K;
int cur_pos = (int)(a / K)*K;
v<int> cur_arr(K, INF); cur_arr[a % K] = 0;
for (int i = 0; i <= max_log; i++)
{
if (tot_jumps & (1 << i))//we should jump i
{
v<int> new_arr(K, INF);
for (int x = 0; x <= K - 1; x++)
for (int y = 0; y <= K - 1; y++)
new_arr[y] = min(new_arr[y], jmp[cur_pos + x][i][y] + cur_arr[x]);
cur_arr = new_arr;
cur_pos += K*(1 << i);
}
}
if (cur_arr[b_pos] == INF) cout << "-1\n";
else cout << cur_arr[b_pos] << "\n";
}
return 0;
}
Compilation message
toll.cpp: In function 'int main()':
toll.cpp:40:48: warning: unused variable 'mid_end' [-Wunused-variable]
40 | int mid_start = (i / K + (1<<(dep-1))) * K, mid_end = mid_start + 4;
| ^~~~~~~
toll.cpp:41:47: warning: unused variable 'res_end' [-Wunused-variable]
41 | int res_start= (i / K + (1 << (dep))) * K, res_end = res_start + 4;
| ^~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
172 ms |
49456 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 |
4 ms |
1212 KB |
Output is correct |
6 |
Correct |
5 ms |
1236 KB |
Output is correct |
7 |
Correct |
4 ms |
1216 KB |
Output is correct |
8 |
Correct |
152 ms |
49364 KB |
Output is correct |
9 |
Correct |
156 ms |
49396 KB |
Output is correct |
10 |
Correct |
110 ms |
48488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
185 ms |
50056 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 |
304 KB |
Output is correct |
7 |
Correct |
21 ms |
1364 KB |
Output is correct |
8 |
Correct |
23 ms |
1392 KB |
Output is correct |
9 |
Correct |
135 ms |
49280 KB |
Output is correct |
10 |
Correct |
238 ms |
50736 KB |
Output is correct |
11 |
Correct |
192 ms |
50324 KB |
Output is correct |
12 |
Correct |
182 ms |
49648 KB |
Output is correct |
13 |
Correct |
190 ms |
31520 KB |
Output is correct |
14 |
Correct |
162 ms |
30540 KB |
Output is correct |
15 |
Correct |
129 ms |
30320 KB |
Output is correct |
16 |
Correct |
121 ms |
30352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
312 KB |
Output is correct |
6 |
Correct |
2 ms |
1236 KB |
Output is correct |
7 |
Correct |
3 ms |
1236 KB |
Output is correct |
8 |
Correct |
5 ms |
1212 KB |
Output is correct |
9 |
Correct |
4 ms |
1236 KB |
Output is correct |
10 |
Correct |
122 ms |
49208 KB |
Output is correct |
11 |
Correct |
162 ms |
49984 KB |
Output is correct |
12 |
Correct |
212 ms |
50608 KB |
Output is correct |
13 |
Correct |
228 ms |
50984 KB |
Output is correct |
14 |
Correct |
207 ms |
50280 KB |
Output is correct |
15 |
Correct |
115 ms |
30368 KB |
Output is correct |
16 |
Correct |
133 ms |
30364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
312 KB |
Output is correct |
6 |
Correct |
2 ms |
1236 KB |
Output is correct |
7 |
Correct |
3 ms |
1236 KB |
Output is correct |
8 |
Correct |
5 ms |
1212 KB |
Output is correct |
9 |
Correct |
4 ms |
1236 KB |
Output is correct |
10 |
Correct |
122 ms |
49208 KB |
Output is correct |
11 |
Correct |
162 ms |
49984 KB |
Output is correct |
12 |
Correct |
212 ms |
50608 KB |
Output is correct |
13 |
Correct |
228 ms |
50984 KB |
Output is correct |
14 |
Correct |
207 ms |
50280 KB |
Output is correct |
15 |
Correct |
115 ms |
30368 KB |
Output is correct |
16 |
Correct |
133 ms |
30364 KB |
Output is correct |
17 |
Correct |
179 ms |
49968 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
308 KB |
Output is correct |
20 |
Correct |
1 ms |
304 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
7 ms |
1236 KB |
Output is correct |
24 |
Correct |
9 ms |
1320 KB |
Output is correct |
25 |
Correct |
11 ms |
1364 KB |
Output is correct |
26 |
Correct |
10 ms |
1236 KB |
Output is correct |
27 |
Correct |
124 ms |
49196 KB |
Output is correct |
28 |
Correct |
223 ms |
50664 KB |
Output is correct |
29 |
Correct |
262 ms |
50892 KB |
Output is correct |
30 |
Correct |
207 ms |
50308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
172 ms |
49456 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 |
4 ms |
1212 KB |
Output is correct |
6 |
Correct |
5 ms |
1236 KB |
Output is correct |
7 |
Correct |
4 ms |
1216 KB |
Output is correct |
8 |
Correct |
152 ms |
49364 KB |
Output is correct |
9 |
Correct |
156 ms |
49396 KB |
Output is correct |
10 |
Correct |
110 ms |
48488 KB |
Output is correct |
11 |
Correct |
185 ms |
50056 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 |
304 KB |
Output is correct |
17 |
Correct |
21 ms |
1364 KB |
Output is correct |
18 |
Correct |
23 ms |
1392 KB |
Output is correct |
19 |
Correct |
135 ms |
49280 KB |
Output is correct |
20 |
Correct |
238 ms |
50736 KB |
Output is correct |
21 |
Correct |
192 ms |
50324 KB |
Output is correct |
22 |
Correct |
182 ms |
49648 KB |
Output is correct |
23 |
Correct |
190 ms |
31520 KB |
Output is correct |
24 |
Correct |
162 ms |
30540 KB |
Output is correct |
25 |
Correct |
129 ms |
30320 KB |
Output is correct |
26 |
Correct |
121 ms |
30352 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
1 ms |
312 KB |
Output is correct |
32 |
Correct |
2 ms |
1236 KB |
Output is correct |
33 |
Correct |
3 ms |
1236 KB |
Output is correct |
34 |
Correct |
5 ms |
1212 KB |
Output is correct |
35 |
Correct |
4 ms |
1236 KB |
Output is correct |
36 |
Correct |
122 ms |
49208 KB |
Output is correct |
37 |
Correct |
162 ms |
49984 KB |
Output is correct |
38 |
Correct |
212 ms |
50608 KB |
Output is correct |
39 |
Correct |
228 ms |
50984 KB |
Output is correct |
40 |
Correct |
207 ms |
50280 KB |
Output is correct |
41 |
Correct |
115 ms |
30368 KB |
Output is correct |
42 |
Correct |
133 ms |
30364 KB |
Output is correct |
43 |
Correct |
179 ms |
49968 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
308 KB |
Output is correct |
46 |
Correct |
1 ms |
304 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
7 ms |
1236 KB |
Output is correct |
50 |
Correct |
9 ms |
1320 KB |
Output is correct |
51 |
Correct |
11 ms |
1364 KB |
Output is correct |
52 |
Correct |
10 ms |
1236 KB |
Output is correct |
53 |
Correct |
124 ms |
49196 KB |
Output is correct |
54 |
Correct |
223 ms |
50664 KB |
Output is correct |
55 |
Correct |
262 ms |
50892 KB |
Output is correct |
56 |
Correct |
207 ms |
50308 KB |
Output is correct |
57 |
Correct |
316 ms |
51752 KB |
Output is correct |
58 |
Correct |
154 ms |
49400 KB |
Output is correct |
59 |
Correct |
208 ms |
50228 KB |
Output is correct |