Submission #354306

# Submission time Handle Problem Language Result Execution time Memory
354306 2021-01-21T17:22:56 Z parsabahrami Toll (BOI17_toll) C++17
100 / 100
170 ms 29036 KB
// Call my Name and Save me from The Dark
#include <bits/stdc++.h>
 
using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
 
#define SZ(x)                       (int) x.size()
#define F                           first
#define S                           second

const int N = 5e4 + 10, MOD = 1e9 + 7;
int M[N], dp[18][5][N], n, k, q, m; vector<pii> in[N], out[N];

void divide(int l = 0, int r = (n - 1) / k + 1, int h = 0) {
    if (r - l < 3) return;
    int mid = (l + r) >> 1;
    //printf("l %d mid %d r %d\n", l, mid, r);
    for (int i = mid * k; i < mid * k + k; i++) dp[h][i % k][i] = 0;
    for (int i = mid * k - 1; i >= l * k; i--) {
        for (int j = 0; j < k; j++) for (pii u : out[i]) {
            dp[h][j][i] = min(dp[h][j][i], dp[h][j][u.F] + u.S);
        }
    }
    for (int i = mid * k + k; i < min(n, r * k); i++) {
        for (int j = 0; j < k; j++) for (pii u : in[i]) {
            dp[h][j][i] = min(dp[h][j][i], dp[h][j][u.F] + u.S);
        }
    }
    for (int i = mid; i < r; i++) M[i] ^= 1 << h;
    divide(l, mid, h + 1), divide(mid, r, h + 1);
}

int main() {
    for (int i = 0; i < 18; i++) for (int j = 0; j < 5; j++) fill(dp[i][j], dp[i][j] + N, MOD);
    scanf("%d%d%d%d", &k, &n, &m, &q);
    for (int i = 0; i < m; i++) {
        int u, v, w; scanf("%d%d%d", &u, &v, &w);
        out[u].push_back({v, w});
        in[v].push_back({u, w});
    }
    divide();
    for (; q; q--) {
        int a, b; scanf("%d%d", &a, &b);
        if (a == b) printf("%d\n", 0);
        else if (a / k == b / k) printf("-1\n");
        else if (b / k - a / k == 1) {
            int ret = -1;
            for (pii u : out[a])  if (u.F == b) ret = u.S;
            printf("%d\n", ret);
        } else {
            if (M[a / k] == M[b / k]) {
                printf("%d %d\n", a / k, b / k);
                exit(0);
            }
            int h = __builtin_ctz(M[a / k] ^ M[b / k]);
            int ret = MOD;
            for (int i = 0; i < 5; i++) {
                ret = min(ret, dp[h][i][a] + dp[h][i][b]);
            }
            printf("%d\n", ret == MOD ? -1 : ret);
        }
    }
    return 0;
}

Compilation message

