Submission #707187

# Submission time Handle Problem Language Result Execution time Memory
707187 2023-03-08T14:59:14 Z thimote75 Toll (BOI17_toll) C++14
100 / 100
231 ms 169144 KB
#include <bits/stdc++.h>

using namespace std;

#define num long long

int nbNodes, nbRoads, bucketSize, nbQuery;
int bucketCount;

#define MAX_BUCKET_SIZE 5
#define MAX_BUCKET_GRID 25

#define LINF 1e17

struct AdjGraph {
    num grid[MAX_BUCKET_SIZE][MAX_BUCKET_SIZE];

    AdjGraph () {
        for (int a = 0; a < bucketSize; a ++)
            for (int b = 0; b < bucketSize; b ++)
                grid[a][b] = LINF;
    }
    void identity () {
        for (int a = 0; a < bucketSize; a ++)
            for (int b = 0; b < bucketSize; b ++)
                grid[a][b] = a == b ? 0 : LINF;
    }

    AdjGraph compile (AdjGraph &other) {
        AdjGraph res;

        for (int a = 0; a < bucketSize; a ++) {
            for (int b = 0; b < bucketSize; b ++) {
                num minValue = LINF;

                for (int i = 0; i < bucketSize; i ++) {
                    minValue = min(
                        minValue,
                        grid[a][i] + other.grid[i][b]
                    );
                }

                res.grid[a][b] = minValue;
            }
        }

        return res;
    }
};

vector<vector<AdjGraph>> displacement_2k;

int getBucket (int node) {
    return node / bucketSize;
}
int getBucketId(int node) {
    return node % bucketSize;
}

AdjGraph compile (int node, int delta) {
    AdjGraph left;
    left.identity();

    int h = displacement_2k[0].size();
    while (h >= 1) {
        h --;
        if (((1 << h) & delta) == 0) continue ;

        left  = left.compile(displacement_2k[node][h]);
        node += 1 << h;
    }

    return left;
}

num solve (int a, int b) {
    int ba = getBucket(a), bb = getBucket(b);

    //bb --;
    AdjGraph graph = compile(ba, bb - ba);

    return graph.grid[getBucketId(a)][getBucketId(b)];
}

int main () {
    cin >> bucketSize >> nbNodes >> nbRoads >> nbQuery;
    bucketCount = nbNodes / bucketSize;

    displacement_2k.resize(bucketCount, vector<AdjGraph>(ceil(log2(bucketCount)) + 1));
    
    for (int idRoad = 0; idRoad < nbRoads; idRoad ++) {
        int a, b;
        cin >> a >> b;

        int ba = getBucket(a);
        int bb = getBucket(b);

        cin >> displacement_2k[ba][0].grid[getBucketId(a)][getBucketId(b)];
    }

    for (int h = 0; h + 1 < displacement_2k[0].size(); h ++) {
        int delta = 1 << h;

        for (int id = 0; id + delta < bucketCount; id ++) {
            displacement_2k[id][h + 1]
             = displacement_2k[id][h].compile(displacement_2k[id + delta][h]);
        }
    }

    for (int q = 0; q < nbQuery; q ++) {
        int a, b;
        cin >> a >> b;

        num res = solve(a, b);
        if (res == LINF) cout << -1 << endl;
        else cout << res << endl;
    }
}

Compilation message

toll.cpp: In function 'int main()':
toll.cpp:97:13: warning: unused variable 'bb' [-Wunused-variable]
   97 |         int bb = getBucket(b);
      |             ^~
