Submission #464067

# Submission time Handle Problem Language Result Execution time Memory
464067 2021-08-12T10:34:14 Z deinfreund Toll (BOI17_toll) C++17
100 / 100
1032 ms 30992 KB
#include <bits/stdc++.h>
#define int int64_t

using namespace std;

vector<map<int, int, greater<int>>> graph; // map<target, distance>
int K;
const int INF = 1e10;

int findShortestDistance(int s, int e) {
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
    unordered_map<int, int> distance;
    pq.emplace(0, s);
    while (!pq.empty()){
        auto [cost, pos] = pq.top();
        pq.pop();
        if (distance.find(pos) != distance.end()) continue;
        distance[pos] = cost;
        int k = -1;
        for (const auto& [p, c] : graph[pos]) {
            if (p <= e) { // can't go backwards due to graph structure
                if (k == -1) k = p / K;
                if (k != p / K) break;
                pq.emplace(cost + c, p);
            }
        }
    }
    return distance.find(e) == distance.end() ? INF : distance.at(e);
}

void addSkipEdges(int skipDistance) {
    vector<tuple<int, int, int>> edgesToAdd;
    for (int startCluster = 0; startCluster < graph.size() / K; startCluster+=skipDistance) {
        for (int startNode = startCluster * K; startNode < (startCluster + 1) * K; startNode++) {
            for (int endNode = (startCluster + skipDistance) * K; endNode < (startCluster + skipDistance + 1) * K; endNode++) {
                if (endNode >= graph.size()) break;
                edgesToAdd.emplace_back(startNode, endNode, findShortestDistance(startNode, endNode));
            }
        }
    }
    for (const auto& [startNode, endNode, distance] : edgesToAdd) {
        graph[startNode][endNode] = distance;
    }
}

main() {
    int N,M,O;
    cin >> K >> N >> M >> O;
    graph.resize(N);
    for (int i = 0; i < M; i++) {
        int s, e, c;
        cin >> s >> e >> c;
        graph[s][e] = c;
    }
    for (int skipDist = 1; skipDist < N / K; skipDist *= 2) {
        addSkipEdges(skipDist);
    }
    for (int o = 0; o < O; o++) {
        int s, e;
        cin >> s >> e;
        int dist = findShortestDistance(s, e);
        cout << (dist < INF ? dist : -1) << "\n";
    }
}

Compilation message

