Submission #1052812

# Submission time Handle Problem Language Result Execution time Memory
1052812 2024-08-11T01:23:28 Z gyg Toll (BOI17_toll) C++17
100 / 100
2541 ms 42616 KB
#pragma GCC optimize("Ofast", "unroll-loops")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int MAX_N = 5e4 + 5, SQRT = 3e2, INF = 1e9;

int k, n, m, q;
array<vector<pii>, MAX_N> adj;

unordered_map<int, int> dist;
unordered_set<int> seen;
priority_queue<pii, vector<pii>, greater<pii>> order;
void dijk(int s, bool halt = false) {
    dist.clear(), seen.clear();
    dist[s] = 0, order.push({0, s});
    while (order.size()) {
        int u = order.top().second;
        order.pop();
        if (halt && (u / k) % SQRT == 0) continue;

        if (seen.count(u)) continue;
        seen.insert(u);

        for (pii x : adj[u]) {
            int new_dist = dist[u] + x.second;
            if (dist.count(x.first) && new_dist >= dist[x.first]) continue;
            dist[x.first] = new_dist;
            order.push({new_dist, x.first});
        }
    }
}

unordered_map<int, array<int, MAX_N>> sp;
void precomp() {
    for (int u = 0; u < n; u++) {
        if ((u / k) % SQRT != 0) continue;
        dijk(u);
        for (int v = 0; v < n; v++)
            sp[u][v] = (dist.count(v)) ? dist[v] : INF;
    }
}

int comp(int s, int f) {
    dijk(s, true);
    
    int ans = INF;
    if (dist.count(f)) ans = min(ans, dist[f]);
    for (pii x : dist)
        if ((x.first / k) % SQRT == 0)
            ans = min(ans, x.second + sp[x.first][f]);

    return ans;
}

