답안 #337601

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
337601 2020-12-21T08:18:00 Z ncduy0303 Relay Marathon (NOI20_relaymarathon) C++17
100 / 100
2778 ms 162616 KB
#include <bits/stdc++.h>

using namespace std;

#define ar array
#define ll long long

const int MAX_N = 1e5 + 1;
const ll MOD = 1e9 + 7;
const ll INF = 1e9;

int V, E, K, head[MAX_N];
vector<int> d, dx1, dy1;
set<int> S;
bool skip[MAX_N];
vector<ar<int,2>> adj[MAX_N];
vector<ar<int,3>> el;
priority_queue<ar<int,2>, vector<ar<int,2>>, greater<ar<int,2>>> pq;

void dijk() {
    while (!pq.empty()) {
        int u = pq.top()[1];
        pq.pop();
        for (auto cur : adj[u]) {
            int v = cur[0], w = cur[1];
            if (skip[v]) continue;
            if (d[u] + w < d[v]) {
                d[v] = d[u] + w;
                pq.push({d[v], v});
                head[v] = head[u];
            }
        }
    }
}

void run(int s) {
    while (!pq.empty()) pq.pop();
    d.assign(V + 1, INF);
    d[s] = 0;
    head[s] = s;
    pq.push({0, s});
    dijk();
}

ar<int,3> find() {
    while (!pq.empty()) pq.pop();
    d.assign(V + 1, INF);
    for (int s : S) {
        if (skip[s]) continue;
        d[s] = 0;
        pq.push({0, s});
        head[s] = s;
    }
    dijk();
    int s1, e1, d1 = INF;
    for (auto [u, v, w] : el) {
        if (skip[u] || skip[v])  continue;
        if (d[u] == INF || d[v] == INF || head[u] == head[v])  continue;
        if (d1 > d[u] + w + d[v]) {
            d1 = d[u] + w + d[v];
            s1 = head[u], e1 = head[v];
        }
    }
    return {s1, e1, d1};
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    cin >> V >> E >> K;
    while (E--) {
        int u, v, w;
        cin >> u >> v >> w;
        adj[u].push_back({v, w});
        adj[v].push_back({u, w});
        el.push_back({u, v, w});
    }
    for (int i = 1; i <= K; i++) {
        int u; cin >> u;
        S.insert(u);
    }

    auto [x1, y1, d1] = find();

    // x1 and y1 are now the closet pair among all special nodes
    // There are 2 cases:
    // Case #1: x1 and y1 are actually x1 and x2 => Run 2 separate Dijkstra on x1 and x2 to find the best y1, y2 using an O(N) technique
    // Case #2: x1 and y1 are still x1 and y1 => Just create a new graph without x1, y1 and repeat the same process to find x2, y2
    // Final answer is the minimum between the two cases

    // Case #1
    run(x1); dx1 = d;
    run(y1); dy1 = d;
    int c1 = -1, c2 = -1;
    for (int c : S) {
        if (c == x1 or c == y1)  continue;
        if (c1 == -1 || dy1[c] <= dy1[c1]) {
            c2 = c1;
            c1 = c;
        } else if(c2 == -1 || dy1[c] < dy1[c2]) {
            c2 = c;
        }
    }
    int ans = INF, a1 = -1, a2 = -1, a3 = -1, a4 = -1;
    if (dx1[c1] != INF && dy1[c2] != INF) {
        ans = dx1[c1] + dy1[c2];
        a1 = x1, a2 = c1, a3 = y1, a4 = c2;
    }
    for (int x2 : S) {
        if (x2 == x1 || x2 == y1 || x2 == c1)  continue;
        if (dx1[x2] != INF && dy1[c1] != INF) {
            if (ans > dx1[x2] + dy1[c1]) {
                ans = dx1[x2] + dy1[c1];
                a1 = x1, a2 = x2, a3 = y1, a4 = c1;
            }
        }
    }

    // Case #2: removing s1 and e1 before repeating the same process
    skip[x1] = skip[y1] = 1;
    auto [x2, y2, d2] = find();
    if (d1 != INF and d2 != INF) {
        if (ans > d1 + d2) {
            ans = d1 + d2;
            a1 = x1, a2 = y1, a3 = x2, a4 = y2;
        }
    }

    cout << ans;
}

Compilation message

RelayMarathon.cpp: In function 'int main()':
RelayMarathon.cpp:107:20: warning: variable 'a1' set but not used [-Wunused-but-set-variable]
  107 |     int ans = INF, a1 = -1, a2 = -1, a3 = -1, a4 = -1;
      |                    ^~
