Submission #1082988

# Submission time Handle Problem Language Result Execution time Memory
1082988 2024-09-02T09:38:03 Z avighna Aesthetic (NOI20_aesthetic) C++17
100 / 100
359 ms 65764 KB
#include <bits/stdc++.h>

typedef long long ll;

struct To {
  ll v, w;
  ll i;
};

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

const ll inf = 1e15;

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

  ll n, m;
  std::cin >> n >> m;
  std::vector<std::vector<To>> adj(n + 1);
  std::vector<Edge> edges;
  ll max_w = 0;
  for (ll i = 0, u, v, w; i < m; ++i) {
    std::cin >> u >> v >> w;
    max_w = std::max(max_w, w);
    adj[u].push_back({v, w, i});
    adj[v].push_back({u, w, i});
    edges.push_back({u, v, w});
  }

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

  auto sssp1 = sssp(1);
  auto ssspn = sssp(n);

  std::vector<bool> active_edge(m), is_bridge(m), vis(n + 1), enc(n + 1);
  std::vector<ll> low(n + 1), tin(n + 1);

  auto check = [&](ll x) {
    for (ll i = 0; i < m; ++i) {
      auto [u, v, w] = edges[i];
      active_edge[i] =
          sssp1[u] + w + ssspn[v] <= x or sssp1[v] + w + ssspn[u] <= x;
    }
    vis.assign(n + 1, false);
    enc.assign(n + 1, false);
    enc[n] = true;
    ll timer = 0;
    std::function<void(ll, ll)> dfs;
    dfs = [&](ll node, ll par) {
      if (vis[node]) {
        return;
      }
      vis[node] = true;
      tin[node] = timer++;
      low[node] = tin[node];
      for (auto &[ch, w, i] : adj[node]) {
        if (ch == par or !active_edge[i]) {
          continue;
        }
        if (!vis[ch]) {
          dfs(ch, node);
          enc[node] = enc[node] || enc[ch];
        }
        is_bridge[i] = low[ch] > tin[node] and enc[ch];
        low[node] = std::min(low[node], low[ch]);
      }
    };
    dfs(1, 0);
    ll worst_road = edges[m - 1].w;
    for (ll i = m - 2; i >= 0; --i) {
      auto [u, v, w] = edges[i];
      if (is_bridge[i] and
          std::min(sssp1[u] + ssspn[v], sssp1[v] + ssspn[u]) + w + worst_road >
              x) {
        return true;
      }
      worst_road = std::max(worst_road, edges[i].w);
    }
    return false;
  };

  ll s = sssp1[n], e = sssp1[n] + max_w;
  while (s < e) {
    ll m = s + (e - s) / 2;
    if (check(m)) {
      s = m + 1;
    } else {
      e = m;
    }
  }
  std::cout << s << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 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 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 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 348 KB Output is correct