int main() {
    // freopen("toll.in", "r", stdin);
    cin >> k >> n >> m >> q;
    for (int i = 1; i <= m; i++) {
        int u, v, w; cin >> u >> v >> w;
        adj[u].push_back({v, w});
    }

    precomp();

    for (int i = 1; i <= q; i++) {
        int s, f; cin >> s >> f;
        int ans = comp(s, f);
        cout << ((ans >= INF) ? -1 : ans) << '\n';
    }
}
# Verdict Execution time Memory Grader output
1 Correct 874 ms 40184 KB Output is correct
2 Correct 1 ms 1628 KB Output is correct
3 Correct 1 ms 1628 KB Output is correct
4 Correct 1 ms 1628 KB Output is correct
5 Correct 3 ms 2396 KB Output is correct
6 Correct 3 ms 2448 KB Output is correct
7 Correct 4 ms 2396 KB Output is correct
8 Correct 188 ms 36116 KB Output is correct
9 Correct 97 ms 35924 KB Output is correct
10 Correct 62 ms 34388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 504 ms 38412 KB Output is correct
2 Correct 1 ms 1624 KB Output is correct
3 Correct 1 ms 1884 KB Output is correct
4 Correct 1 ms 2176 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 16 ms 2460 KB Output is correct
8 Correct 16 ms 2648 KB Output is correct
9 Correct 105 ms 36088 KB Output is correct
10 Correct 1143 ms 41348 KB Output is correct
11 Correct 954 ms 40536 KB Output is correct
12 Correct 108 ms 36004 KB Output is correct
13 Correct 512 ms 26380 KB Output is correct
14 Correct 386 ms 25612 KB Output is correct
15 Correct 344 ms 23816 KB Output is correct
16 Correct 381 ms 24844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1624 KB Output is correct
2 Correct 1 ms 1884 KB Output is correct
3 Correct 1 ms 2140 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 5 ms 2396 KB Output is correct
8 Correct 20 ms 2652 KB Output is correct
9 Correct 12 ms 2392 KB Output is correct
10 Correct 85 ms 36176 KB Output is correct
11 Correct 748 ms 40344 KB Output is correct
12 Correct 930 ms 41016 KB Output is correct
13 Correct 931 ms 41320 KB Output is correct
14 Correct 789 ms 38444 KB Output is correct
15 Correct 343 ms 23988 KB Output is correct
16 Correct 318 ms 23920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1624 KB Output is correct
2 Correct 1 ms 1884 KB Output is correct
3 Correct 1 ms 2140 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 5 ms 2396 KB Output is correct
8 Correct 20 ms 2652 KB Output is correct
9 Correct 12 ms 2392 KB Output is correct
10 Correct 85 ms 36176 KB Output is correct
11 Correct 748 ms 40344 KB Output is correct
12 Correct 930 ms 41016 KB Output is correct
13 Correct 931 ms 41320 KB Output is correct
14 Correct 789 ms 38444 KB Output is correct
15 Correct 343 ms 23988 KB Output is correct
16 Correct 318 ms 23920 KB Output is correct
17 Correct 929 ms 40360 KB Output is correct
18 Correct 1 ms 1624 KB Output is correct
19 Correct 1 ms 1884 KB Output is correct
20 Correct 1 ms 2140 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 1 ms 2396 KB Output is correct
23 Correct 17 ms 2472 KB Output is correct
24 Correct 88 ms 2524 KB Output is correct
25 Correct 335 ms 2740 KB Output is correct
26 Correct 271 ms 2532 KB Output is correct
27 Correct 113 ms 36176 KB Output is correct
28 Correct 1226 ms 41076 KB Output is correct
29 Correct 1283 ms 41404 KB Output is correct
30 Correct 717 ms 38400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 874 ms 40184 KB Output is correct
2 Correct 1 ms 1628 KB Output is correct
3 Correct 1 ms 1628 KB Output is correct
4 Correct 1 ms 1628 KB Output is correct
5 Correct 3 ms 2396 KB Output is correct
6 Correct 3 ms 2448 KB Output is correct
7 Correct 4 ms 2396 KB Output is correct
8 Correct 188 ms 36116 KB Output is correct
9 Correct 97 ms 35924 KB Output is correct
10 Correct 62 ms 34388 KB Output is correct
11 Correct 504 ms 38412 KB Output is correct
12 Correct 1 ms 1624 KB Output is correct
13 Correct 1 ms 1884 KB Output is correct
14 Correct 1 ms 2176 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 16 ms 2460 KB Output is correct
18 Correct 16 ms 2648 KB Output is correct
19 Correct 105 ms 36088 KB Output is correct
20 Correct 1143 ms 41348 KB Output is correct
21 Correct 954 ms 40536 KB Output is correct
22 Correct 108 ms 36004 KB Output is correct
23 Correct 512 ms 26380 KB Output is correct
24 Correct 386 ms 25612 KB Output is correct
25 Correct 344 ms 23816 KB Output is correct
26 Correct 381 ms 24844 KB Output is correct
27 Correct 1 ms 1624 KB Output is correct
28 Correct 1 ms 1884 KB Output is correct
29 Correct 1 ms 2140 KB Output is correct
30 Correct 1 ms 2396 KB Output is correct
31 Correct 1 ms 2396 KB Output is correct
32 Correct 1 ms 2396 KB Output is correct
33 Correct 5 ms 2396 KB Output is correct
34 Correct 20 ms 2652 KB Output is correct
35 Correct 12 ms 2392 KB Output is correct
36 Correct 85 ms 36176 KB Output is correct
37 Correct 748 ms 40344 KB Output is correct
38 Correct 930 ms 41016 KB Output is correct
39 Correct 931 ms 41320 KB Output is correct
40 Correct 789 ms 38444 KB Output is correct
41 Correct 343 ms 23988 KB Output is correct
42 Correct 318 ms 23920 KB Output is correct
43 Correct 929 ms 40360 KB Output is correct
44 Correct 1 ms 1624 KB Output is correct
45 Correct 1 ms 1884 KB Output is correct
46 Correct 1 ms 2140 KB Output is correct
47 Correct 1 ms 2396 KB Output is correct
48 Correct 1 ms 2396 KB Output is correct
49 Correct 17 ms 2472 KB Output is correct
50 Correct 88 ms 2524 KB Output is correct
51 Correct 335 ms 2740 KB Output is correct
52 Correct 271 ms 2532 KB Output is correct
53 Correct 113 ms 36176 KB Output is correct
54 Correct 1226 ms 41076 KB Output is correct
55 Correct 1283 ms 41404 KB Output is correct
56 Correct 717 ms 38400 KB Output is correct
57 Correct 2541 ms 41728 KB Output is correct
58 Correct 197 ms 37012 KB Output is correct
59 Correct 1309 ms 42616 KB Output is correct