Submission #960104

# Submission time Handle Problem Language Result Execution time Memory
960104 2024-04-09T16:27:45 Z sheldon Toll (BOI17_toll) C++14
100 / 100
280 ms 160340 KB
#include <bits/stdc++.h>

using namespace std;
#define int long long
const int nax = 5e4 + 5;


int dp[nax][16][5][5];

void combine (int first[5][5], int second[5][5], int third[5][5]) {
    for (int i = 0; i < 5; ++i) {
        for (int j = 0; j < 5; ++j) {
            for (int k = 0; k < 5; ++k) {
                third[i][k] = min (third[i][k], first[i][j] + second[j][k]); 
            }
        }
    }
}

void solve() {
    int k, n, m, q;
    cin >> k >> n >> m >> q;
    memset (dp, 0x3f, sizeof (dp));
    for (int i = 0; i < m; ++i) {
        int a, b, t;
        cin >> a >> b >> t;
        int loca = a / k, locb =  b / k;
        dp[loca][0][a % k][b % k] = min (dp[loca][0][a % k][b % k], t);
    }
    for (int bit = 1; bit < 16; ++bit) {
        for (int i = (n - 1) / k; i >= 0; --i) {
            if (i + (1 << bit) > (n - 1) / k) continue;

            combine (dp[i][bit - 1], dp[i + (1 << (bit - 1))][bit - 1], dp[i][bit]);
        }
    }
    while (q--) {
        int a, b;
        cin >> a >> b;
        int dist = b / k - a / k;
        int loca = a % k, locb = b % k;
        int at[5];
        memset (at, 0x3f, sizeof (at));
        bool found = 0;
        int node = a / k;
        for (int bit = 0; bit < 16; ++bit) {
            int new_dp[5];
            memset (new_dp, 0x3f, sizeof (new_dp));
            if (dist & (1 << bit)) {
                if (!found) {
                    for (int i = 0; i < k; ++i) {
                        at[i] = dp[a / k][bit][loca][i];
                    }
                    found = 1;
                } else {
                    for (int i = 0; i < k; ++i) {
                        for (int j = 0; j < k; ++j) {
                            new_dp[i] = min (new_dp[i], at[j] + dp[node][bit][j][i]);
                        }
                    }
                    for (int i = 0; i < k; ++i) {
                        at[i] = new_dp[i];
                    }
                }
                node += (1 << bit);
            }
        }
        assert (node == b / k);
        cout << (at[locb] >= 1e9 ? -1 : at[locb]) << '\n';
    }
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    solve();
}

Compilation message

toll.cpp: In function 'void solve()':
toll.cpp:27:27: warning: unused variable 'locb' [-Wunused-variable]
   27 |         int loca = a / k, locb =  b / k;
      |                           ^~~~
