Submission #670204

# Submission time Handle Problem Language Result Execution time Memory
670204 2022-12-08T09:43:02 Z Forested Relay Marathon (NOI20_relaymarathon) C++17
100 / 100
2522 ms 232660 KB
#ifndef LOCAL
#define FAST_IO
#endif

// ============
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define OVERRIDE(a, b, c, d, ...) d
#define REP2(i, n) for (i32 i = 0; i < (i32) (n); ++i)
#define REP3(i, m, n) for (i32 i = (i32) (m); i < (i32) (n); ++i)
#define REP(...) OVERRIDE(__VA_ARGS__, REP3, REP2)(__VA_ARGS__)
#define PER(i, n) for (i32 i = (i32) (n) - 1; i >= 0; --i)
#define ALL(x) begin(x), end(x)

using namespace std;

using u32 = unsigned int;
using u64 = unsigned long long;
using u128 = __uint128_t;
using i32 = signed int;
using i64 = signed long long;
using i128 = __int128_t;
using f64 = double;
using f80 = long double;

template <typename T>
using Vec = vector<T>;

template <typename T>
bool chmin(T &x, const T &y) {
    if (x > y) {
        x = y;
        return true;
    }
    return false;
}
template <typename T>
bool chmax(T &x, const T &y) {
    if (x < y) {
        x = y;
        return true;
    }
    return false;
}

istream &operator>>(istream &is, i128 &x) {
    i64 v;
    is >> v;
    x = v;
    return is;
}
ostream &operator<<(ostream &os, i128 x) {
    os << (i64) x;
    return os;
}
istream &operator>>(istream &is, u128 &x) {
    u64 v;
    is >> v;
    x = v;
    return is;
}
ostream &operator<<(ostream &os, u128 x) {
    os << (u64) x;
    return os;
}

[[maybe_unused]] constexpr i32 INF = 1000000100;
[[maybe_unused]] constexpr i64 INF64 = 3000000000000000100;
struct SetUpIO {
    SetUpIO() {
#ifdef FAST_IO
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
#endif
        cout << fixed << setprecision(15);
    }
} set_up_io;
// ============

#ifdef DEBUGF
#else
#define DBG(x) (void) 0
#endif

constexpr i32 SZ = 4;

bool update(array<Vec<pair<i64, i32>>, SZ> &dist, i32 v, i64 d, i32 f) {
    REP(i, SZ) {
        if (dist[i][v].second == f) {
            if (d >= dist[i][v].first) {
                return false;
            }
            dist[i][v].first = d;
            i32 cur = i;
            while (cur > 0 && dist[cur - 1][v].first > dist[cur][v].first) {
                swap(dist[cur - 1][v], dist[cur][v]);
                --cur;
            }
            return true;
        }
    }
    if (d >= dist[SZ - 1][v].first) {
        return false;
    }
    dist[SZ - 1][v] = pair<i64, i32>(d, f);
    i32 cur = SZ - 1;
    while (cur > 0 && dist[cur - 1][v].first > dist[cur][v].first) {
        swap(dist[cur - 1][v], dist[cur][v]);
        --cur;
    }
    return true;
}