RelayMarathon.cpp:107:29: warning: variable 'a2' set but not used [-Wunused-but-set-variable]
  107 |     int ans = INF, a1 = -1, a2 = -1, a3 = -1, a4 = -1;
      |                             ^~
RelayMarathon.cpp:107:38: warning: variable 'a3' set but not used [-Wunused-but-set-variable]
  107 |     int ans = INF, a1 = -1, a2 = -1, a3 = -1, a4 = -1;
      |                                      ^~
RelayMarathon.cpp:107:47: warning: variable 'a4' set but not used [-Wunused-but-set-variable]
  107 |     int ans = INF, a1 = -1, a2 = -1, a3 = -1, a4 = -1;
      |                                               ^~
RelayMarathon.cpp: In function 'std::array<int, 3> find()':
RelayMarathon.cpp:64:23: warning: 'e1' may be used uninitialized in this function [-Wmaybe-uninitialized]
   64 |     return {s1, e1, d1};
      |                       ^
RelayMarathon.cpp:64:23: warning: 's1' may be used uninitialized in this function [-Wmaybe-uninitialized]
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2796 KB Output is correct
2 Correct 3 ms 2796 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 2 ms 2668 KB Output is correct
5 Correct 2 ms 2668 KB Output is correct
6 Correct 2 ms 2668 KB Output is correct
7 Correct 2 ms 2796 KB Output is correct
8 Correct 3 ms 2796 KB Output is correct
9 Correct 2 ms 2668 KB Output is correct
10 Correct 2 ms 2668 KB Output is correct
11 Correct 2 ms 2668 KB Output is correct
12 Correct 2 ms 2668 KB Output is correct
13 Correct 2 ms 2796 KB Output is correct
14 Correct 2 ms 2796 KB Output is correct
15 Correct 3 ms 2796 KB Output is correct
16 Correct 2 ms 2668 KB Output is correct
17 Correct 2 ms 2796 KB Output is correct
18 Correct 2 ms 2668 KB Output is correct
19 Correct 2 ms 2796 KB Output is correct
20 Correct 2 ms 2668 KB Output is correct
21 Correct 2 ms 2796 KB Output is correct
22 Correct 2 ms 2668 KB Output is correct
23 Correct 2 ms 2668 KB Output is correct
24 Correct 2 ms 2668 KB Output is correct
25 Correct 2 ms 2668 KB Output is correct
26 Correct 2 ms 2668 KB Output is correct
27 Correct 2 ms 2668 KB Output is correct
28 Correct 3 ms 2796 KB Output is correct
29 Correct 2 ms 2796 KB Output is correct
30 Correct 3 ms 2796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2796 KB Output is correct
2 Correct 3 ms 2796 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 2 ms 2668 KB Output is correct
5 Correct 2 ms 2668 KB Output is correct
6 Correct 2 ms 2668 KB Output is correct
7 Correct 2 ms 2796 KB Output is correct
8 Correct 3 ms 2796 KB Output is correct
9 Correct 2 ms 2668 KB Output is correct
10 Correct 2 ms 2668 KB Output is correct
11 Correct 2 ms 2668 KB Output is correct
12 Correct 2 ms 2668 KB Output is correct
13 Correct 2 ms 2796 KB Output is correct
14 Correct 2 ms 2796 KB Output is correct
15 Correct 3 ms 2796 KB Output is correct
16 Correct 2 ms 2668 KB Output is correct
17 Correct 2 ms 2796 KB Output is correct
18 Correct 2 ms 2668 KB Output is correct
19 Correct 2 ms 2796 KB Output is correct
20 Correct 2 ms 2668 KB Output is correct
21 Correct 2 ms 2796 KB Output is correct
22 Correct 2 ms 2668 KB Output is correct
23 Correct 2 ms 2668 KB Output is correct
24 Correct 2 ms 2668 KB Output is correct
25 Correct 2 ms 2668 KB Output is correct
26 Correct 2 ms 2668 KB Output is correct
27 Correct 2 ms 2668 KB Output is correct
28 Correct 3 ms 2796 KB Output is correct
29 Correct 2 ms 2796 KB Output is correct
30 Correct 3 ms 2796 KB Output is correct
31 Correct 2 ms 2668 KB Output is correct
32 Correct 2 ms 2796 KB Output is correct
33 Correct 3 ms 2796 KB Output is correct
34 Correct 2 ms 2668 KB Output is correct
35 Correct 2 ms 2668 KB Output is correct
36 Correct 4 ms 2924 KB Output is correct
37 Correct 5 ms 3052 KB Output is correct
38 Correct 3 ms 2796 KB Output is correct
39 Correct 34 ms 7076 KB Output is correct
40 Correct 10 ms 3820 KB Output is correct
41 Correct 3 ms 2796 KB Output is correct
42 Correct 9 ms 3820 KB Output is correct
43 Correct 4 ms 2924 KB Output is correct
44 Correct 3 ms 2796 KB Output is correct
45 Correct 2 ms 2668 KB Output is correct
46 Correct 42 ms 7456 KB Output is correct
47 Correct 8 ms 3308 KB Output is correct
48 Correct 32 ms 7076 KB Output is correct
49 Correct 35 ms 7204 KB Output is correct
50 Correct 3 ms 2796 KB Output is correct
51 Correct 3 ms 2796 KB Output is correct
52 Correct 3 ms 2796 KB Output is correct
53 Correct 20 ms 5028 KB Output is correct
54 Correct 41 ms 7076 KB Output is correct
55 Correct 3 ms 2796 KB Output is correct
56 Correct 2 ms 2668 KB Output is correct
57 Correct 3 ms 2796 KB Output is correct
58 Correct 44 ms 7712 KB Output is correct
59 Correct 2 ms 2668 KB Output is correct
60 Correct 3 ms 2796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 86 ms 8992 KB Output is correct
2 Correct 8 ms 4588 KB Output is correct
3 Correct 2322 ms 139896 KB Output is correct
4 Correct 1173 ms 59392 KB Output is correct
5 Correct 228 ms 15224 KB Output is correct
6 Correct 172 ms 12840 KB Output is correct
7 Correct 272 ms 16156 KB Output is correct
8 Correct 86 ms 9120 KB Output is correct
9 Correct 159 ms 12064 KB Output is correct
10 Correct 120 ms 10272 KB Output is correct
11 Correct 2445 ms 140496 KB Output is correct
12 Correct 129 ms 10656 KB Output is correct
13 Correct 814 ms 41500 KB Output is correct
14 Correct 362 ms 16668 KB Output is correct
15 Correct 2456 ms 139876 KB Output is correct
16 Correct 65 ms 8484 KB Output is correct
17 Correct 1721 ms 93156 KB Output is correct
18 Correct 8 ms 4844 KB Output is correct
19 Correct 2646 ms 143176 KB Output is correct
20 Correct 242 ms 15888 KB Output is correct
21 Correct 202 ms 14096 KB Output is correct
22 Correct 108 ms 10036 KB Output is correct
23 Correct 28 ms 7332 KB Output is correct
24 Correct 1761 ms 104392 KB Output is correct
25 Correct 172 ms 12392 KB Output is correct
26 Correct 88 ms 9120 KB Output is correct
27 Correct 136 ms 11112 KB Output is correct
28 Correct 16 ms 6056 KB Output is correct
29 Correct 237 ms 15900 KB Output is correct
30 Correct 639 ms 28688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2796 KB Output is correct
2 Correct 3 ms 2796 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 2 ms 2668 KB Output is correct
5 Correct 2 ms 2668 KB Output is correct
6 Correct 2 ms 2668 KB Output is correct
7 Correct 2 ms 2796 KB Output is correct
8 Correct 3 ms 2796 KB Output is correct
9 Correct 2 ms 2668 KB Output is correct
10 Correct 2 ms 2668 KB Output is correct
11 Correct 2 ms 2668 KB Output is correct
12 Correct 2 ms 2668 KB Output is correct
13 Correct 2 ms 2796 KB Output is correct
14 Correct 2 ms 2796 KB Output is correct
15 Correct 3 ms 2796 KB Output is correct
16 Correct 2 ms 2668 KB Output is correct
17 Correct 2 ms 2796 KB Output is correct
18 Correct 2 ms 2668 KB Output is correct
19 Correct 2 ms 2796 KB Output is correct
20 Correct 2 ms 2668 KB Output is correct
21 Correct 2 ms 2796 KB Output is correct
22 Correct 2 ms 2668 KB Output is correct
23 Correct 2 ms 2668 KB Output is correct
24 Correct 2 ms 2668 KB Output is correct
25 Correct 2 ms 2668 KB Output is correct
26 Correct 2 ms 2668 KB Output is correct
27 Correct 2 ms 2668 KB Output is correct
28 Correct 3 ms 2796 KB Output is correct
29 Correct 2 ms 2796 KB Output is correct
30 Correct 3 ms 2796 KB Output is correct
31 Correct 2 ms 2668 KB Output is correct
32 Correct 2 ms 2796 KB Output is correct
33 Correct 3 ms 2796 KB Output is correct
34 Correct 2 ms 2668 KB Output is correct
35 Correct 2 ms 2668 KB Output is correct
36 Correct 4 ms 2924 KB Output is correct
37 Correct 5 ms 3052 KB Output is correct
38 Correct 3 ms 2796 KB Output is correct
39 Correct 34 ms 7076 KB Output is correct
40 Correct 10 ms 3820 KB Output is correct
41 Correct 3 ms 2796 KB Output is correct
42 Correct 9 ms 3820 KB Output is correct
43 Correct 4 ms 2924 KB Output is correct
44 Correct 3 ms 2796 KB Output is correct
45 Correct 2 ms 2668 KB Output is correct
46 Correct 42 ms 7456 KB Output is correct
47 Correct 8 ms 3308 KB Output is correct
48 Correct 32 ms 7076 KB Output is correct
49 Correct 35 ms 7204 KB Output is correct
50 Correct 3 ms 2796 KB Output is correct
51 Correct 3 ms 2796 KB Output is correct
52 Correct 3 ms 2796 KB Output is correct
53 Correct 20 ms 5028 KB Output is correct
54 Correct 41 ms 7076 KB Output is correct
55 Correct 3 ms 2796 KB Output is correct
56 Correct 2 ms 2668 KB Output is correct
57 Correct 3 ms 2796 KB Output is correct
58 Correct 44 ms 7712 KB Output is correct
59 Correct 2 ms 2668 KB Output is correct
60 Correct 3 ms 2796 KB Output is correct
61 Correct 86 ms 8992 KB Output is correct
62 Correct 8 ms 4588 KB Output is correct
63 Correct 2322 ms 139896 KB Output is correct
64 Correct 1173 ms 59392 KB Output is correct
65 Correct 228 ms 15224 KB Output is correct
66 Correct 172 ms 12840 KB Output is correct
67 Correct 272 ms 16156 KB Output is correct
68 Correct 86 ms 9120 KB Output is correct
69 Correct 159 ms 12064 KB Output is correct
70 Correct 120 ms 10272 KB Output is correct
71 Correct 2445 ms 140496 KB Output is correct
72 Correct 129 ms 10656 KB Output is correct
73 Correct 814 ms 41500 KB Output is correct
74 Correct 362 ms 16668 KB Output is correct
75 Correct 2456 ms 139876 KB Output is correct
76 Correct 65 ms 8484 KB Output is correct
77 Correct 1721 ms 93156 KB Output is correct
78 Correct 8 ms 4844 KB Output is correct
79 Correct 2646 ms 143176 KB Output is correct
80 Correct 242 ms 15888 KB Output is correct
81 Correct 202 ms 14096 KB Output is correct
82 Correct 108 ms 10036 KB Output is correct
83 Correct 28 ms 7332 KB Output is correct
84 Correct 1761 ms 104392 KB Output is correct
85 Correct 172 ms 12392 KB Output is correct
86 Correct 88 ms 9120 KB Output is correct
87 Correct 136 ms 11112 KB Output is correct
88 Correct 16 ms 6056 KB Output is correct
89 Correct 237 ms 15900 KB Output is correct
90 Correct 639 ms 28688 KB Output is correct
91 Correct 218 ms 11680 KB Output is correct
92 Correct 2528 ms 151488 KB Output is correct
93 Correct 355 ms 16384 KB Output is correct
94 Correct 1664 ms 87040 KB Output is correct
95 Correct 8 ms 4716 KB Output is correct
96 Correct 9 ms 4972 KB Output is correct
97 Correct 475 ms 21912 KB Output is correct
98 Correct 212 ms 11552 KB Output is correct
99 Correct 176 ms 12704 KB Output is correct
100 Correct 2778 ms 161208 KB Output is correct
101 Correct 1171 ms 58400 KB Output is correct
102 Correct 1157 ms 57472 KB Output is correct
103 Correct 1775 ms 107780 KB Output is correct
104 Correct 2276 ms 139864 KB Output is correct
105 Correct 130 ms 9376 KB Output is correct
106 Correct 1839 ms 108740 KB Output is correct
107 Correct 706 ms 31248 KB Output is correct
108 Correct 330 ms 14876 KB Output is correct
109 Correct 21 ms 6568 KB Output is correct
110 Correct 101 ms 8736 KB Output is correct
111 Correct 322 ms 15388 KB Output is correct
112 Correct 2699 ms 161604 KB Output is correct
113 Correct 2698 ms 162616 KB Output is correct
114 Correct 317 ms 14812 KB Output is correct
115 Correct 1046 ms 56692 KB Output is correct
116 Correct 2501 ms 148560 KB Output is correct
117 Correct 1765 ms 105688 KB Output is correct
118 Correct 1643 ms 94316 KB Output is correct
119 Correct 2380 ms 139664 KB Output is correct
120 Correct 1518 ms 77548 KB Output is correct