# Verdict Execution time Memory Grader output
1 Correct 266 ms 156952 KB Output is correct
2 Correct 48 ms 157012 KB Output is correct
3 Correct 47 ms 156908 KB Output is correct
4 Correct 54 ms 157060 KB Output is correct
5 Correct 50 ms 157012 KB Output is correct
6 Correct 49 ms 156916 KB Output is correct
7 Correct 50 ms 156960 KB Output is correct
8 Correct 263 ms 157888 KB Output is correct
9 Correct 252 ms 157776 KB Output is correct
10 Correct 245 ms 157012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 168 ms 157180 KB Output is correct
2 Correct 48 ms 156756 KB Output is correct
3 Correct 49 ms 157016 KB Output is correct
4 Correct 48 ms 156752 KB Output is correct
5 Correct 47 ms 156756 KB Output is correct
6 Correct 48 ms 156756 KB Output is correct
7 Correct 51 ms 157008 KB Output is correct
8 Correct 52 ms 157024 KB Output is correct
9 Correct 257 ms 157008 KB Output is correct
10 Correct 131 ms 159168 KB Output is correct
11 Correct 173 ms 158696 KB Output is correct
12 Correct 127 ms 158288 KB Output is correct
13 Correct 96 ms 159568 KB Output is correct
14 Correct 88 ms 158460 KB Output is correct
15 Correct 75 ms 158172 KB Output is correct
16 Correct 75 ms 158036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 156932 KB Output is correct
2 Correct 48 ms 156756 KB Output is correct
3 Correct 49 ms 156820 KB Output is correct
4 Correct 56 ms 156756 KB Output is correct
5 Correct 49 ms 156752 KB Output is correct
6 Correct 49 ms 156968 KB Output is correct
7 Correct 50 ms 157268 KB Output is correct
8 Correct 50 ms 156808 KB Output is correct
9 Correct 48 ms 156760 KB Output is correct
10 Correct 256 ms 157004 KB Output is correct
11 Correct 154 ms 158548 KB Output is correct
12 Correct 126 ms 159180 KB Output is correct
13 Correct 127 ms 159316 KB Output is correct
14 Correct 123 ms 158800 KB Output is correct
15 Correct 73 ms 158120 KB Output is correct
16 Correct 73 ms 158036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 156932 KB Output is correct
2 Correct 48 ms 156756 KB Output is correct
3 Correct 49 ms 156820 KB Output is correct
4 Correct 56 ms 156756 KB Output is correct
5 Correct 49 ms 156752 KB Output is correct
6 Correct 49 ms 156968 KB Output is correct
7 Correct 50 ms 157268 KB Output is correct
8 Correct 50 ms 156808 KB Output is correct
9 Correct 48 ms 156760 KB Output is correct
10 Correct 256 ms 157004 KB Output is correct
11 Correct 154 ms 158548 KB Output is correct
12 Correct 126 ms 159180 KB Output is correct
13 Correct 127 ms 159316 KB Output is correct
14 Correct 123 ms 158800 KB Output is correct
15 Correct 73 ms 158120 KB Output is correct
16 Correct 73 ms 158036 KB Output is correct
17 Correct 164 ms 158568 KB Output is correct
18 Correct 46 ms 157016 KB Output is correct
19 Correct 50 ms 156832 KB Output is correct
20 Correct 48 ms 157012 KB Output is correct
21 Correct 52 ms 156852 KB Output is correct
22 Correct 47 ms 156940 KB Output is correct
23 Correct 50 ms 157008 KB Output is correct
24 Correct 48 ms 156844 KB Output is correct
25 Correct 50 ms 157008 KB Output is correct
26 Correct 49 ms 157080 KB Output is correct
27 Correct 261 ms 157848 KB Output is correct
28 Correct 125 ms 159056 KB Output is correct
29 Correct 130 ms 159476 KB Output is correct
30 Correct 146 ms 158652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 266 ms 156952 KB Output is correct
2 Correct 48 ms 157012 KB Output is correct
3 Correct 47 ms 156908 KB Output is correct
4 Correct 54 ms 157060 KB Output is correct
5 Correct 50 ms 157012 KB Output is correct
6 Correct 49 ms 156916 KB Output is correct
7 Correct 50 ms 156960 KB Output is correct
8 Correct 263 ms 157888 KB Output is correct
9 Correct 252 ms 157776 KB Output is correct
10 Correct 245 ms 157012 KB Output is correct
11 Correct 168 ms 157180 KB Output is correct
12 Correct 48 ms 156756 KB Output is correct
13 Correct 49 ms 157016 KB Output is correct
14 Correct 48 ms 156752 KB Output is correct
15 Correct 47 ms 156756 KB Output is correct
16 Correct 48 ms 156756 KB Output is correct
17 Correct 51 ms 157008 KB Output is correct
18 Correct 52 ms 157024 KB Output is correct
19 Correct 257 ms 157008 KB Output is correct
20 Correct 131 ms 159168 KB Output is correct
21 Correct 173 ms 158696 KB Output is correct
22 Correct 127 ms 158288 KB Output is correct
23 Correct 96 ms 159568 KB Output is correct
24 Correct 88 ms 158460 KB Output is correct
25 Correct 75 ms 158172 KB Output is correct
26 Correct 75 ms 158036 KB Output is correct
27 Correct 47 ms 156932 KB Output is correct
28 Correct 48 ms 156756 KB Output is correct
29 Correct 49 ms 156820 KB Output is correct
30 Correct 56 ms 156756 KB Output is correct
31 Correct 49 ms 156752 KB Output is correct
32 Correct 49 ms 156968 KB Output is correct
33 Correct 50 ms 157268 KB Output is correct
34 Correct 50 ms 156808 KB Output is correct
35 Correct 48 ms 156760 KB Output is correct
36 Correct 256 ms 157004 KB Output is correct
37 Correct 154 ms 158548 KB Output is correct
38 Correct 126 ms 159180 KB Output is correct
39 Correct 127 ms 159316 KB Output is correct
40 Correct 123 ms 158800 KB Output is correct
41 Correct 73 ms 158120 KB Output is correct
42 Correct 73 ms 158036 KB Output is correct
43 Correct 164 ms 158568 KB Output is correct
44 Correct 46 ms 157016 KB Output is correct
45 Correct 50 ms 156832 KB Output is correct
46 Correct 48 ms 157012 KB Output is correct
47 Correct 52 ms 156852 KB Output is correct
48 Correct 47 ms 156940 KB Output is correct
49 Correct 50 ms 157008 KB Output is correct
50 Correct 48 ms 156844 KB Output is correct
51 Correct 50 ms 157008 KB Output is correct
52 Correct 49 ms 157080 KB Output is correct
53 Correct 261 ms 157848 KB Output is correct
54 Correct 125 ms 159056 KB Output is correct
55 Correct 130 ms 159476 KB Output is correct
56 Correct 146 ms 158652 KB Output is correct
57 Correct 128 ms 160340 KB Output is correct
58 Correct 280 ms 158068 KB Output is correct
59 Correct 167 ms 158796 KB Output is correct