답안 #707259

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
707259 2023-03-08T17:31:44 Z lto5 Relay Marathon (NOI20_relaymarathon) C++14
100 / 100
1433 ms 129216 KB
#include <bits/stdc++.h>

using namespace std;
const int N = 1e5 + 5;
const int64_t INF = 0x3f3f3f3f3f3f3f3f;

template <typename T>
bool minimize(T& a, T b) {
  if (a > b) {
    return a = b, 1;
  }
  return 0;
}

int n, m, k;
vector<pair<int, int>> g[N];
int spec[N];

tuple<int64_t, int, int> best_pair() {
  vector<int64_t> d(n + 1, INF);
  vector<int> head(n + 1, 0);
  priority_queue<pair<int64_t, int>, vector<pair<int64_t, int>>, greater<pair<int64_t, int>>> pq;
  for (int i = 1; i <= n; i++) {
    if (spec[i]) {
      head[i] = i;
      d[i] = 0;
      pq.emplace(d[i], i);
    }
  }
  while (!pq.empty()) {
    auto [du, u] = pq.top();
    pq.pop();
    if (d[u] != du) {
      continue;
    }
    for (auto [v, w] : g[u]) {
      if (d[v] > d[u] + w) {
        d[v] = d[u] + w;
        head[v] = head[u];
        pq.emplace(d[v], v);
      }
    }
  }
  int64_t best = INF;
  int best_u = -1, best_v = -1;
  for (int u = 1; u <= n; u++) {
    for (auto [v, w] : g[u]) {
      if (d[u] != INF && d[v] != INF && head[u] != head[v]) {
        if (minimize(best, d[u] + d[v] + w)) {
          best = d[u] + d[v] + w;
          best_u = head[u];
          best_v = head[v];
        }
      }
    }
  }
  return {best, best_u, best_v};
}

pair<int64_t, int> nearest_spec(int s) {
  vector<int64_t> d(n + 1, INF);
  priority_queue<pair<int64_t, int>, vector<pair<int64_t, int>>, greater<pair<int64_t, int>>> pq;
  d[s] = 0;
  pq.emplace(d[s], s);
  while (!pq.empty()) {
    auto [du, u] = pq.top();
    pq.pop();
    if (d[u] != du) {
      continue;
    }
    if (spec[u]) {
      return {d[u], u};
    }
    for (auto [v, w] : g[u]) {
      if (d[v] > d[u] + w) {
        d[v] = d[u] + w;
        pq.emplace(d[v], v);
      }
    }
  }
  return {INF, 0};
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#define task "a"
  if (fopen(task ".inp", "r")) {
    freopen(task ".inp", "r", stdin);
    freopen(task ".out", "w", stdout);
  }
  cin >> n >> m >> k;
  while (m--) {
    int u, v, w;
    cin >> u >> v >> w;
    g[u].emplace_back(v, w);
    g[v].emplace_back(u, w);
  }
  while (k--) {
    int u;
    cin >> u;
    spec[u] = 1;
  }
  // {a, b, c, d}
  // d(a, b) + d(c, d) min
  // (x, y) = min pair
  /* {x, y} is subset of {a, b, c, d}
   * x != {a, b, c, d} or y != {a, b, c, d} : change something by {x, y} and get better answer
   */

  auto [min_d, x, y] = best_pair();
  int64_t best = INF;
  spec[x] = spec[y] = 0;
  // a = x, b = y
  {
    auto [second_min_d, z, t] = best_pair();
    minimize(best, min_d + second_min_d);
  }
  // a = x, c = y
  {
    auto [nearest_a, z] = nearest_spec(x);
    spec[z] = 0;
    auto [nearest_b, t] = nearest_spec(y);
    minimize(best, nearest_a + nearest_b);
    spec[z] = 1;
  }
  {
    auto [nearest_a, z] = nearest_spec(y);
    spec[z] = 0;
    auto [nearest_b, t] = nearest_spec(x);
    minimize(best, nearest_a + nearest_b);
    spec[z] = 1;
  }
  cout << best;
  return 0;
}