int main() {
    i32 n, m, k;
    cin >> n >> m >> k;
    Vec<Vec<pair<i32, i64>>> g(n);
    REP(ei, m) {
        i32 u, v, w;
        cin >> u >> v >> w;
        --u;
        --v;
        g[u].emplace_back(v, w);
        g[v].emplace_back(u, w);
    }
    Vec<i32> a(k);
    REP(i, k) {
        cin >> a[i];
        --a[i];
    }
    
    array<Vec<pair<i64, i32>>, SZ> dist;
    fill(ALL(dist), Vec<pair<i64, i32>>(n, pair<i64, i32>(INF64, -1)));
    priority_queue<tuple<i64, i32, i32>> pq;
    REP(i, k) {
        dist[0][a[i]] = pair<i64, i32>(0, i);
        pq.emplace(0, i, a[i]);
    }
    while (!pq.empty()) {
        auto [d, f, v] = pq.top();
        pq.pop();
        d *= -1;
        bool found = false;
        REP(i, SZ) {
            if (dist[i][v] == pair<i64, i32>(d, f)) {
                found = true;
            }
        }
        if (!found) {
            continue;
        }
        for (auto &[u, w] : g[v]) {
            if (update(dist, u, d + w, f)) {
                pq.emplace(-(d + w), f, u);
            }
        }
    }
    
    array<Vec<pair<i64, i32>>, SZ> adist;
    fill(ALL(adist), Vec<pair<i64, i32>>(k, pair<i64, i32>(INF64, -1)));
    REP(i, 4) REP(j, k) {
        adist[i][j] = dist[i][a[j]];
    }
    
    i64 closest = INF64;
    i32 cu = -1, cv = -1;
    REP(i, k) {
        if (chmin(closest, adist[1][i].first)) {
            cu = i;
            cv = adist[1][i].second;
        }
    }
    
    i64 exmin = INF64;
    REP(i, k) {
        if (i == cu || i == cv) {
            continue;
        }
        REP(j, 1, 4) {
            if (adist[j][i].second != cu && adist[j][i].second != cv) {
                chmin(exmin, adist[j][i].first);
            }
        }
    }
    
    i64 ans = closest + exmin;
    
    REP(i, 2, 4) REP(j, 2, 4) {
        if (adist[i][cu].second != adist[j][cv].second) {
            chmin(ans, adist[i][cu].first + adist[j][cv].first);
        }
    }
    
    cout << ans << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 316 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 0 ms 316 KB Output is correct
26 Correct 1 ms 316 KB Output is correct
27 Correct 0 ms 320 KB Output is correct
28 Correct 1 ms 324 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 316 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 0 ms 316 KB Output is correct
26 Correct 1 ms 316 KB Output is correct
27 Correct 0 ms 320 KB Output is correct
28 Correct 1 ms 324 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 0 ms 340 KB Output is correct
36 Correct 3 ms 676 KB Output is correct
37 Correct 3 ms 908 KB Output is correct
38 Correct 1 ms 468 KB Output is correct
39 Correct 27 ms 6120 KB Output is correct
40 Correct 7 ms 1748 KB Output is correct
41 Correct 1 ms 468 KB Output is correct
42 Correct 6 ms 1748 KB Output is correct
43 Correct 3 ms 596 KB Output is correct
44 Correct 1 ms 468 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 27 ms 6348 KB Output is correct
47 Correct 5 ms 1236 KB Output is correct
48 Correct 23 ms 6064 KB Output is correct
49 Correct 24 ms 6176 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 1 ms 468 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Correct 16 ms 3540 KB Output is correct
54 Correct 27 ms 6244 KB Output is correct
55 Correct 1 ms 340 KB Output is correct
56 Correct 1 ms 340 KB Output is correct
57 Correct 1 ms 340 KB Output is correct
58 Correct 30 ms 6464 KB Output is correct
59 Correct 0 ms 340 KB Output is correct
60 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 113 ms 15224 KB Output is correct
2 Correct 8 ms 11084 KB Output is correct
3 Correct 1995 ms 195336 KB Output is correct
4 Correct 1143 ms 92448 KB Output is correct
5 Correct 344 ms 26600 KB Output is correct
6 Correct 270 ms 23960 KB Output is correct
7 Correct 371 ms 28208 KB Output is correct
8 Correct 139 ms 15436 KB Output is correct
9 Correct 254 ms 20788 KB Output is correct
10 Correct 175 ms 16764 KB Output is correct
11 Correct 2153 ms 201880 KB Output is correct
12 Correct 198 ms 18620 KB Output is correct
13 Correct 776 ms 66332 KB Output is correct
14 Correct 356 ms 28292 KB Output is correct
15 Correct 2204 ms 198840 KB Output is correct
16 Correct 77 ms 14676 KB Output is correct
17 Correct 1508 ms 135520 KB Output is correct
18 Correct 8 ms 11092 KB Output is correct
19 Correct 2203 ms 205044 KB Output is correct
20 Correct 355 ms 27184 KB Output is correct
21 Correct 342 ms 25680 KB Output is correct
22 Correct 163 ms 16428 KB Output is correct
23 Correct 27 ms 13576 KB Output is correct
24 Correct 1518 ms 154880 KB Output is correct
25 Correct 258 ms 21104 KB Output is correct
26 Correct 122 ms 15312 KB Output is correct
27 Correct 201 ms 19072 KB Output is correct
28 Correct 14 ms 12188 KB Output is correct
29 Correct 366 ms 27616 KB Output is correct
30 Correct 599 ms 47520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 316 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 0 ms 316 KB Output is correct
26 Correct 1 ms 316 KB Output is correct
27 Correct 0 ms 320 KB Output is correct
28 Correct 1 ms 324 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 0 ms 340 KB Output is correct
36 Correct 3 ms 676 KB Output is correct
37 Correct 3 ms 908 KB Output is correct
38 Correct 1 ms 468 KB Output is correct
39 Correct 27 ms 6120 KB Output is correct
40 Correct 7 ms 1748 KB Output is correct
41 Correct 1 ms 468 KB Output is correct
42 Correct 6 ms 1748 KB Output is correct
43 Correct 3 ms 596 KB Output is correct
44 Correct 1 ms 468 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 27 ms 6348 KB Output is correct
47 Correct 5 ms 1236 KB Output is correct
48 Correct 23 ms 6064 KB Output is correct
49 Correct 24 ms 6176 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 1 ms 468 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Correct 16 ms 3540 KB Output is correct
54 Correct 27 ms 6244 KB Output is correct
55 Correct 1 ms 340 KB Output is correct
56 Correct 1 ms 340 KB Output is correct
57 Correct 1 ms 340 KB Output is correct
58 Correct 30 ms 6464 KB Output is correct
59 Correct 0 ms 340 KB Output is correct
60 Correct 1 ms 468 KB Output is correct
61 Correct 113 ms 15224 KB Output is correct
62 Correct 8 ms 11084 KB Output is correct
63 Correct 1995 ms 195336 KB Output is correct
64 Correct 1143 ms 92448 KB Output is correct
65 Correct 344 ms 26600 KB Output is correct
66 Correct 270 ms 23960 KB Output is correct
67 Correct 371 ms 28208 KB Output is correct
68 Correct 139 ms 15436 KB Output is correct
69 Correct 254 ms 20788 KB Output is correct
70 Correct 175 ms 16764 KB Output is correct
71 Correct 2153 ms 201880 KB Output is correct
72 Correct 198 ms 18620 KB Output is correct
73 Correct 776 ms 66332 KB Output is correct
74 Correct 356 ms 28292 KB Output is correct
75 Correct 2204 ms 198840 KB Output is correct
76 Correct 77 ms 14676 KB Output is correct
77 Correct 1508 ms 135520 KB Output is correct
78 Correct 8 ms 11092 KB Output is correct
79 Correct 2203 ms 205044 KB Output is correct
80 Correct 355 ms 27184 KB Output is correct
81 Correct 342 ms 25680 KB Output is correct
82 Correct 163 ms 16428 KB Output is correct
83 Correct 27 ms 13576 KB Output is correct
84 Correct 1518 ms 154880 KB Output is correct
85 Correct 258 ms 21104 KB Output is correct
86 Correct 122 ms 15312 KB Output is correct
87 Correct 201 ms 19072 KB Output is correct
88 Correct 14 ms 12188 KB Output is correct
89 Correct 366 ms 27616 KB Output is correct
90 Correct 599 ms 47520 KB Output is correct
91 Correct 234 ms 20004 KB Output is correct
92 Correct 2158 ms 216528 KB Output is correct
93 Correct 400 ms 28260 KB Output is correct
94 Correct 1389 ms 126732 KB Output is correct
95 Correct 8 ms 11092 KB Output is correct
96 Correct 8 ms 11212 KB Output is correct
97 Correct 465 ms 37468 KB Output is correct
98 Correct 233 ms 19996 KB Output is correct
99 Correct 268 ms 21636 KB Output is correct
100 Correct 2346 ms 230844 KB Output is correct
101 Correct 1028 ms 91612 KB Output is correct
102 Correct 1062 ms 90656 KB Output is correct
103 Correct 1560 ms 159196 KB Output is correct
104 Correct 1922 ms 187916 KB Output is correct
105 Correct 143 ms 15564 KB Output is correct
106 Correct 1651 ms 158476 KB Output is correct
107 Correct 667 ms 50464 KB Output is correct
108 Correct 356 ms 26544 KB Output is correct
109 Correct 18 ms 12756 KB Output is correct
110 Correct 108 ms 14896 KB Output is correct
111 Correct 351 ms 27084 KB Output is correct
112 Correct 2522 ms 230980 KB Output is correct
113 Correct 2357 ms 232660 KB Output is correct
114 Correct 385 ms 26416 KB Output is correct
115 Correct 1054 ms 89104 KB Output is correct
116 Correct 2208 ms 212240 KB Output is correct
117 Correct 1602 ms 155900 KB Output is correct
118 Correct 1511 ms 139124 KB Output is correct
119 Correct 2082 ms 191216 KB Output is correct
120 Correct 1374 ms 113320 KB Output is correct