# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
362629 |
2021-02-03T18:19:53 Z |
JerryLiu06 |
Toll (BOI17_toll) |
Java 11 |
|
2483 ms |
368100 KB |
import java.io.*;
import java.util.*;
// Solution Notes: Split the graph into N / K "layers" with K nodes each. Notice that each edge leads from one layer to the
// next layer. Now, let DP[a][b][x][y] denote the minimum cost of a path between nodes Ka + x and Kb + y. For any triple
// a < b < c, the following recurrence holds: DP[a][c][x][y] = MIN_{z in [0, K)} (DP[a][b][x][z] + DP[b][c][z][y]). This is
// known as the (min, +) product. Now, we have to note that there are N^2 K^2 states, so we have to reduce the number of
// states. This motivates us to, instead of storing DP[a][b][x][y], we can store only DP[a][a + 2^i][x][y] for each a, i, x,
// y. Then we can find the value of DP[A / K][B / K][A % K][B % K] in O(K^3 (N + O) log N) complexity.
public class toll {
static int K, N, M, O; static int[][][][] DP = new int[50010][20][5][5];
static int[][] temp = new int[5][5]; static int[][] ans = new int[5][5];
static final int INF = Integer.MAX_VALUE / 3;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
K = Integer.parseInt(st.nextToken());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
O = Integer.parseInt(st.nextToken());
for (int i = 0; i < 50010; i++) for (int j = 0; j < 20; j++) {
for (int k = 0; k < 5; k++) Arrays.fill(DP[i][j][k], INF);
}
for (int i = 0; i < M; i++) { st = new StringTokenizer(br.readLine());
int A = Integer.parseInt(st.nextToken());
int B = Integer.parseInt(st.nextToken());
int W = Integer.parseInt(st.nextToken());
DP[A / K][0][A % K][B % K] = W;
}
for (int j = 1; j < 20; j++) for (int i = 0; i + (1 << j) < (N + K - 1) / K; i++) {
combine(DP[i][j], DP[i][j - 1], DP[i + (1 << (j - 1))][j - 1]);
}
for (int i = 0; i < O; i++) { st = new StringTokenizer(br.readLine());
int A = Integer.parseInt(st.nextToken());
int B = Integer.parseInt(st.nextToken());
for (int j = 0; j < 5; j++) Arrays.fill(ans[j], INF);
for (int j = 0; j < 5; j++) ans[j][j] = 0;
int jump = (B / K) - (A / K); int curLev = A / K;
for (int j = 0; j < 20; j++) if ((jump & (1 << j)) > 0) {
for (int k = 0; k < 5; k++) Arrays.fill(temp[k], INF);
combine(temp, ans, DP[curLev][j]);
for (int k = 0; k < 5; k++) for (int l = 0; l < 5; l++) ans[k][l] = temp[k][l];
curLev += (1 << j);
}
System.out.println(ans[A % K][B % K] == INF ? -1 : ans[A % K][B % K]);
}
}
static void combine(int[][] res, int[][] A, int[][] B) {
for (int i = 0; i < 5; i++) for (int j = 0; j < 5; j++) for (int k = 0; k < 5; k++) {
res[i][j] = Math.min(res[i][j], A[i][k] + B[k][j]);
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2345 ms |
275468 KB |
Output is correct |
2 |
Correct |
1269 ms |
257516 KB |
Output is correct |
3 |
Correct |
1261 ms |
257380 KB |
Output is correct |
4 |
Correct |
1297 ms |
257152 KB |
Output is correct |
5 |
Correct |
1504 ms |
259108 KB |
Output is correct |
6 |
Correct |
1512 ms |
258876 KB |
Output is correct |
7 |
Correct |
1498 ms |
258968 KB |
Output is correct |
8 |
Correct |
2349 ms |
274768 KB |
Output is correct |
9 |
Correct |
2387 ms |
274376 KB |
Output is correct |
10 |
Correct |
2220 ms |
261744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2419 ms |
366088 KB |
Output is correct |
2 |
Correct |
1299 ms |
257572 KB |
Output is correct |
3 |
Correct |
1303 ms |
257044 KB |
Output is correct |
4 |
Correct |
1271 ms |
257224 KB |
Output is correct |
5 |
Correct |
1273 ms |
257296 KB |
Output is correct |
6 |
Correct |
1286 ms |
257268 KB |
Output is correct |
7 |
Correct |
1936 ms |
261836 KB |
Output is correct |
8 |
Correct |
2151 ms |
262936 KB |
Output is correct |
9 |
Correct |
2291 ms |
274776 KB |
Output is correct |
10 |
Correct |
2469 ms |
367008 KB |
Output is correct |
11 |
Correct |
2420 ms |
366484 KB |
Output is correct |
12 |
Correct |
2199 ms |
281468 KB |
Output is correct |
13 |
Correct |
2054 ms |
367392 KB |
Output is correct |
14 |
Correct |
1825 ms |
282480 KB |
Output is correct |
15 |
Correct |
1793 ms |
278496 KB |
Output is correct |
16 |
Correct |
1800 ms |
278600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1301 ms |
257012 KB |
Output is correct |
2 |
Correct |
1270 ms |
257144 KB |
Output is correct |
3 |
Correct |
1291 ms |
257280 KB |
Output is correct |
4 |
Correct |
1270 ms |
257408 KB |
Output is correct |
5 |
Correct |
1262 ms |
257264 KB |
Output is correct |
6 |
Correct |
1423 ms |
258932 KB |
Output is correct |
7 |
Correct |
1448 ms |
258324 KB |
Output is correct |
8 |
Correct |
1480 ms |
258984 KB |
Output is correct |
9 |
Correct |
1477 ms |
258704 KB |
Output is correct |
10 |
Correct |
1874 ms |
272004 KB |
Output is correct |
11 |
Correct |
1940 ms |
364788 KB |
Output is correct |
12 |
Correct |
1931 ms |
367184 KB |
Output is correct |
13 |
Correct |
1976 ms |
367464 KB |
Output is correct |
14 |
Correct |
1906 ms |
366424 KB |
Output is correct |
15 |
Correct |
1648 ms |
278400 KB |
Output is correct |
16 |
Correct |
1658 ms |
278396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1301 ms |
257012 KB |
Output is correct |
2 |
Correct |
1270 ms |
257144 KB |
Output is correct |
3 |
Correct |
1291 ms |
257280 KB |
Output is correct |
4 |
Correct |
1270 ms |
257408 KB |
Output is correct |
5 |
Correct |
1262 ms |
257264 KB |
Output is correct |
6 |
Correct |
1423 ms |
258932 KB |
Output is correct |
7 |
Correct |
1448 ms |
258324 KB |
Output is correct |
8 |
Correct |
1480 ms |
258984 KB |
Output is correct |
9 |
Correct |
1477 ms |
258704 KB |
Output is correct |
10 |
Correct |
1874 ms |
272004 KB |
Output is correct |
11 |
Correct |
1940 ms |
364788 KB |
Output is correct |
12 |
Correct |
1931 ms |
367184 KB |
Output is correct |
13 |
Correct |
1976 ms |
367464 KB |
Output is correct |
14 |
Correct |
1906 ms |
366424 KB |
Output is correct |
15 |
Correct |
1648 ms |
278400 KB |
Output is correct |
16 |
Correct |
1658 ms |
278396 KB |
Output is correct |
17 |
Correct |
2080 ms |
365236 KB |
Output is correct |
18 |
Correct |
1287 ms |
257388 KB |
Output is correct |
19 |
Correct |
1297 ms |
257296 KB |
Output is correct |
20 |
Correct |
1280 ms |
257544 KB |
Output is correct |
21 |
Correct |
1306 ms |
257332 KB |
Output is correct |
22 |
Correct |
1277 ms |
257156 KB |
Output is correct |
23 |
Correct |
1675 ms |
259276 KB |
Output is correct |
24 |
Correct |
2081 ms |
262344 KB |
Output is correct |
25 |
Correct |
1803 ms |
260340 KB |
Output is correct |
26 |
Correct |
1813 ms |
262168 KB |
Output is correct |
27 |
Correct |
1981 ms |
271860 KB |
Output is correct |
28 |
Correct |
2137 ms |
367088 KB |
Output is correct |
29 |
Correct |
2118 ms |
367204 KB |
Output is correct |
30 |
Correct |
2040 ms |
366996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2345 ms |
275468 KB |
Output is correct |
2 |
Correct |
1269 ms |
257516 KB |
Output is correct |
3 |
Correct |
1261 ms |
257380 KB |
Output is correct |
4 |
Correct |
1297 ms |
257152 KB |
Output is correct |
5 |
Correct |
1504 ms |
259108 KB |
Output is correct |
6 |
Correct |
1512 ms |
258876 KB |
Output is correct |
7 |
Correct |
1498 ms |
258968 KB |
Output is correct |
8 |
Correct |
2349 ms |
274768 KB |
Output is correct |
9 |
Correct |
2387 ms |
274376 KB |
Output is correct |
10 |
Correct |
2220 ms |
261744 KB |
Output is correct |
11 |
Correct |
2419 ms |
366088 KB |
Output is correct |
12 |
Correct |
1299 ms |
257572 KB |
Output is correct |
13 |
Correct |
1303 ms |
257044 KB |
Output is correct |
14 |
Correct |
1271 ms |
257224 KB |
Output is correct |
15 |
Correct |
1273 ms |
257296 KB |
Output is correct |
16 |
Correct |
1286 ms |
257268 KB |
Output is correct |
17 |
Correct |
1936 ms |
261836 KB |
Output is correct |
18 |
Correct |
2151 ms |
262936 KB |
Output is correct |
19 |
Correct |
2291 ms |
274776 KB |
Output is correct |
20 |
Correct |
2469 ms |
367008 KB |
Output is correct |
21 |
Correct |
2420 ms |
366484 KB |
Output is correct |
22 |
Correct |
2199 ms |
281468 KB |
Output is correct |
23 |
Correct |
2054 ms |
367392 KB |
Output is correct |
24 |
Correct |
1825 ms |
282480 KB |
Output is correct |
25 |
Correct |
1793 ms |
278496 KB |
Output is correct |
26 |
Correct |
1800 ms |
278600 KB |
Output is correct |
27 |
Correct |
1301 ms |
257012 KB |
Output is correct |
28 |
Correct |
1270 ms |
257144 KB |
Output is correct |
29 |
Correct |
1291 ms |
257280 KB |
Output is correct |
30 |
Correct |
1270 ms |
257408 KB |
Output is correct |
31 |
Correct |
1262 ms |
257264 KB |
Output is correct |
32 |
Correct |
1423 ms |
258932 KB |
Output is correct |
33 |
Correct |
1448 ms |
258324 KB |
Output is correct |
34 |
Correct |
1480 ms |
258984 KB |
Output is correct |
35 |
Correct |
1477 ms |
258704 KB |
Output is correct |
36 |
Correct |
1874 ms |
272004 KB |
Output is correct |
37 |
Correct |
1940 ms |
364788 KB |
Output is correct |
38 |
Correct |
1931 ms |
367184 KB |
Output is correct |
39 |
Correct |
1976 ms |
367464 KB |
Output is correct |
40 |
Correct |
1906 ms |
366424 KB |
Output is correct |
41 |
Correct |
1648 ms |
278400 KB |
Output is correct |
42 |
Correct |
1658 ms |
278396 KB |
Output is correct |
43 |
Correct |
2080 ms |
365236 KB |
Output is correct |
44 |
Correct |
1287 ms |
257388 KB |
Output is correct |
45 |
Correct |
1297 ms |
257296 KB |
Output is correct |
46 |
Correct |
1280 ms |
257544 KB |
Output is correct |
47 |
Correct |
1306 ms |
257332 KB |
Output is correct |
48 |
Correct |
1277 ms |
257156 KB |
Output is correct |
49 |
Correct |
1675 ms |
259276 KB |
Output is correct |
50 |
Correct |
2081 ms |
262344 KB |
Output is correct |
51 |
Correct |
1803 ms |
260340 KB |
Output is correct |
52 |
Correct |
1813 ms |
262168 KB |
Output is correct |
53 |
Correct |
1981 ms |
271860 KB |
Output is correct |
54 |
Correct |
2137 ms |
367088 KB |
Output is correct |
55 |
Correct |
2118 ms |
367204 KB |
Output is correct |
56 |
Correct |
2040 ms |
366996 KB |
Output is correct |
57 |
Correct |
2440 ms |
368100 KB |
Output is correct |
58 |
Correct |
2332 ms |
274960 KB |
Output is correct |
59 |
Correct |
2483 ms |
366520 KB |
Output is correct |