Compilation message

RelayMarathon.cpp: In function 'std::tuple<long int, int, int> best_pair()':
RelayMarathon.cpp:31:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   31 |     auto [du, u] = pq.top();
      |          ^
RelayMarathon.cpp:36:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   36 |     for (auto [v, w] : g[u]) {
      |               ^
RelayMarathon.cpp:47:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   47 |     for (auto [v, w] : g[u]) {
      |               ^
RelayMarathon.cpp: In function 'std::pair<long int, int> nearest_spec(int)':
RelayMarathon.cpp:66:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   66 |     auto [du, u] = pq.top();
      |          ^
RelayMarathon.cpp:74:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   74 |     for (auto [v, w] : g[u]) {
      |               ^
RelayMarathon.cpp: In function 'int main()':
RelayMarathon.cpp:112:8: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  112 |   auto [min_d, x, y] = best_pair();
      |        ^
RelayMarathon.cpp:117:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  117 |     auto [second_min_d, z, t] = best_pair();
      |          ^
RelayMarathon.cpp:122:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  122 |     auto [nearest_a, z] = nearest_spec(x);
      |          ^
RelayMarathon.cpp:124:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  124 |     auto [nearest_b, t] = nearest_spec(y);
      |          ^
RelayMarathon.cpp:129:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  129 |     auto [nearest_a, z] = nearest_spec(y);
      |          ^
RelayMarathon.cpp:131:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  131 |     auto [nearest_b, t] = nearest_spec(x);
      |          ^
RelayMarathon.cpp:90:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   90 |     freopen(task ".inp", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
RelayMarathon.cpp:91:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   91 |     freopen(task ".out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 1 ms 2672 KB Output is correct
6 Correct 2 ms 2672 KB Output is correct
7 Correct 2 ms 2688 KB Output is correct
8 Correct 2 ms 2688 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 1 ms 2680 KB Output is correct
11 Correct 1 ms 2676 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2668 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 1 ms 2644 KB Output is correct
21 Correct 2 ms 2684 KB Output is correct
22 Correct 1 ms 2644 KB Output is correct
23 Correct 2 ms 2644 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 1 ms 2644 KB Output is correct
27 Correct 1 ms 2644 KB Output is correct
28 Correct 2 ms 2644 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 2 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 1 ms 2672 KB Output is correct
6 Correct 2 ms 2672 KB Output is correct
7 Correct 2 ms 2688 KB Output is correct
8 Correct 2 ms 2688 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 1 ms 2680 KB Output is correct
11 Correct 1 ms 2676 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2668 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 1 ms 2644 KB Output is correct
21 Correct 2 ms 2684 KB Output is correct
22 Correct 1 ms 2644 KB Output is correct
23 Correct 2 ms 2644 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 1 ms 2644 KB Output is correct
27 Correct 1 ms 2644 KB Output is correct
28 Correct 2 ms 2644 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 2 ms 2644 KB Output is correct
31 Correct 2 ms 2644 KB Output is correct
32 Correct 2 ms 2684 KB Output is correct
33 Correct 3 ms 2644 KB Output is correct
34 Correct 2 ms 2644 KB Output is correct
35 Correct 2 ms 2644 KB Output is correct
36 Correct 3 ms 2772 KB Output is correct
37 Correct 3 ms 2948 KB Output is correct
38 Correct 2 ms 2644 KB Output is correct
39 Correct 19 ms 5896 KB Output is correct
40 Correct 5 ms 3412 KB Output is correct
41 Correct 2 ms 2644 KB Output is correct
42 Correct 6 ms 3464 KB Output is correct
43 Correct 2 ms 2772 KB Output is correct
44 Correct 2 ms 2644 KB Output is correct
45 Correct 2 ms 2680 KB Output is correct
46 Correct 23 ms 6128 KB Output is correct
47 Correct 5 ms 3156 KB Output is correct
48 Correct 20 ms 5840 KB Output is correct
49 Correct 20 ms 5908 KB Output is correct
50 Correct 2 ms 2644 KB Output is correct
51 Correct 2 ms 2644 KB Output is correct
52 Correct 2 ms 2644 KB Output is correct
53 Correct 13 ms 4308 KB Output is correct
54 Correct 21 ms 6000 KB Output is correct
55 Correct 2 ms 2644 KB Output is correct
56 Correct 2 ms 2668 KB Output is correct
57 Correct 2 ms 2644 KB Output is correct
58 Correct 26 ms 6200 KB Output is correct
59 Correct 2 ms 2644 KB Output is correct
60 Correct 2 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 54 ms 7144 KB Output is correct
2 Correct 6 ms 4540 KB Output is correct
3 Correct 1155 ms 66048 KB Output is correct
4 Correct 534 ms 37004 KB Output is correct
5 Correct 142 ms 11352 KB Output is correct
6 Correct 120 ms 9604 KB Output is correct
7 Correct 159 ms 12076 KB Output is correct
8 Correct 60 ms 7216 KB Output is correct
9 Correct 104 ms 9228 KB Output is correct
10 Correct 78 ms 7864 KB Output is correct
11 Correct 1234 ms 70924 KB Output is correct
12 Correct 86 ms 8172 KB Output is correct
13 Correct 344 ms 23620 KB Output is correct
14 Correct 167 ms 12436 KB Output is correct
15 Correct 1169 ms 69632 KB Output is correct
16 Correct 40 ms 6672 KB Output is correct
17 Correct 823 ms 55012 KB Output is correct
18 Correct 8 ms 4708 KB Output is correct
19 Correct 1282 ms 78244 KB Output is correct
20 Correct 146 ms 11996 KB Output is correct
21 Correct 143 ms 10876 KB Output is correct
22 Correct 67 ms 7784 KB Output is correct
23 Correct 21 ms 6168 KB Output is correct
24 Correct 915 ms 56840 KB Output is correct
25 Correct 107 ms 9348 KB Output is correct
26 Correct 55 ms 7172 KB Output is correct
27 Correct 86 ms 8600 KB Output is correct
28 Correct 13 ms 5460 KB Output is correct
29 Correct 164 ms 11944 KB Output is correct
30 Correct 324 ms 20288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 1 ms 2672 KB Output is correct
6 Correct 2 ms 2672 KB Output is correct
7 Correct 2 ms 2688 KB Output is correct
8 Correct 2 ms 2688 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 1 ms 2680 KB Output is correct
11 Correct 1 ms 2676 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
15 Correct 2 ms 2644 KB Output is correct
16 Correct 2 ms 2668 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 1 ms 2644 KB Output is correct
21 Correct 2 ms 2684 KB Output is correct
22 Correct 1 ms 2644 KB Output is correct
23 Correct 2 ms 2644 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 1 ms 2644 KB Output is correct
27 Correct 1 ms 2644 KB Output is correct
28 Correct 2 ms 2644 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 2 ms 2644 KB Output is correct
31 Correct 2 ms 2644 KB Output is correct
32 Correct 2 ms 2684 KB Output is correct
33 Correct 3 ms 2644 KB Output is correct
34 Correct 2 ms 2644 KB Output is correct
35 Correct 2 ms 2644 KB Output is correct
36 Correct 3 ms 2772 KB Output is correct
37 Correct 3 ms 2948 KB Output is correct
38 Correct 2 ms 2644 KB Output is correct
39 Correct 19 ms 5896 KB Output is correct
40 Correct 5 ms 3412 KB Output is correct
41 Correct 2 ms 2644 KB Output is correct
42 Correct 6 ms 3464 KB Output is correct
43 Correct 2 ms 2772 KB Output is correct
44 Correct 2 ms 2644 KB Output is correct
45 Correct 2 ms 2680 KB Output is correct
46 Correct 23 ms 6128 KB Output is correct
47 Correct 5 ms 3156 KB Output is correct
48 Correct 20 ms 5840 KB Output is correct
49 Correct 20 ms 5908 KB Output is correct
50 Correct 2 ms 2644 KB Output is correct
51 Correct 2 ms 2644 KB Output is correct
52 Correct 2 ms 2644 KB Output is correct
53 Correct 13 ms 4308 KB Output is correct
54 Correct 21 ms 6000 KB Output is correct
55 Correct 2 ms 2644 KB Output is correct
56 Correct 2 ms 2668 KB Output is correct
57 Correct 2 ms 2644 KB Output is correct
58 Correct 26 ms 6200 KB Output is correct
59 Correct 2 ms 2644 KB Output is correct
60 Correct 2 ms 2644 KB Output is correct
61 Correct 54 ms 7144 KB Output is correct
62 Correct 6 ms 4540 KB Output is correct
63 Correct 1155 ms 66048 KB Output is correct
64 Correct 534 ms 37004 KB Output is correct
65 Correct 142 ms 11352 KB Output is correct
66 Correct 120 ms 9604 KB Output is correct
67 Correct 159 ms 12076 KB Output is correct
68 Correct 60 ms 7216 KB Output is correct
69 Correct 104 ms 9228 KB Output is correct
70 Correct 78 ms 7864 KB Output is correct
71 Correct 1234 ms 70924 KB Output is correct
72 Correct 86 ms 8172 KB Output is correct
73 Correct 344 ms 23620 KB Output is correct
74 Correct 167 ms 12436 KB Output is correct
75 Correct 1169 ms 69632 KB Output is correct
76 Correct 40 ms 6672 KB Output is correct
77 Correct 823 ms 55012 KB Output is correct
78 Correct 8 ms 4708 KB Output is correct
79 Correct 1282 ms 78244 KB Output is correct
80 Correct 146 ms 11996 KB Output is correct
81 Correct 143 ms 10876 KB Output is correct
82 Correct 67 ms 7784 KB Output is correct
83 Correct 21 ms 6168 KB Output is correct
84 Correct 915 ms 56840 KB Output is correct
85 Correct 107 ms 9348 KB Output is correct
86 Correct 55 ms 7172 KB Output is correct
87 Correct 86 ms 8600 KB Output is correct
88 Correct 13 ms 5460 KB Output is correct
89 Correct 164 ms 11944 KB Output is correct
90 Correct 324 ms 20288 KB Output is correct
91 Correct 105 ms 10316 KB Output is correct
92 Correct 1306 ms 119380 KB Output is correct
93 Correct 171 ms 14968 KB Output is correct
94 Correct 853 ms 74100 KB Output is correct
95 Correct 7 ms 4588 KB Output is correct
96 Correct 9 ms 4796 KB Output is correct
97 Correct 220 ms 19768 KB Output is correct
98 Correct 107 ms 10408 KB Output is correct
99 Correct 128 ms 11416 KB Output is correct
100 Correct 1427 ms 129216 KB Output is correct
101 Correct 588 ms 51568 KB Output is correct
102 Correct 569 ms 50980 KB Output is correct
103 Correct 885 ms 87004 KB Output is correct
104 Correct 1183 ms 102352 KB Output is correct
105 Correct 64 ms 8220 KB Output is correct
106 Correct 922 ms 90316 KB Output is correct
107 Correct 324 ms 28300 KB Output is correct
108 Correct 146 ms 13796 KB Output is correct
109 Correct 16 ms 6020 KB Output is correct
110 Correct 53 ms 7748 KB Output is correct
111 Correct 153 ms 14212 KB Output is correct
112 Correct 1433 ms 128648 KB Output is correct
113 Correct 1375 ms 128972 KB Output is correct
114 Correct 182 ms 13624 KB Output is correct
115 Correct 453 ms 45816 KB Output is correct
116 Correct 1331 ms 116772 KB Output is correct
117 Correct 897 ms 85208 KB Output is correct
118 Correct 824 ms 78784 KB Output is correct
119 Correct 1251 ms 104132 KB Output is correct
120 Correct 775 ms 66068 KB Output is correct