Submission #889865

# Submission time Handle Problem Language Result Execution time Memory
889865 2023-12-20T08:37:53 Z CDuong Relay Marathon (NOI20_relaymarathon) C++17
100 / 100
1220 ms 125956 KB
/*
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,fma,bmi,bmi2,sse4.2,popcnt,lzcnt")
*/

#include <bits/stdc++.h>
#define taskname ""
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define i64 long long
#define pb push_back
#define ff first
#define ss second
#define isz(x) (int)x.size()
using namespace std;

const int mxN = 1e5 + 5;
const int mod = 1e9 + 7;
const i64 oo = 1e18;

int n, m, k;
bool marked[mxN];
vector<pair<int, int>> G[mxN];

pair<int, int> dijkstra_with_starting_point(int st) {
    vector<int> dis(n + 1, 1e9);
    priority_queue<pair<int, int>> pq;
    pq.emplace(0, st); dis[st] = 0;
    while (not pq.empty()) {
        int v = pq.top().ss;
        int cdis = -pq.top().ff;
        pq.pop();
        if (marked[v]) return {cdis, v};
        if (cdis != dis[v]) continue;
        for (auto [u, w] : G[v]) {
            if (cdis + w < dis[u]) {
                dis[u] = cdis + w;
                pq.emplace(-dis[u], u);
            }
        }
    }
    return {1e9, -1};
}

pair<int, pair<int, int>> dijkstra_without_starting_point() {
    vector<int> src(n + 1, 0);
    vector<int> dis(n + 1, 1e9);
    priority_queue<pair<int, int>> pq;
    for (int i = 1; i <= n; ++i) if (marked[i]) {
        pq.emplace(0, i);
        dis[i] = 0, src[i] = i;
    }
    while (not pq.empty()) {
        int v = pq.top().ss;
        int cdis = -pq.top().ff;
        pq.pop();
        if (cdis != dis[v]) continue;
        for (auto [u, w] : G[v]) {
            if (cdis + w < dis[u]) {
                src[u] = src[v];
                dis[u] = cdis + w;
                pq.emplace(-dis[u], u);
            }
        }
    }
    int res = 1e9, ru = -1, rv = -1;
    for (int i = 1; i <= n; ++i) for (auto [u, w] : G[i]) {
        if (src[i] != src[u] && dis[u] + dis[i] + w < res)
            res = dis[u] + dis[i] + w, ru = src[i], rv = src[u];
    }
    return {res, {ru, rv}};
}

void solve() {
    cin >> n >> m >> k;
    for (int i = 1; i <= m; ++i) {
        int u, v, w;
        cin >> u >> v >> w;
        G[u].emplace_back(v, w);
        G[v].emplace_back(u, w);
    }
    for (int i = 1; i <= k; ++i) {
        int idx; cin >> idx;
        marked[idx] = true;
    }

   int res = 1e9;
   auto tmp = dijkstra_without_starting_point();
   int s = tmp.ss.ff, t = tmp.ss.ss, dis1 = tmp.ff;
   marked[s] = marked[t] = false;
   tmp = dijkstra_without_starting_point();
   res = min(res, dis1 + tmp.ff);

   auto tmp1 = dijkstra_with_starting_point(s);
   dis1 = tmp1.ff, marked[tmp1.ss] = false;
   res = min(res, dis1 + dijkstra_with_starting_point(t).ff);
   marked[tmp1.ss] = true;

   auto tmp2 = dijkstra_with_starting_point(t);
   dis1 = tmp2.ff, marked[tmp2.ss] = false;
   res = min(res, dis1 + dijkstra_with_starting_point(s).ff);
   marked[tmp2.ss] = false;

   cout << res << endl;
}

