Submission #1049952

# Submission time Handle Problem Language Result Execution time Memory
1049952 2024-08-09T06:19:55 Z avighna Relay Marathon (NOI20_relaymarathon) C++17
100 / 100
1483 ms 277464 KB
#include <bits/stdc++.h>

typedef long long ll;

const ll inf = 1e15;

struct Edge {
  ll u, v, w;
};

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);

  ll n, m, k;
  std::cin >> n >> m >> k;
  std::vector<std::vector<std::pair<ll, ll>>> adj(n + 1);
  std::vector<Edge> edges;
  for (ll i = 0, u, v, w; i < m; ++i) {
    std::cin >> u >> v >> w;
    adj[u].push_back({v, w});
    adj[v].push_back({u, w});
    edges.push_back({u, v, w});
  }
  std::vector<ll> A(k);
  std::vector<bool> special(n + 1);
  for (auto &i : A) {
    std::cin >> i;
    special[i] = true;
  }

  auto get_best = [&]() {
    std::priority_queue<std::pair<ll, std::pair<ll, ll>>> pq;
    std::vector<ll> d(n + 1, inf);
    for (auto &i : A) {
      pq.push({0, {i, i}});
      d[i] = 0;
    }
    std::vector<std::pair<ll, ll>> closest_special(n + 1);
    std::vector<bool> vis(n + 1);
    while (!pq.empty()) {
      ll node = pq.top().second.first, source = pq.top().second.second,
         dist = -pq.top().first;
      pq.pop();
      if (vis[node]) {
        continue;
      }
      vis[node] = true;
      closest_special[node] = {source, dist};
      for (auto &[i, i_d] : adj[node]) {
        if (dist + i_d < d[i]) {
          d[i] = dist + i_d;
          pq.push({-d[i], {i, source}});
        }
      }
    }

    std::pair<ll, std::pair<ll, ll>> ans = {inf, {0, 0}};
    for (auto &[u, v, w] : edges) {
      if (special[u] and special[v]) {
        ans = std::min(ans, {w, {u, v}});
      }
    }
    for (auto &[u, v, w] : edges) {
      if (closest_special[u].first != closest_special[v].first) {
        ans = std::min(
            ans, {closest_special[u].second + w + closest_special[v].second,
                  {closest_special[u].first, closest_special[v].first}});
      }
    }
    return ans;
  };

  auto sssp = [&](ll x) {
    std::priority_queue<std::pair<ll, ll>> pq;
    pq.push({0, x});
    std::vector<ll> ans(n + 1, inf);
    std::vector<bool> vis(n + 1);
    ans[x] = 0;
    while (!pq.empty()) {
      ll node = pq.top().second, d = -pq.top().first;
      pq.pop();
      if (vis[node]) {
        continue;
      }
      vis[node] = true;
      for (auto &[i, dist] : adj[node]) {
        if (d + dist < ans[i]) {
          ans[i] = d + dist;
          pq.push({-ans[i], i});
        }
      }
    }
    return ans;
  };

  auto best = get_best();
  auto &[u, v] = best.second;
  special[u] = special[v] = false;
  A.erase(std::remove(A.begin(), A.end(), u), A.end());
  A.erase(std::remove(A.begin(), A.end(), v), A.end());
  auto second_best = get_best();
  ll ans = best.first + second_best.first;
  auto sssp1 = sssp(u);
  std::vector<std::pair<ll, ll>> best1;
  for (ll x = 1; x <= n; ++x) {
    if (x != v and special[x]) {
      best1.push_back({sssp1[x], x});
    }
  }
  auto sssp2 = sssp(v);
  std::vector<std::pair<ll, ll>> best2;
  for (ll y = 1; y <= n; ++y) {
    if (y != u and special[y]) {
      best2.push_back({sssp2[y], y});
    }
  }
  std::sort(best1.begin(), best1.end());
  std::sort(best2.begin(), best2.end());
  for (ll i = 0; i < std::min(best1.size(), size_t(5)); ++i) {
    for (ll j = 0; j < std::min(best2.size(), size_t(5)); ++j) {
      if (best1[i].second != best2[j].second) {
        ans = std::min(ans, best1[i].first + best2[j].first);
      }
    }
  }
  std::cout << ans << '\n';
}

