Submission #487151

# Submission time Handle Problem Language Result Execution time Memory
487151 2021-11-14T15:39:27 Z KienTran Relay Marathon (NOI20_relaymarathon) C++14
100 / 100
2945 ms 246180 KB
#include <bits/stdc++.h>
#define int long long

using namespace std;

const int O = 3e6 + 5;
const int inf = 1e18;

int n, k, m, E[O], W[O], par[O];
vector <int> g[O];

priority_queue <pair <int, int>, vector <pair <int, int>>, greater <pair <int, int>>> q;

vector <int> Dijktra(vector <int> S){
    vector <int> d(n);
    for (int i = 0; i < n; ++ i) d[i] = inf, par[i] = 0;

    for (int i : S){
        d[i] = 0;
        par[i] = i;
        q.push({0, i});
    }

    while (q.size()){
        int u = q.top().second;
        int dist = q.top().first;
        q.pop();

        if (dist != d[u]) continue;

        for (int i : g[u]){
            int v = u ^ E[i];
            int w = W[i];

            if (dist + w < d[v]){
                d[v] = dist + w;
                par[v] = par[u];
                q.push({dist + w, v});
            }
        }
    }

    return d;
}

array <int, 3> bestPair(vector <int> d){
    array <int, 3> res; res[2] = inf;
    for (int u = 0; u < n; ++ u){
        for (int i : g[u]){
            int v = u ^ E[i];
            int w = W[i];

            if (par[u] != par[v] && d[u] + d[v] + w < res[2]){
                res[0] = par[u];
                res[1] = par[v];
                res[2] = d[u] + d[v] + w;
            }
        }
    }
    return res;
}

main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> n >> m >> k;
    for (int i = 1; i <= m; ++ i){
        int u, v, w; cin >> u >> v >> w;
        u -= 1; v -= 1;
        g[u].push_back(i);
        g[v].push_back(i);
        E[i] = u ^ v;
        W[i] = w;
    }

    vector <int> specials(k);
    for (auto &i : specials){
        cin >> i; i -= 1;
    }

    ///
    array <int, 3> P1 = bestPair(Dijktra(specials));

    vector <int> anoSpecials;
    for (int i : specials){
        if (i != P1[0] && i != P1[1]){
            anoSpecials.push_back(i);
        }
    }

    array <int, 3> P2 = bestPair(Dijktra(anoSpecials));
    ///
    int res = P1[2] + P2[2];

    vector <int> onlyP0 = {P1[0]};
    vector <int> onlyP1 = {P1[1]};
    vector <int> dist0 = Dijktra(onlyP0);
    vector <int> dist1 = Dijktra(onlyP1);

    set <pair <int, int>> Min;
    for (int i : specials){
        if (i != P1[0] && i != P1[1]){
            Min.insert({dist1[i], i});
        }
    }

    for (int i : specials){
        if (i != P1[0] && i != P1[1]){
            Min.erase({dist1[i], i});
            int minCost = (*Min.begin()).first;
            Min.insert({dist1[i], i});
            res = min(res, dist0[i] + minCost);
        }
    }

    cout << res;
}

Compilation message