toll.cpp: In function 'void addSkipEdges(int64_t)':
toll.cpp:33:45: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'long unsigned int' [-Wsign-compare]
   33 |     for (int startCluster = 0; startCluster < graph.size() / K; startCluster+=skipDistance) {
      |                                ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
toll.cpp:36:29: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<std::map<long int, long int, std::greater<long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   36 |                 if (endNode >= graph.size()) break;
      |                     ~~~~~~~~^~~~~~~~~~~~~~~
toll.cpp: At global scope:
toll.cpp:46:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   46 | main() {
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 178 ms 9876 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 292 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 6 ms 460 KB Output is correct
6 Correct 6 ms 460 KB Output is correct
7 Correct 8 ms 460 KB Output is correct
8 Correct 180 ms 9844 KB Output is correct
9 Correct 173 ms 9692 KB Output is correct
10 Correct 102 ms 8968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 350 ms 16780 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 33 ms 460 KB Output is correct
8 Correct 45 ms 672 KB Output is correct
9 Correct 154 ms 9776 KB Output is correct
10 Correct 598 ms 23804 KB Output is correct
11 Correct 366 ms 16812 KB Output is correct
12 Correct 494 ms 22852 KB Output is correct
13 Correct 764 ms 22828 KB Output is correct
14 Correct 313 ms 14444 KB Output is correct
15 Correct 605 ms 21684 KB Output is correct
16 Correct 619 ms 21716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 268 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 3 ms 460 KB Output is correct
7 Correct 7 ms 588 KB Output is correct
8 Correct 20 ms 1068 KB Output is correct
9 Correct 17 ms 808 KB Output is correct
10 Correct 119 ms 9632 KB Output is correct
11 Correct 282 ms 16620 KB Output is correct
12 Correct 479 ms 23520 KB Output is correct
13 Correct 515 ms 23840 KB Output is correct
14 Correct 454 ms 23132 KB Output is correct
15 Correct 563 ms 21644 KB Output is correct
16 Correct 550 ms 21776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 268 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 3 ms 460 KB Output is correct
7 Correct 7 ms 588 KB Output is correct
8 Correct 20 ms 1068 KB Output is correct
9 Correct 17 ms 808 KB Output is correct
10 Correct 119 ms 9632 KB Output is correct
11 Correct 282 ms 16620 KB Output is correct
12 Correct 479 ms 23520 KB Output is correct
13 Correct 515 ms 23840 KB Output is correct
14 Correct 454 ms 23132 KB Output is correct
15 Correct 563 ms 21644 KB Output is correct
16 Correct 550 ms 21776 KB Output is correct
17 Correct 327 ms 16636 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 288 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 12 ms 460 KB Output is correct
24 Correct 20 ms 664 KB Output is correct
25 Correct 62 ms 1040 KB Output is correct
26 Correct 42 ms 828 KB Output is correct
27 Correct 130 ms 9680 KB Output is correct
28 Correct 522 ms 23808 KB Output is correct
29 Correct 548 ms 23892 KB Output is correct
30 Correct 523 ms 23272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 178 ms 9876 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 292 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 6 ms 460 KB Output is correct
6 Correct 6 ms 460 KB Output is correct
7 Correct 8 ms 460 KB Output is correct
8 Correct 180 ms 9844 KB Output is correct
9 Correct 173 ms 9692 KB Output is correct
10 Correct 102 ms 8968 KB Output is correct
11 Correct 350 ms 16780 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 33 ms 460 KB Output is correct
18 Correct 45 ms 672 KB Output is correct
19 Correct 154 ms 9776 KB Output is correct
20 Correct 598 ms 23804 KB Output is correct
21 Correct 366 ms 16812 KB Output is correct
22 Correct 494 ms 22852 KB Output is correct
23 Correct 764 ms 22828 KB Output is correct
24 Correct 313 ms 14444 KB Output is correct
25 Correct 605 ms 21684 KB Output is correct
26 Correct 619 ms 21716 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 268 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 204 KB Output is correct
32 Correct 3 ms 460 KB Output is correct
33 Correct 7 ms 588 KB Output is correct
34 Correct 20 ms 1068 KB Output is correct
35 Correct 17 ms 808 KB Output is correct
36 Correct 119 ms 9632 KB Output is correct
37 Correct 282 ms 16620 KB Output is correct
38 Correct 479 ms 23520 KB Output is correct
39 Correct 515 ms 23840 KB Output is correct
40 Correct 454 ms 23132 KB Output is correct
41 Correct 563 ms 21644 KB Output is correct
42 Correct 550 ms 21776 KB Output is correct
43 Correct 327 ms 16636 KB Output is correct
44 Correct 1 ms 204 KB Output is correct
45 Correct 1 ms 204 KB Output is correct
46 Correct 1 ms 288 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 12 ms 460 KB Output is correct
50 Correct 20 ms 664 KB Output is correct
51 Correct 62 ms 1040 KB Output is correct
52 Correct 42 ms 828 KB Output is correct
53 Correct 130 ms 9680 KB Output is correct
54 Correct 522 ms 23808 KB Output is correct
55 Correct 548 ms 23892 KB Output is correct
56 Correct 523 ms 23272 KB Output is correct
57 Correct 1032 ms 30992 KB Output is correct
58 Correct 170 ms 9884 KB Output is correct
59 Correct 368 ms 16848 KB Output is correct