signed main() {

#ifndef CDuongg
    if(fopen(taskname".inp", "r"))
        assert(freopen(taskname".inp", "r", stdin)), assert(freopen(taskname".out", "w", stdout));
#else
    freopen("bai3.inp", "r", stdin);
    freopen("bai3.out", "w", stdout);
    auto start = chrono::high_resolution_clock::now();
#endif

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1; //cin >> t;
    while(t--) solve();

#ifdef CDuongg
   auto end = chrono::high_resolution_clock::now();
   cout << "\n"; for(int i = 1; i <= 100; ++i) cout << '=';
   cout << "\nExecution time: " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
   cout << "Check array size pls sir" << endl;
#endif

}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Correct 1 ms 2652 KB Output is correct
13 Correct 1 ms 2652 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
15 Correct 1 ms 2652 KB Output is correct
16 Correct 1 ms 2816 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 1 ms 2652 KB Output is correct
19 Correct 2 ms 2824 KB Output is correct
20 Correct 1 ms 2652 KB Output is correct
21 Correct 1 ms 2652 KB Output is correct
22 Correct 1 ms 2816 KB Output is correct
23 Correct 1 ms 2652 KB Output is correct
24 Correct 1 ms 2652 KB Output is correct
25 Correct 1 ms 2652 KB Output is correct
26 Correct 1 ms 2652 KB Output is correct
27 Correct 1 ms 2652 KB Output is correct
28 Correct 1 ms 2680 KB Output is correct
29 Correct 1 ms 2652 KB Output is correct
30 Correct 1 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Correct 1 ms 2652 KB Output is correct
13 Correct 1 ms 2652 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
15 Correct 1 ms 2652 KB Output is correct
16 Correct 1 ms 2816 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 1 ms 2652 KB Output is correct
19 Correct 2 ms 2824 KB Output is correct
20 Correct 1 ms 2652 KB Output is correct
21 Correct 1 ms 2652 KB Output is correct
22 Correct 1 ms 2816 KB Output is correct
23 Correct 1 ms 2652 KB Output is correct
24 Correct 1 ms 2652 KB Output is correct
25 Correct 1 ms 2652 KB Output is correct
26 Correct 1 ms 2652 KB Output is correct
27 Correct 1 ms 2652 KB Output is correct
28 Correct 1 ms 2680 KB Output is correct
29 Correct 1 ms 2652 KB Output is correct
30 Correct 1 ms 2652 KB Output is correct
31 Correct 1 ms 2652 KB Output is correct
32 Correct 1 ms 2816 KB Output is correct
33 Correct 1 ms 2728 KB Output is correct
34 Correct 1 ms 2816 KB Output is correct
35 Correct 1 ms 2652 KB Output is correct
36 Correct 2 ms 2908 KB Output is correct
37 Correct 2 ms 2940 KB Output is correct
38 Correct 1 ms 2648 KB Output is correct
39 Correct 17 ms 6012 KB Output is correct
40 Correct 4 ms 3420 KB Output is correct
41 Correct 1 ms 2652 KB Output is correct
42 Correct 5 ms 3384 KB Output is correct
43 Correct 3 ms 2904 KB Output is correct
44 Correct 1 ms 2648 KB Output is correct
45 Correct 1 ms 2652 KB Output is correct
46 Correct 18 ms 6236 KB Output is correct
47 Correct 4 ms 3160 KB Output is correct
48 Correct 14 ms 5952 KB Output is correct
49 Correct 17 ms 5980 KB Output is correct
50 Correct 1 ms 2652 KB Output is correct
51 Correct 1 ms 2652 KB Output is correct
52 Correct 1 ms 2652 KB Output is correct
53 Correct 10 ms 4444 KB Output is correct
54 Correct 17 ms 6028 KB Output is correct
55 Correct 1 ms 2652 KB Output is correct
56 Correct 1 ms 2652 KB Output is correct
57 Correct 1 ms 2652 KB Output is correct
58 Correct 27 ms 6292 KB Output is correct
59 Correct 1 ms 2652 KB Output is correct
60 Correct 2 ms 2908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 7536 KB Output is correct
2 Correct 4 ms 4184 KB Output is correct
3 Correct 1036 ms 101388 KB Output is correct
4 Correct 460 ms 47568 KB Output is correct
5 Correct 103 ms 12408 KB Output is correct
6 Correct 76 ms 10516 KB Output is correct
7 Correct 112 ms 13604 KB Output is correct
8 Correct 47 ms 7500 KB Output is correct
9 Correct 75 ms 9932 KB Output is correct
10 Correct 49 ms 8440 KB Output is correct
11 Correct 956 ms 107312 KB Output is correct
12 Correct 55 ms 8796 KB Output is correct
13 Correct 243 ms 30408 KB Output is correct
14 Correct 120 ms 13720 KB Output is correct
15 Correct 1042 ms 105200 KB Output is correct
16 Correct 28 ms 7004 KB Output is correct
17 Correct 768 ms 74720 KB Output is correct
18 Correct 4 ms 4188 KB Output is correct
19 Correct 1125 ms 112540 KB Output is correct
20 Correct 101 ms 13068 KB Output is correct
21 Correct 115 ms 11900 KB Output is correct
22 Correct 46 ms 8284 KB Output is correct
23 Correct 14 ms 6236 KB Output is correct
24 Correct 720 ms 81652 KB Output is correct
25 Correct 73 ms 10224 KB Output is correct
26 Correct 38 ms 7612 KB Output is correct
27 Correct 59 ms 9184 KB Output is correct
28 Correct 9 ms 5464 KB Output is correct
29 Correct 124 ms 13212 KB Output is correct
30 Correct 207 ms 23744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 1 ms 2652 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Correct 1 ms 2652 KB Output is correct
13 Correct 1 ms 2652 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
15 Correct 1 ms 2652 KB Output is correct
16 Correct 1 ms 2816 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 1 ms 2652 KB Output is correct
19 Correct 2 ms 2824 KB Output is correct
20 Correct 1 ms 2652 KB Output is correct
21 Correct 1 ms 2652 KB Output is correct
22 Correct 1 ms 2816 KB Output is correct
23 Correct 1 ms 2652 KB Output is correct
24 Correct 1 ms 2652 KB Output is correct
25 Correct 1 ms 2652 KB Output is correct
26 Correct 1 ms 2652 KB Output is correct
27 Correct 1 ms 2652 KB Output is correct
28 Correct 1 ms 2680 KB Output is correct
29 Correct 1 ms 2652 KB Output is correct
30 Correct 1 ms 2652 KB Output is correct
31 Correct 1 ms 2652 KB Output is correct
32 Correct 1 ms 2816 KB Output is correct
33 Correct 1 ms 2728 KB Output is correct
34 Correct 1 ms 2816 KB Output is correct
35 Correct 1 ms 2652 KB Output is correct
36 Correct 2 ms 2908 KB Output is correct
37 Correct 2 ms 2940 KB Output is correct
38 Correct 1 ms 2648 KB Output is correct
39 Correct 17 ms 6012 KB Output is correct
40 Correct 4 ms 3420 KB Output is correct
41 Correct 1 ms 2652 KB Output is correct
42 Correct 5 ms 3384 KB Output is correct
43 Correct 3 ms 2904 KB Output is correct
44 Correct 1 ms 2648 KB Output is correct
45 Correct 1 ms 2652 KB Output is correct
46 Correct 18 ms 6236 KB Output is correct
47 Correct 4 ms 3160 KB Output is correct
48 Correct 14 ms 5952 KB Output is correct
49 Correct 17 ms 5980 KB Output is correct
50 Correct 1 ms 2652 KB Output is correct
51 Correct 1 ms 2652 KB Output is correct
52 Correct 1 ms 2652 KB Output is correct
53 Correct 10 ms 4444 KB Output is correct
54 Correct 17 ms 6028 KB Output is correct
55 Correct 1 ms 2652 KB Output is correct
56 Correct 1 ms 2652 KB Output is correct
57 Correct 1 ms 2652 KB Output is correct
58 Correct 27 ms 6292 KB Output is correct
59 Correct 1 ms 2652 KB Output is correct
60 Correct 2 ms 2908 KB Output is correct
61 Correct 45 ms 7536 KB Output is correct
62 Correct 4 ms 4184 KB Output is correct
63 Correct 1036 ms 101388 KB Output is correct
64 Correct 460 ms 47568 KB Output is correct
65 Correct 103 ms 12408 KB Output is correct
66 Correct 76 ms 10516 KB Output is correct
67 Correct 112 ms 13604 KB Output is correct
68 Correct 47 ms 7500 KB Output is correct
69 Correct 75 ms 9932 KB Output is correct
70 Correct 49 ms 8440 KB Output is correct
71 Correct 956 ms 107312 KB Output is correct
72 Correct 55 ms 8796 KB Output is correct
73 Correct 243 ms 30408 KB Output is correct
74 Correct 120 ms 13720 KB Output is correct
75 Correct 1042 ms 105200 KB Output is correct
76 Correct 28 ms 7004 KB Output is correct
77 Correct 768 ms 74720 KB Output is correct
78 Correct 4 ms 4188 KB Output is correct
79 Correct 1125 ms 112540 KB Output is correct
80 Correct 101 ms 13068 KB Output is correct
81 Correct 115 ms 11900 KB Output is correct
82 Correct 46 ms 8284 KB Output is correct
83 Correct 14 ms 6236 KB Output is correct
84 Correct 720 ms 81652 KB Output is correct
85 Correct 73 ms 10224 KB Output is correct
86 Correct 38 ms 7612 KB Output is correct
87 Correct 59 ms 9184 KB Output is correct
88 Correct 9 ms 5464 KB Output is correct
89 Correct 124 ms 13212 KB Output is correct
90 Correct 207 ms 23744 KB Output is correct
91 Correct 76 ms 9540 KB Output is correct
92 Correct 1080 ms 115516 KB Output is correct
93 Correct 128 ms 13592 KB Output is correct
94 Correct 677 ms 69844 KB Output is correct
95 Correct 4 ms 4108 KB Output is correct
96 Correct 5 ms 4316 KB Output is correct
97 Correct 137 ms 17468 KB Output is correct
98 Correct 65 ms 9484 KB Output is correct
99 Correct 95 ms 10836 KB Output is correct
100 Correct 1220 ms 124756 KB Output is correct
101 Correct 451 ms 47696 KB Output is correct
102 Correct 477 ms 46780 KB Output is correct
103 Correct 729 ms 84572 KB Output is correct
104 Correct 959 ms 97864 KB Output is correct
105 Correct 44 ms 7744 KB Output is correct
106 Correct 753 ms 86588 KB Output is correct
107 Correct 255 ms 25572 KB Output is correct
108 Correct 131 ms 12656 KB Output is correct
109 Correct 12 ms 5724 KB Output is correct
110 Correct 33 ms 7260 KB Output is correct
111 Correct 117 ms 12996 KB Output is correct
112 Correct 1186 ms 124724 KB Output is correct
113 Correct 1177 ms 125956 KB Output is correct
114 Correct 132 ms 12368 KB Output is correct
115 Correct 361 ms 43216 KB Output is correct
116 Correct 1061 ms 113348 KB Output is correct
117 Correct 757 ms 82776 KB Output is correct
118 Correct 723 ms 74884 KB Output is correct
119 Correct 1010 ms 101468 KB Output is correct
120 Correct 653 ms 61772 KB Output is correct