RelayMarathon.cpp:63:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   63 | main(){
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 31 ms 70800 KB Output is correct
2 Correct 32 ms 70800 KB Output is correct
3 Correct 31 ms 70768 KB Output is correct
4 Correct 31 ms 70668 KB Output is correct
5 Correct 32 ms 70780 KB Output is correct
6 Correct 32 ms 70724 KB Output is correct
7 Correct 33 ms 70744 KB Output is correct
8 Correct 32 ms 70764 KB Output is correct
9 Correct 32 ms 70692 KB Output is correct
10 Correct 31 ms 70720 KB Output is correct
11 Correct 31 ms 70704 KB Output is correct
12 Correct 31 ms 70764 KB Output is correct
13 Correct 34 ms 70840 KB Output is correct
14 Correct 31 ms 70820 KB Output is correct
15 Correct 32 ms 70784 KB Output is correct
16 Correct 39 ms 70756 KB Output is correct
17 Correct 37 ms 70724 KB Output is correct
18 Correct 31 ms 70732 KB Output is correct
19 Correct 34 ms 70776 KB Output is correct
20 Correct 31 ms 70776 KB Output is correct
21 Correct 32 ms 70828 KB Output is correct
22 Correct 35 ms 70676 KB Output is correct
23 Correct 32 ms 70732 KB Output is correct
24 Correct 35 ms 70748 KB Output is correct
25 Correct 32 ms 70692 KB Output is correct
26 Correct 33 ms 70708 KB Output is correct
27 Correct 38 ms 70724 KB Output is correct
28 Correct 32 ms 70864 KB Output is correct
29 Correct 32 ms 70720 KB Output is correct
30 Correct 31 ms 70860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 70800 KB Output is correct
2 Correct 32 ms 70800 KB Output is correct
3 Correct 31 ms 70768 KB Output is correct
4 Correct 31 ms 70668 KB Output is correct
5 Correct 32 ms 70780 KB Output is correct
6 Correct 32 ms 70724 KB Output is correct
7 Correct 33 ms 70744 KB Output is correct
8 Correct 32 ms 70764 KB Output is correct
9 Correct 32 ms 70692 KB Output is correct
10 Correct 31 ms 70720 KB Output is correct
11 Correct 31 ms 70704 KB Output is correct
12 Correct 31 ms 70764 KB Output is correct
13 Correct 34 ms 70840 KB Output is correct
14 Correct 31 ms 70820 KB Output is correct
15 Correct 32 ms 70784 KB Output is correct
16 Correct 39 ms 70756 KB Output is correct
17 Correct 37 ms 70724 KB Output is correct
18 Correct 31 ms 70732 KB Output is correct
19 Correct 34 ms 70776 KB Output is correct
20 Correct 31 ms 70776 KB Output is correct
21 Correct 32 ms 70828 KB Output is correct
22 Correct 35 ms 70676 KB Output is correct
23 Correct 32 ms 70732 KB Output is correct
24 Correct 35 ms 70748 KB Output is correct
25 Correct 32 ms 70692 KB Output is correct
26 Correct 33 ms 70708 KB Output is correct
27 Correct 38 ms 70724 KB Output is correct
28 Correct 32 ms 70864 KB Output is correct
29 Correct 32 ms 70720 KB Output is correct
30 Correct 31 ms 70860 KB Output is correct
31 Correct 32 ms 70724 KB Output is correct
32 Correct 31 ms 70772 KB Output is correct
33 Correct 32 ms 70808 KB Output is correct
34 Correct 39 ms 70724 KB Output is correct
35 Correct 40 ms 70724 KB Output is correct
36 Correct 35 ms 71044 KB Output is correct
37 Correct 34 ms 71108 KB Output is correct
38 Correct 33 ms 70876 KB Output is correct
39 Correct 52 ms 75364 KB Output is correct
40 Correct 37 ms 71732 KB Output is correct
41 Correct 32 ms 70860 KB Output is correct
42 Correct 38 ms 71896 KB Output is correct
43 Correct 34 ms 70952 KB Output is correct
44 Correct 33 ms 70860 KB Output is correct
45 Correct 32 ms 70792 KB Output is correct
46 Correct 57 ms 75956 KB Output is correct
47 Correct 37 ms 71620 KB Output is correct
48 Correct 65 ms 75196 KB Output is correct
49 Correct 54 ms 75400 KB Output is correct
50 Correct 32 ms 70748 KB Output is correct
51 Correct 32 ms 70800 KB Output is correct
52 Correct 32 ms 70832 KB Output is correct
53 Correct 45 ms 73256 KB Output is correct
54 Correct 54 ms 75516 KB Output is correct
55 Correct 32 ms 70732 KB Output is correct
56 Correct 36 ms 70776 KB Output is correct
57 Correct 34 ms 70732 KB Output is correct
58 Correct 60 ms 76220 KB Output is correct
59 Correct 31 ms 70752 KB Output is correct
60 Correct 33 ms 70868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 93 ms 78116 KB Output is correct
2 Correct 49 ms 73780 KB Output is correct
3 Correct 1848 ms 210748 KB Output is correct
4 Correct 900 ms 133300 KB Output is correct
5 Correct 209 ms 84768 KB Output is correct
6 Correct 219 ms 82128 KB Output is correct
7 Correct 228 ms 86056 KB Output is correct
8 Correct 103 ms 78376 KB Output is correct
9 Correct 173 ms 81428 KB Output is correct
10 Correct 110 ms 79288 KB Output is correct
11 Correct 1838 ms 218300 KB Output is correct
12 Correct 122 ms 79836 KB Output is correct
13 Correct 579 ms 114276 KB Output is correct
14 Correct 274 ms 86804 KB Output is correct
15 Correct 1993 ms 215560 KB Output is correct
16 Correct 83 ms 77492 KB Output is correct
17 Correct 1287 ms 168952 KB Output is correct
18 Correct 40 ms 73780 KB Output is correct
19 Correct 2003 ms 224452 KB Output is correct
20 Correct 225 ms 86028 KB Output is correct
21 Correct 223 ms 83836 KB Output is correct
22 Correct 157 ms 78900 KB Output is correct
23 Correct 55 ms 76344 KB Output is correct
24 Correct 1443 ms 179908 KB Output is correct
25 Correct 162 ms 81452 KB Output is correct
26 Correct 98 ms 78228 KB Output is correct
27 Correct 178 ms 80136 KB Output is correct
28 Correct 51 ms 75048 KB Output is correct
29 Correct 230 ms 85540 KB Output is correct
30 Correct 464 ms 99864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 70800 KB Output is correct
2 Correct 32 ms 70800 KB Output is correct
3 Correct 31 ms 70768 KB Output is correct
4 Correct 31 ms 70668 KB Output is correct
5 Correct 32 ms 70780 KB Output is correct
6 Correct 32 ms 70724 KB Output is correct
7 Correct 33 ms 70744 KB Output is correct
8 Correct 32 ms 70764 KB Output is correct
9 Correct 32 ms 70692 KB Output is correct
10 Correct 31 ms 70720 KB Output is correct
11 Correct 31 ms 70704 KB Output is correct
12 Correct 31 ms 70764 KB Output is correct
13 Correct 34 ms 70840 KB Output is correct
14 Correct 31 ms 70820 KB Output is correct
15 Correct 32 ms 70784 KB Output is correct
16 Correct 39 ms 70756 KB Output is correct
17 Correct 37 ms 70724 KB Output is correct
18 Correct 31 ms 70732 KB Output is correct
19 Correct 34 ms 70776 KB Output is correct
20 Correct 31 ms 70776 KB Output is correct
21 Correct 32 ms 70828 KB Output is correct
22 Correct 35 ms 70676 KB Output is correct
23 Correct 32 ms 70732 KB Output is correct
24 Correct 35 ms 70748 KB Output is correct
25 Correct 32 ms 70692 KB Output is correct
26 Correct 33 ms 70708 KB Output is correct
27 Correct 38 ms 70724 KB Output is correct
28 Correct 32 ms 70864 KB Output is correct
29 Correct 32 ms 70720 KB Output is correct
30 Correct 31 ms 70860 KB Output is correct
31 Correct 32 ms 70724 KB Output is correct
32 Correct 31 ms 70772 KB Output is correct
33 Correct 32 ms 70808 KB Output is correct
34 Correct 39 ms 70724 KB Output is correct
35 Correct 40 ms 70724 KB Output is correct
36 Correct 35 ms 71044 KB Output is correct
37 Correct 34 ms 71108 KB Output is correct
38 Correct 33 ms 70876 KB Output is correct
39 Correct 52 ms 75364 KB Output is correct
40 Correct 37 ms 71732 KB Output is correct
41 Correct 32 ms 70860 KB Output is correct
42 Correct 38 ms 71896 KB Output is correct
43 Correct 34 ms 70952 KB Output is correct
44 Correct 33 ms 70860 KB Output is correct
45 Correct 32 ms 70792 KB Output is correct
46 Correct 57 ms 75956 KB Output is correct
47 Correct 37 ms 71620 KB Output is correct
48 Correct 65 ms 75196 KB Output is correct
49 Correct 54 ms 75400 KB Output is correct
50 Correct 32 ms 70748 KB Output is correct
51 Correct 32 ms 70800 KB Output is correct
52 Correct 32 ms 70832 KB Output is correct
53 Correct 45 ms 73256 KB Output is correct
54 Correct 54 ms 75516 KB Output is correct
55 Correct 32 ms 70732 KB Output is correct
56 Correct 36 ms 70776 KB Output is correct
57 Correct 34 ms 70732 KB Output is correct
58 Correct 60 ms 76220 KB Output is correct
59 Correct 31 ms 70752 KB Output is correct
60 Correct 33 ms 70868 KB Output is correct
61 Correct 93 ms 78116 KB Output is correct
62 Correct 49 ms 73780 KB Output is correct
63 Correct 1848 ms 210748 KB Output is correct
64 Correct 900 ms 133300 KB Output is correct
65 Correct 209 ms 84768 KB Output is correct
66 Correct 219 ms 82128 KB Output is correct
67 Correct 228 ms 86056 KB Output is correct
68 Correct 103 ms 78376 KB Output is correct
69 Correct 173 ms 81428 KB Output is correct
70 Correct 110 ms 79288 KB Output is correct
71 Correct 1838 ms 218300 KB Output is correct
72 Correct 122 ms 79836 KB Output is correct
73 Correct 579 ms 114276 KB Output is correct
74 Correct 274 ms 86804 KB Output is correct
75 Correct 1993 ms 215560 KB Output is correct
76 Correct 83 ms 77492 KB Output is correct
77 Correct 1287 ms 168952 KB Output is correct
78 Correct 40 ms 73780 KB Output is correct
79 Correct 2003 ms 224452 KB Output is correct
80 Correct 225 ms 86028 KB Output is correct
81 Correct 223 ms 83836 KB Output is correct
82 Correct 157 ms 78900 KB Output is correct
83 Correct 55 ms 76344 KB Output is correct
84 Correct 1443 ms 179908 KB Output is correct
85 Correct 162 ms 81452 KB Output is correct
86 Correct 98 ms 78228 KB Output is correct
87 Correct 178 ms 80136 KB Output is correct
88 Correct 51 ms 75048 KB Output is correct
89 Correct 230 ms 85540 KB Output is correct
90 Correct 464 ms 99864 KB Output is correct
91 Correct 212 ms 80764 KB Output is correct
92 Correct 2883 ms 235144 KB Output is correct
93 Correct 354 ms 86164 KB Output is correct
94 Correct 1616 ms 162060 KB Output is correct
95 Correct 45 ms 73908 KB Output is correct
96 Correct 46 ms 74028 KB Output is correct
97 Correct 497 ms 93444 KB Output is correct
98 Correct 244 ms 80688 KB Output is correct
99 Correct 191 ms 81812 KB Output is correct
100 Correct 2945 ms 243968 KB Output is correct
101 Correct 1190 ms 131656 KB Output is correct
102 Correct 1143 ms 130888 KB Output is correct
103 Correct 1912 ms 186168 KB Output is correct
104 Correct 2304 ms 206016 KB Output is correct
105 Correct 138 ms 78408 KB Output is correct
106 Correct 1946 ms 186764 KB Output is correct
107 Correct 676 ms 103312 KB Output is correct
108 Correct 321 ms 84640 KB Output is correct
109 Correct 65 ms 75680 KB Output is correct
110 Correct 144 ms 77860 KB Output is correct
111 Correct 314 ms 85192 KB Output is correct
112 Correct 2866 ms 244820 KB Output is correct
113 Correct 2941 ms 246180 KB Output is correct
114 Correct 327 ms 84456 KB Output is correct
115 Correct 1182 ms 132152 KB Output is correct
116 Correct 2732 ms 231972 KB Output is correct
117 Correct 1980 ms 183544 KB Output is correct
118 Correct 1724 ms 171320 KB Output is correct
119 Correct 2391 ms 213020 KB Output is correct
120 Correct 1482 ms 151872 KB Output is correct