Compilation message

RelayMarathon.cpp: In function 'int main()':
RelayMarathon.cpp:120:20: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'const long unsigned int' [-Wsign-compare]
  120 |   for (ll i = 0; i < std::min(best1.size(), size_t(5)); ++i) {
      |                  ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
RelayMarathon.cpp:121:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'const long unsigned int' [-Wsign-compare]
  121 |     for (ll j = 0; j < std::min(best2.size(), size_t(5)); ++j) {
      |                    ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 464 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 456 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 456 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 600 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 464 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 456 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 456 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 600 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 2 ms 860 KB Output is correct
37 Correct 2 ms 924 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 16 ms 9424 KB Output is correct
40 Correct 4 ms 2200 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 4 ms 2200 KB Output is correct
43 Correct 1 ms 604 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 0 ms 348 KB Output is correct
46 Correct 19 ms 9740 KB Output is correct
47 Correct 3 ms 1684 KB Output is correct
48 Correct 14 ms 9488 KB Output is correct
49 Correct 20 ms 8208 KB Output is correct
50 Correct 1 ms 348 KB Output is correct
51 Correct 1 ms 348 KB Output is correct
52 Correct 1 ms 348 KB Output is correct
53 Correct 10 ms 4440 KB Output is correct
54 Correct 18 ms 9232 KB Output is correct
55 Correct 1 ms 348 KB Output is correct
56 Correct 0 ms 348 KB Output is correct
57 Correct 1 ms 344 KB Output is correct
58 Correct 24 ms 8972 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 12808 KB Output is correct
2 Correct 4 ms 5980 KB Output is correct
3 Correct 761 ms 243680 KB Output is correct
4 Correct 386 ms 105192 KB Output is correct
5 Correct 118 ms 24440 KB Output is correct
6 Correct 71 ms 19344 KB Output is correct
7 Correct 104 ms 26616 KB Output is correct
8 Correct 35 ms 12808 KB Output is correct
9 Correct 63 ms 17480 KB Output is correct
10 Correct 47 ms 14344 KB Output is correct
11 Correct 891 ms 243420 KB Output is correct
12 Correct 52 ms 14600 KB Output is correct
13 Correct 228 ms 63868 KB Output is correct
14 Correct 112 ms 25804 KB Output is correct
15 Correct 811 ms 242908 KB Output is correct
16 Correct 26 ms 10980 KB Output is correct
17 Correct 609 ms 163272 KB Output is correct
18 Correct 5 ms 6036 KB Output is correct
19 Correct 894 ms 249100 KB Output is correct
20 Correct 120 ms 25112 KB Output is correct
21 Correct 95 ms 22688 KB Output is correct
22 Correct 72 ms 13508 KB Output is correct
23 Correct 13 ms 9188 KB Output is correct
24 Correct 648 ms 175856 KB Output is correct
25 Correct 73 ms 17764 KB Output is correct
26 Correct 42 ms 12032 KB Output is correct
27 Correct 55 ms 15480 KB Output is correct
28 Correct 13 ms 7652 KB Output is correct
29 Correct 139 ms 26248 KB Output is correct
30 Correct 215 ms 49908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 464 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 456 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 456 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 600 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 2 ms 860 KB Output is correct
37 Correct 2 ms 924 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 16 ms 9424 KB Output is correct
40 Correct 4 ms 2200 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 4 ms 2200 KB Output is correct
43 Correct 1 ms 604 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 0 ms 348 KB Output is correct
46 Correct 19 ms 9740 KB Output is correct
47 Correct 3 ms 1684 KB Output is correct
48 Correct 14 ms 9488 KB Output is correct
49 Correct 20 ms 8208 KB Output is correct
50 Correct 1 ms 348 KB Output is correct
51 Correct 1 ms 348 KB Output is correct
52 Correct 1 ms 348 KB Output is correct
53 Correct 10 ms 4440 KB Output is correct
54 Correct 18 ms 9232 KB Output is correct
55 Correct 1 ms 348 KB Output is correct
56 Correct 0 ms 348 KB Output is correct
57 Correct 1 ms 344 KB Output is correct
58 Correct 24 ms 8972 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 1 ms 604 KB Output is correct
61 Correct 33 ms 12808 KB Output is correct
62 Correct 4 ms 5980 KB Output is correct
63 Correct 761 ms 243680 KB Output is correct
64 Correct 386 ms 105192 KB Output is correct
65 Correct 118 ms 24440 KB Output is correct
66 Correct 71 ms 19344 KB Output is correct
67 Correct 104 ms 26616 KB Output is correct
68 Correct 35 ms 12808 KB Output is correct
69 Correct 63 ms 17480 KB Output is correct
70 Correct 47 ms 14344 KB Output is correct
71 Correct 891 ms 243420 KB Output is correct
72 Correct 52 ms 14600 KB Output is correct
73 Correct 228 ms 63868 KB Output is correct
74 Correct 112 ms 25804 KB Output is correct
75 Correct 811 ms 242908 KB Output is correct
76 Correct 26 ms 10980 KB Output is correct
77 Correct 609 ms 163272 KB Output is correct
78 Correct 5 ms 6036 KB Output is correct
79 Correct 894 ms 249100 KB Output is correct
80 Correct 120 ms 25112 KB Output is correct
81 Correct 95 ms 22688 KB Output is correct
82 Correct 72 ms 13508 KB Output is correct
83 Correct 13 ms 9188 KB Output is correct
84 Correct 648 ms 175856 KB Output is correct
85 Correct 73 ms 17764 KB Output is correct
86 Correct 42 ms 12032 KB Output is correct
87 Correct 55 ms 15480 KB Output is correct
88 Correct 13 ms 7652 KB Output is correct
89 Correct 139 ms 26248 KB Output is correct
90 Correct 215 ms 49908 KB Output is correct
91 Correct 173 ms 17152 KB Output is correct
92 Correct 1278 ms 258244 KB Output is correct
93 Correct 271 ms 25496 KB Output is correct
94 Correct 838 ms 152136 KB Output is correct
95 Correct 4 ms 6040 KB Output is correct
96 Correct 6 ms 6248 KB Output is correct
97 Correct 246 ms 36040 KB Output is correct
98 Correct 155 ms 16584 KB Output is correct
99 Correct 99 ms 18176 KB Output is correct
100 Correct 1483 ms 273472 KB Output is correct
101 Correct 676 ms 103400 KB Output is correct
102 Correct 591 ms 100328 KB Output is correct
103 Correct 923 ms 187092 KB Output is correct
104 Correct 1159 ms 243844 KB Output is correct
105 Correct 92 ms 12300 KB Output is correct
106 Correct 965 ms 188392 KB Output is correct
107 Correct 379 ms 53700 KB Output is correct
108 Correct 214 ms 25172 KB Output is correct
109 Correct 17 ms 8212 KB Output is correct
110 Correct 96 ms 12584 KB Output is correct
111 Correct 208 ms 24824 KB Output is correct
112 Correct 1425 ms 274308 KB Output is correct
113 Correct 1449 ms 277464 KB Output is correct
114 Correct 208 ms 24844 KB Output is correct
115 Correct 581 ms 96112 KB Output is correct
116 Correct 1232 ms 254268 KB Output is correct
117 Correct 1052 ms 181888 KB Output is correct
118 Correct 932 ms 161508 KB Output is correct
119 Correct 1291 ms 242116 KB Output is correct
120 Correct 808 ms 134672 KB Output is correct