toll.cpp:102:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<AdjGraph>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  102 |     for (int h = 0; h + 1 < displacement_2k[0].size(); h ++) {
      |                     ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 167 ms 169132 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 4 ms 2388 KB Output is correct
6 Correct 4 ms 2388 KB Output is correct
7 Correct 4 ms 2388 KB Output is correct
8 Correct 164 ms 169132 KB Output is correct
9 Correct 179 ms 169120 KB Output is correct
10 Correct 140 ms 168312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 158 ms 81308 KB Output is correct
2 Correct 1 ms 304 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 19 ms 2584 KB Output is correct
8 Correct 21 ms 1428 KB Output is correct
9 Correct 184 ms 169076 KB Output is correct
10 Correct 180 ms 55524 KB Output is correct
11 Correct 157 ms 81320 KB Output is correct
12 Correct 132 ms 54392 KB Output is correct
13 Correct 144 ms 19260 KB Output is correct
14 Correct 98 ms 31352 KB Output is correct
15 Correct 87 ms 18108 KB Output is correct
16 Correct 83 ms 18112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 296 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 300 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 3 ms 2388 KB Output is correct
7 Correct 2 ms 1320 KB Output is correct
8 Correct 4 ms 696 KB Output is correct
9 Correct 3 ms 724 KB Output is correct
10 Correct 155 ms 169032 KB Output is correct
11 Correct 138 ms 81052 KB Output is correct
12 Correct 159 ms 55284 KB Output is correct
13 Correct 176 ms 55588 KB Output is correct
14 Correct 140 ms 54860 KB Output is correct
15 Correct 78 ms 18116 KB Output is correct
16 Correct 76 ms 18100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 296 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 300 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 3 ms 2388 KB Output is correct
7 Correct 2 ms 1320 KB Output is correct
8 Correct 4 ms 696 KB Output is correct
9 Correct 3 ms 724 KB Output is correct
10 Correct 155 ms 169032 KB Output is correct
11 Correct 138 ms 81052 KB Output is correct
12 Correct 159 ms 55284 KB Output is correct
13 Correct 176 ms 55588 KB Output is correct
14 Correct 140 ms 54860 KB Output is correct
15 Correct 78 ms 18116 KB Output is correct
16 Correct 76 ms 18100 KB Output is correct
17 Correct 155 ms 81192 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 0 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 7 ms 2516 KB Output is correct
24 Correct 11 ms 1372 KB Output is correct
25 Correct 12 ms 696 KB Output is correct
26 Correct 9 ms 724 KB Output is correct
27 Correct 162 ms 169032 KB Output is correct
28 Correct 165 ms 55288 KB Output is correct
29 Correct 172 ms 55584 KB Output is correct
30 Correct 153 ms 55044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 167 ms 169132 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 4 ms 2388 KB Output is correct
6 Correct 4 ms 2388 KB Output is correct
7 Correct 4 ms 2388 KB Output is correct
8 Correct 164 ms 169132 KB Output is correct
9 Correct 179 ms 169120 KB Output is correct
10 Correct 140 ms 168312 KB Output is correct
11 Correct 158 ms 81308 KB Output is correct
12 Correct 1 ms 304 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 19 ms 2584 KB Output is correct
18 Correct 21 ms 1428 KB Output is correct
19 Correct 184 ms 169076 KB Output is correct
20 Correct 180 ms 55524 KB Output is correct
21 Correct 157 ms 81320 KB Output is correct
22 Correct 132 ms 54392 KB Output is correct
23 Correct 144 ms 19260 KB Output is correct
24 Correct 98 ms 31352 KB Output is correct
25 Correct 87 ms 18108 KB Output is correct
26 Correct 83 ms 18112 KB Output is correct
27 Correct 1 ms 296 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 0 ms 300 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 3 ms 2388 KB Output is correct
33 Correct 2 ms 1320 KB Output is correct
34 Correct 4 ms 696 KB Output is correct
35 Correct 3 ms 724 KB Output is correct
36 Correct 155 ms 169032 KB Output is correct
37 Correct 138 ms 81052 KB Output is correct
38 Correct 159 ms 55284 KB Output is correct
39 Correct 176 ms 55588 KB Output is correct
40 Correct 140 ms 54860 KB Output is correct
41 Correct 78 ms 18116 KB Output is correct
42 Correct 76 ms 18100 KB Output is correct
43 Correct 155 ms 81192 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 0 ms 212 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 7 ms 2516 KB Output is correct
50 Correct 11 ms 1372 KB Output is correct
51 Correct 12 ms 696 KB Output is correct
52 Correct 9 ms 724 KB Output is correct
53 Correct 162 ms 169032 KB Output is correct
54 Correct 165 ms 55288 KB Output is correct
55 Correct 172 ms 55584 KB Output is correct
56 Correct 153 ms 55044 KB Output is correct
57 Correct 231 ms 40652 KB Output is correct
58 Correct 166 ms 169144 KB Output is correct
59 Correct 165 ms 81344 KB Output is correct