toll.cpp: In function 'int main()':
toll.cpp:37:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   37 |     scanf("%d%d%d%d", &k, &n, &m, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:39:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   39 |         int u, v, w; scanf("%d%d%d", &u, &v, &w);
      |                      ~~~~~^~~~~~~~~~~~~~~~~~~~~~
toll.cpp:45:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   45 |         int a, b; scanf("%d%d", &a, &b);
      |                   ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 55 ms 23660 KB Output is correct
2 Correct 13 ms 20332 KB Output is correct
3 Correct 13 ms 20332 KB Output is correct
4 Correct 13 ms 20332 KB Output is correct
5 Correct 14 ms 20332 KB Output is correct
6 Correct 14 ms 20332 KB Output is correct
7 Correct 14 ms 20332 KB Output is correct
8 Correct 52 ms 24684 KB Output is correct
9 Correct 54 ms 24428 KB Output is correct
10 Correct 21 ms 20588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 86 ms 23532 KB Output is correct
2 Correct 15 ms 20332 KB Output is correct
3 Correct 13 ms 20332 KB Output is correct
4 Correct 13 ms 20332 KB Output is correct
5 Correct 13 ms 20332 KB Output is correct
6 Correct 17 ms 20332 KB Output is correct
7 Correct 17 ms 20460 KB Output is correct
8 Correct 17 ms 20460 KB Output is correct
9 Correct 58 ms 24556 KB Output is correct
10 Correct 144 ms 27628 KB Output is correct
11 Correct 85 ms 25324 KB Output is correct
12 Correct 80 ms 24684 KB Output is correct
13 Correct 124 ms 28268 KB Output is correct
14 Correct 80 ms 25068 KB Output is correct
15 Correct 65 ms 24172 KB Output is correct
16 Correct 64 ms 24172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 20332 KB Output is correct
2 Correct 13 ms 20332 KB Output is correct
3 Correct 13 ms 20328 KB Output is correct
4 Correct 13 ms 20332 KB Output is correct
5 Correct 13 ms 20332 KB Output is correct
6 Correct 14 ms 20332 KB Output is correct
7 Correct 14 ms 20332 KB Output is correct
8 Correct 16 ms 20588 KB Output is correct
9 Correct 15 ms 20460 KB Output is correct
10 Correct 45 ms 24428 KB Output is correct
11 Correct 81 ms 25068 KB Output is correct
12 Correct 127 ms 27448 KB Output is correct
13 Correct 137 ms 28256 KB Output is correct
14 Correct 95 ms 26348 KB Output is correct
15 Correct 67 ms 24192 KB Output is correct
16 Correct 64 ms 24172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 20332 KB Output is correct
2 Correct 13 ms 20332 KB Output is correct
3 Correct 13 ms 20328 KB Output is correct
4 Correct 13 ms 20332 KB Output is correct
5 Correct 13 ms 20332 KB Output is correct
6 Correct 14 ms 20332 KB Output is correct
7 Correct 14 ms 20332 KB Output is correct
8 Correct 16 ms 20588 KB Output is correct
9 Correct 15 ms 20460 KB Output is correct
10 Correct 45 ms 24428 KB Output is correct
11 Correct 81 ms 25068 KB Output is correct
12 Correct 127 ms 27448 KB Output is correct
13 Correct 137 ms 28256 KB Output is correct
14 Correct 95 ms 26348 KB Output is correct
15 Correct 67 ms 24192 KB Output is correct
16 Correct 64 ms 24172 KB Output is correct
17 Correct 82 ms 25196 KB Output is correct
18 Correct 13 ms 20332 KB Output is correct
19 Correct 13 ms 20332 KB Output is correct
20 Correct 13 ms 20332 KB Output is correct
21 Correct 13 ms 20332 KB Output is correct
22 Correct 13 ms 20332 KB Output is correct
23 Correct 15 ms 20460 KB Output is correct
24 Correct 16 ms 20588 KB Output is correct
25 Correct 17 ms 20588 KB Output is correct
26 Correct 16 ms 20460 KB Output is correct
27 Correct 48 ms 24428 KB Output is correct
28 Correct 148 ms 27500 KB Output is correct
29 Correct 138 ms 28268 KB Output is correct
30 Correct 111 ms 26476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 23660 KB Output is correct
2 Correct 13 ms 20332 KB Output is correct
3 Correct 13 ms 20332 KB Output is correct
4 Correct 13 ms 20332 KB Output is correct
5 Correct 14 ms 20332 KB Output is correct
6 Correct 14 ms 20332 KB Output is correct
7 Correct 14 ms 20332 KB Output is correct
8 Correct 52 ms 24684 KB Output is correct
9 Correct 54 ms 24428 KB Output is correct
10 Correct 21 ms 20588 KB Output is correct
11 Correct 86 ms 23532 KB Output is correct
12 Correct 15 ms 20332 KB Output is correct
13 Correct 13 ms 20332 KB Output is correct
14 Correct 13 ms 20332 KB Output is correct
15 Correct 13 ms 20332 KB Output is correct
16 Correct 17 ms 20332 KB Output is correct
17 Correct 17 ms 20460 KB Output is correct
18 Correct 17 ms 20460 KB Output is correct
19 Correct 58 ms 24556 KB Output is correct
20 Correct 144 ms 27628 KB Output is correct
21 Correct 85 ms 25324 KB Output is correct
22 Correct 80 ms 24684 KB Output is correct
23 Correct 124 ms 28268 KB Output is correct
24 Correct 80 ms 25068 KB Output is correct
25 Correct 65 ms 24172 KB Output is correct
26 Correct 64 ms 24172 KB Output is correct
27 Correct 14 ms 20332 KB Output is correct
28 Correct 13 ms 20332 KB Output is correct
29 Correct 13 ms 20328 KB Output is correct
30 Correct 13 ms 20332 KB Output is correct
31 Correct 13 ms 20332 KB Output is correct
32 Correct 14 ms 20332 KB Output is correct
33 Correct 14 ms 20332 KB Output is correct
34 Correct 16 ms 20588 KB Output is correct
35 Correct 15 ms 20460 KB Output is correct
36 Correct 45 ms 24428 KB Output is correct
37 Correct 81 ms 25068 KB Output is correct
38 Correct 127 ms 27448 KB Output is correct
39 Correct 137 ms 28256 KB Output is correct
40 Correct 95 ms 26348 KB Output is correct
41 Correct 67 ms 24192 KB Output is correct
42 Correct 64 ms 24172 KB Output is correct
43 Correct 82 ms 25196 KB Output is correct
44 Correct 13 ms 20332 KB Output is correct
45 Correct 13 ms 20332 KB Output is correct
46 Correct 13 ms 20332 KB Output is correct
47 Correct 13 ms 20332 KB Output is correct
48 Correct 13 ms 20332 KB Output is correct
49 Correct 15 ms 20460 KB Output is correct
50 Correct 16 ms 20588 KB Output is correct
51 Correct 17 ms 20588 KB Output is correct
52 Correct 16 ms 20460 KB Output is correct
53 Correct 48 ms 24428 KB Output is correct
54 Correct 148 ms 27500 KB Output is correct
55 Correct 138 ms 28268 KB Output is correct
56 Correct 111 ms 26476 KB Output is correct
57 Correct 170 ms 29036 KB Output is correct
58 Correct 55 ms 24556 KB Output is correct
59 Correct 84 ms 25324 KB Output is correct