3 Correct 1 ms 348 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 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 2 ms 604 KB Output is correct
11 Correct 2 ms 668 KB Output is correct
12 Correct 2 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 2 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 280 ms 43936 KB Output is correct
2 Correct 283 ms 44268 KB Output is correct
3 Correct 289 ms 43748 KB Output is correct
4 Correct 320 ms 43660 KB Output is correct
5 Correct 290 ms 44056 KB Output is correct
6 Correct 318 ms 44872 KB Output is correct
7 Correct 359 ms 44520 KB Output is correct
8 Correct 307 ms 45028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 291 ms 45028 KB Output is correct
2 Correct 318 ms 44356 KB Output is correct
3 Correct 290 ms 44168 KB Output is correct
4 Correct 302 ms 44736 KB Output is correct
5 Correct 291 ms 43932 KB Output is correct
6 Correct 303 ms 44080 KB Output is correct
7 Correct 325 ms 44008 KB Output is correct
8 Correct 290 ms 44516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 236 ms 40344 KB Output is correct
2 Correct 190 ms 44996 KB Output is correct
3 Correct 151 ms 50660 KB Output is correct
4 Correct 190 ms 50668 KB Output is correct
5 Correct 153 ms 50660 KB Output is correct
6 Correct 212 ms 50916 KB Output is correct
7 Correct 187 ms 50916 KB Output is correct
8 Correct 200 ms 51172 KB Output is correct
9 Correct 197 ms 50920 KB Output is correct
10 Correct 164 ms 51432 KB Output is correct
11 Correct 166 ms 51328 KB Output is correct
12 Correct 220 ms 45028 KB Output is correct
13 Correct 177 ms 51124 KB Output is correct
14 Correct 99 ms 58856 KB Output is correct
15 Correct 91 ms 58920 KB Output is correct
16 Correct 245 ms 44968 KB Output is correct
17 Correct 218 ms 43748 KB Output is correct
18 Correct 233 ms 44516 KB Output is correct
19 Correct 151 ms 45028 KB Output is correct
20 Correct 150 ms 45088 KB Output is correct
21 Correct 160 ms 45140 KB Output is correct
22 Correct 145 ms 45004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 236 ms 40344 KB Output is correct
2 Correct 190 ms 44996 KB Output is correct
3 Correct 151 ms 50660 KB Output is correct
4 Correct 190 ms 50668 KB Output is correct
5 Correct 153 ms 50660 KB Output is correct
6 Correct 212 ms 50916 KB Output is correct
7 Correct 187 ms 50916 KB Output is correct
8 Correct 200 ms 51172 KB Output is correct
9 Correct 197 ms 50920 KB Output is correct
10 Correct 164 ms 51432 KB Output is correct
11 Correct 166 ms 51328 KB Output is correct
12 Correct 220 ms 45028 KB Output is correct
13 Correct 177 ms 51124 KB Output is correct
14 Correct 99 ms 58856 KB Output is correct
15 Correct 91 ms 58920 KB Output is correct
16 Correct 245 ms 44968 KB Output is correct
17 Correct 218 ms 43748 KB Output is correct
18 Correct 233 ms 44516 KB Output is correct
19 Correct 151 ms 45028 KB Output is correct
20 Correct 150 ms 45088 KB Output is correct
21 Correct 160 ms 45140 KB Output is correct
22 Correct 145 ms 45004 KB Output is correct
23 Correct 276 ms 45056 KB Output is correct
24 Correct 177 ms 45288 KB Output is correct
25 Correct 167 ms 44004 KB Output is correct
26 Correct 181 ms 42460 KB Output is correct
27 Correct 163 ms 42472 KB Output is correct
28 Correct 172 ms 44264 KB Output is correct
29 Correct 182 ms 42728 KB Output is correct
30 Correct 181 ms 43260 KB Output is correct
31 Correct 178 ms 43492 KB Output is correct
32 Correct 167 ms 42724 KB Output is correct
33 Correct 176 ms 44004 KB Output is correct
34 Correct 269 ms 44776 KB Output is correct
35 Correct 166 ms 43496 KB Output is correct
36 Correct 120 ms 63204 KB Output is correct
37 Correct 123 ms 65236 KB Output is correct
38 Correct 273 ms 44516 KB Output is correct
39 Correct 276 ms 44184 KB Output is correct
40 Correct 258 ms 45032 KB Output is correct
41 Correct 192 ms 45424 KB Output is correct
42 Correct 186 ms 45032 KB Output is correct
43 Correct 199 ms 45544 KB Output is correct
44 Correct 184 ms 45212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 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 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 2 ms 604 KB Output is correct
11 Correct 2 ms 668 KB Output is correct
12 Correct 2 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 2 ms 604 KB Output is correct
17 Correct 280 ms 43936 KB Output is correct
18 Correct 283 ms 44268 KB Output is correct
19 Correct 289 ms 43748 KB Output is correct
20 Correct 320 ms 43660 KB Output is correct
21 Correct 290 ms 44056 KB Output is correct
22 Correct 318 ms 44872 KB Output is correct
23 Correct 359 ms 44520 KB Output is correct
24 Correct 307 ms 45028 KB Output is correct
25 Correct 291 ms 45028 KB Output is correct
26 Correct 318 ms 44356 KB Output is correct
27 Correct 290 ms 44168 KB Output is correct
28 Correct 302 ms 44736 KB Output is correct
29 Correct 291 ms 43932 KB Output is correct
30 Correct 303 ms 44080 KB Output is correct
31 Correct 325 ms 44008 KB Output is correct
32 Correct 290 ms 44516 KB Output is correct
33 Correct 236 ms 40344 KB Output is correct
34 Correct 190 ms 44996 KB Output is correct
35 Correct 151 ms 50660 KB Output is correct
36 Correct 190 ms 50668 KB Output is correct
37 Correct 153 ms 50660 KB Output is correct
38 Correct 212 ms 50916 KB Output is correct
39 Correct 187 ms 50916 KB Output is correct
40 Correct 200 ms 51172 KB Output is correct
41 Correct 197 ms 50920 KB Output is correct
42 Correct 164 ms 51432 KB Output is correct
43 Correct 166 ms 51328 KB Output is correct
44 Correct 220 ms 45028 KB Output is correct
45 Correct 177 ms 51124 KB Output is correct
46 Correct 99 ms 58856 KB Output is correct
47 Correct 91 ms 58920 KB Output is correct
48 Correct 245 ms 44968 KB Output is correct
49 Correct 218 ms 43748 KB Output is correct
50 Correct 233 ms 44516 KB Output is correct
51 Correct 151 ms 45028 KB Output is correct
52 Correct 150 ms 45088 KB Output is correct
53 Correct 160 ms 45140 KB Output is correct
54 Correct 145 ms 45004 KB Output is correct
55 Correct 276 ms 45056 KB Output is correct
56 Correct 177 ms 45288 KB Output is correct
57 Correct 167 ms 44004 KB Output is correct
58 Correct 181 ms 42460 KB Output is correct
59 Correct 163 ms 42472 KB Output is correct
60 Correct 172 ms 44264 KB Output is correct
61 Correct 182 ms 42728 KB Output is correct
62 Correct 181 ms 43260 KB Output is correct
63 Correct 178 ms 43492 KB Output is correct
64 Correct 167 ms 42724 KB Output is correct
65 Correct 176 ms 44004 KB Output is correct
66 Correct 269 ms 44776 KB Output is correct
67 Correct 166 ms 43496 KB Output is correct
68 Correct 120 ms 63204 KB Output is correct
69 Correct 123 ms 65236 KB Output is correct
70 Correct 273 ms 44516 KB Output is correct
71 Correct 276 ms 44184 KB Output is correct
72 Correct 258 ms 45032 KB Output is correct
73 Correct 192 ms 45424 KB Output is correct
74 Correct 186 ms 45032 KB Output is correct
75 Correct 199 ms 45544 KB Output is correct
76 Correct 184 ms 45212 KB Output is correct
77 Correct 314 ms 47848 KB Output is correct
78 Correct 316 ms 47400 KB Output is correct
79 Correct 289 ms 53292 KB Output is correct
80 Correct 270 ms 52968 KB Output is correct
81 Correct 279 ms 52972 KB Output is correct
82 Correct 270 ms 52744 KB Output is correct
83 Correct 264 ms 53476 KB Output is correct
84 Correct 280 ms 53736 KB Output is correct
85 Correct 264 ms 52968 KB Output is correct
86 Correct 290 ms 53160 KB Output is correct
87 Correct 271 ms 53480 KB Output is correct
88 Correct 301 ms 47172 KB Output is correct
89 Correct 275 ms 53392 KB Output is correct
90 Correct 249 ms 64032 KB Output is correct
91 Correct 275 ms 65764 KB Output is correct
92 Correct 323 ms 47660 KB Output is correct
93 Correct 303 ms 46884 KB Output is correct
94 Correct 306 ms 46316 KB Output is correct
95 Correct 326 ms 47588 KB Output is correct
96 Correct 321 ms 47592 KB Output is correct
97 Correct 336 ms 47588 KB Output is correct
98 Correct 305 ms 47552 KB Output is correct