Submission #553119

# Submission time Handle Problem Language Result Execution time Memory
553119 2022-04-24T17:12:21 Z Talha_Taki Toll (BOI17_toll) C++17
100 / 100
234 ms 23336 KB
#include <bits/stdc++.h>

using namespace std;


typedef long long ll;
typedef pair <int, int> pii;


int main(const int argc, const char *argv[]) {

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    const int INF = 1e9;
    int k, n, m, o, group;
    cin >> k >> n >> m >> o;
    group = n/k;

    vector <int> lg(group+1);
    lg[1] = 0;
    for(int i = 2; i <= group; ++i) {
        lg[i] = lg[i>>1] + 1;
    }
    vector <vector <vector <vector <int>>>> dp(group+1, vector <vector <vector <int>>> (k, vector <vector <int>> (k, vector <int> (lg[group]+1, INF))));

    for(int i = 0; i < m; ++i) {
        int a, b, w;
        cin >> a >> b >> w;
        dp[a/k][a%k][b%k][0] = w;
    }

    for(int l = 1; l <= lg[group]; ++l) {
        for(int i = 0; i <= group; ++i) {
            for(int y = 0; y < k; ++y) {
                for(int z = 0; z < k; ++z) {
                    for(int mid = 0; mid < k; ++mid) {
                        int next = i + (1<<(l-1));
                        if (next > group) continue;
                        dp[i][y][z][l] = min(dp[i][y][z][l], dp[i][y][mid][l-1] + dp[next][mid][z][l-1]);
                    }
                }
            }
        }
    }

    while (o--) {
        int a, b;
        cin >> a >> b;

        int group_a = a/k;
        int group_b = b/k;
        int left = group_b - group_a;
        int source = a % k;
        int destination = __builtin_popcount(left)*k + b % k;
        int node_cnt = (__builtin_popcount(left) + 1) * k;

        vector <vector <pii>> adj(node_cnt);
        vector <int> indegree(node_cnt, 0);

        int ptr = 0;
        for(int l = lg[group]; l >= 0; --l) {
            if ((1<<l) <= left) {
                left -= (1<<l);
                for(int y = 0; y < k; ++y) {
                    int u = ptr*k + y;
                    for(int z = 0; z < k; ++z) {
                        int v = (ptr+1)*k + z;
                        assert(v < node_cnt);
                        adj[u].push_back({v, dp[group_a][y][z][l]});
                        indegree[v]++;
                    }
                }
                group_a += (1<<l);
                ptr++;
            }
        }

        queue <int> Q;
        vector <int> dist(node_cnt, INF);

        for(int i = 0; i < k; ++i) Q.push(i);
        dist[source] = 0;

        while (!Q.empty()) {
            int u = Q.front();
            Q.pop();

            for(auto edge : adj[u]) {
                int v = edge.first;
                int w = edge.second;

                dist[v] = min(dist[v],  dist[u] + w);
                indegree[v]--;

                if (indegree[v] == 0) Q.push(v);
            }
        }

        cout << (dist[destination] == INF? -1LL:dist[destination]) << '\n';
    }


    return 0;
}



# Verdict Execution time Memory Grader output
1 Correct 46 ms 8832 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 324 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 2 ms 596 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 2 ms 536 KB Output is correct
8 Correct 58 ms 9652 KB Output is correct
9 Correct 49 ms 9632 KB Output is correct
10 Correct 31 ms 8804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 91 ms 13524 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 320 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 212 KB Output is correct
7 Correct 8 ms 532 KB Output is correct
8 Correct 13 ms 656 KB Output is correct
9 Correct 48 ms 9648 KB Output is correct
10 Correct 162 ms 20008 KB Output is correct
11 Correct 96 ms 15204 KB Output is correct
12 Correct 128 ms 19020 KB Output is correct
13 Correct 195 ms 16820 KB Output is correct
14 Correct 80 ms 10756 KB Output is correct
15 Correct 125 ms 15624 KB Output is correct
16 Correct 128 ms 15596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 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 324 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 4 ms 720 KB Output is correct
9 Correct 3 ms 596 KB Output is correct
10 Correct 40 ms 9496 KB Output is correct
11 Correct 73 ms 15052 KB Output is correct
12 Correct 128 ms 19908 KB Output is correct
13 Correct 126 ms 20140 KB Output is correct
14 Correct 112 ms 19524 KB Output is correct
15 Correct 115 ms 15564 KB Output is correct
16 Correct 115 ms 15536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 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 324 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 4 ms 720 KB Output is correct
9 Correct 3 ms 596 KB Output is correct
10 Correct 40 ms 9496 KB Output is correct
11 Correct 73 ms 15052 KB Output is correct
12 Correct 128 ms 19908 KB Output is correct
13 Correct 126 ms 20140 KB Output is correct
14 Correct 112 ms 19524 KB Output is correct
15 Correct 115 ms 15564 KB Output is correct
16 Correct 115 ms 15536 KB Output is correct
17 Correct 83 ms 15072 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 3 ms 468 KB Output is correct
24 Correct 6 ms 596 KB Output is correct
25 Correct 17 ms 800 KB Output is correct
26 Correct 9 ms 736 KB Output is correct
27 Correct 40 ms 9584 KB Output is correct
28 Correct 133 ms 19900 KB Output is correct
29 Correct 139 ms 20148 KB Output is correct
30 Correct 134 ms 19512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 8832 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 324 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 2 ms 596 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 2 ms 536 KB Output is correct
8 Correct 58 ms 9652 KB Output is correct
9 Correct 49 ms 9632 KB Output is correct
10 Correct 31 ms 8804 KB Output is correct
11 Correct 91 ms 13524 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 320 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 212 KB Output is correct
17 Correct 8 ms 532 KB Output is correct
18 Correct 13 ms 656 KB Output is correct
19 Correct 48 ms 9648 KB Output is correct
20 Correct 162 ms 20008 KB Output is correct
21 Correct 96 ms 15204 KB Output is correct
22 Correct 128 ms 19020 KB Output is correct
23 Correct 195 ms 16820 KB Output is correct
24 Correct 80 ms 10756 KB Output is correct
25 Correct 125 ms 15624 KB Output is correct
26 Correct 128 ms 15596 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 324 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 1 ms 468 KB Output is correct
34 Correct 4 ms 720 KB Output is correct
35 Correct 3 ms 596 KB Output is correct
36 Correct 40 ms 9496 KB Output is correct
37 Correct 73 ms 15052 KB Output is correct
38 Correct 128 ms 19908 KB Output is correct
39 Correct 126 ms 20140 KB Output is correct
40 Correct 112 ms 19524 KB Output is correct
41 Correct 115 ms 15564 KB Output is correct
42 Correct 115 ms 15536 KB Output is correct
43 Correct 83 ms 15072 KB Output is correct
44 Correct 0 ms 212 KB Output is correct
45 Correct 1 ms 212 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 1 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 3 ms 468 KB Output is correct
50 Correct 6 ms 596 KB Output is correct
51 Correct 17 ms 800 KB Output is correct
52 Correct 9 ms 736 KB Output is correct
53 Correct 40 ms 9584 KB Output is correct
54 Correct 133 ms 19900 KB Output is correct
55 Correct 139 ms 20148 KB Output is correct
56 Correct 134 ms 19512 KB Output is correct
57 Correct 234 ms 23336 KB Output is correct
58 Correct 58 ms 9664 KB Output is correct
59 Correct 130 ms 15260 KB Output is correct