Submission #1082669

# Submission time Handle Problem Language Result Execution time Memory
1082669 2024-09-01T05:23:54 Z avighna Aesthetic (NOI20_aesthetic) C++17
35 / 100
2000 ms 49236 KB
#include <bits/stdc++.h>

typedef long long ll;

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

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<ll> roads(m);
  for (ll i = 0, u, v, w; i < m; ++i) {
    std::cin >> u >> v >> w;
    adj[u].push_back({v, w, i});
    adj[v].push_back({u, w, i});
    roads[i] = w;
  }

  if (m == n - 1) {
    // there will only be one path
    std::set<ll> edges;
    std::function<bool(ll, ll)> dfs;
    ll def = 0;
    dfs = [&](ll node, ll par) {
      if (node == n) {
        return true;
      }
      for (auto &[ch, w, i] : adj[node]) {
        if (ch == par) {
          continue;
        }
        if (dfs(ch, node)) {
          edges.insert(i);
          def += w;
          return true;
        }
      }
      return false;
    };
    dfs(1, 0);

    ll ans = def;
    ll worst_road = roads[m - 1];
    for (ll i = m - 2; i >= 0; --i) {
      if (edges.count(i)) {
        ans = std::max(ans, def + worst_road);
      }
      worst_road = std::max(worst_road, roads[i]);
    }
    std::cout << ans << '\n';

    return 0;
  }

  if (m == n) {
    // find the cycle
    std::vector<std::pair<ll, ll>> up(n + 1);
    std::function<void(ll, ll)> dfs;
    std::vector<bool> vis(n + 1);
    bool found = false;
    ll begin_node = -1;
    dfs = [&](ll node, ll par) {
      if (found) {
        return;
      }
      if (vis[node]) {
        return;
      }
      vis[node] = true;
      for (auto &[ch, w, i] : adj[node]) {
        if (ch == par) {
          continue;
        }
        up[ch] = {node, i};
        if (vis[ch]) {
          begin_node = node;
          found = true;
          return;
        }
        dfs(ch, node);
        if (found) {
          return;
        }
      };
    };
    dfs(1, 0);
    std::set<ll> cycle_edegs;
    ll tot_cyc_wt = 0;
    if (found) {
      vis.assign(n + 1, false);
      while (!vis[begin_node]) {
        vis[begin_node] = true;
        cycle_edegs.insert(up[begin_node].second);
        tot_cyc_wt += roads[up[begin_node].second];
        begin_node = up[begin_node].first;
      }
    }

    std::set<ll> shortest_path_edges;
    ll shortest_cyc_wt = 0;
    ll def = 0;
    auto sssp = [&]() {
      std::vector<ll> ans(n + 1, inf);
      std::vector<bool> vis(n + 1);
      std::vector<std::pair<ll, ll>> up(n + 1);
      std::priority_queue<std::pair<ll, ll>> pq;
      pq.push({0, 1});
      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) {
            up[ch] = {node, cur_i};
            ans[ch] = d + w;
            pq.push({-ans[ch], ch});
          }
        }
      }
      def = ans[n];
      ll end = n;
      while (end != 1) {
        shortest_path_edges.insert(up[end].second);
        if (cycle_edegs.count(up[end].second)) {
          shortest_cyc_wt += roads[up[end].second];
        }
        end = up[end].first;
      }
    };
    sssp();

    ll ans = def;
    ll worst_road = roads[m - 1];
    for (ll i = m - 2; i >= 0; --i) {
      if (shortest_path_edges.count(i)) {
        if (cycle_edegs.count(i)) {
          ans = std::max(ans, std::min(def + worst_road,
                                       def - 2 * shortest_cyc_wt + tot_cyc_wt));
        } else {
          ans = std::max(ans, def + worst_road);
        }
      }
      worst_road = std::max(worst_road, roads[i]);
    }
    std::cout << ans << '\n';

    return 0;
  };

  // exclude edge i
  auto sssp = [&](ll i, ll add) {
    std::vector<ll> ans(n + 1, inf);
    std::vector<bool> vis(n + 1);
    std::priority_queue<std::pair<ll, ll>> pq;
    pq.push({0, 1});
    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 (cur_i == i) {
          w += add;
        }
        if (ans[ch] > d + w) {
          ans[ch] = d + w;
          pq.push({-ans[ch], ch});
        }
      }
    }
    return ans;
  };

  ll def = sssp(-1, 0)[n];
  ll ans = def;
  ll worst_road = roads[m - 1];
  for (ll i = m - 2; i >= 0; --i) {
    ans = std::max(ans, sssp(i, worst_road)[n]);
    worst_road = std::max(worst_road, roads[i]);
  }
  std::cout << ans << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 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 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 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 194 ms 600 KB Output is correct
10 Correct 205 ms 604 KB Output is correct
11 Correct 139 ms 604 KB Output is correct
12 Correct 141 ms 600 KB Output is correct
13 Correct 77 ms 604 KB Output is correct
14 Correct 77 ms 688 KB Output is correct
15 Correct 66 ms 604 KB Output is correct
16 Correct 65 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 131 ms 36692 KB Output is correct
2 Correct 155 ms 36948 KB Output is correct
3 Correct 142 ms 36576 KB Output is correct
4 Correct 130 ms 36432 KB Output is correct
5 Correct 129 ms 36692 KB Output is correct
6 Correct 155 ms 37688 KB Output is correct
7 Correct 144 ms 37460 KB Output is correct
8 Correct 135 ms 37456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 257 ms 49232 KB Output is correct
2 Correct 211 ms 48464 KB Output is correct
3 Correct 240 ms 48464 KB Output is correct
4 Correct 236 ms 49236 KB Output is correct
5 Correct 236 ms 48304 KB Output is correct
6 Correct 210 ms 48536 KB Output is correct
7 Correct 203 ms 48460 KB Output is correct
8 Correct 207 ms 48808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 2029 ms 36976 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2029 ms 36976 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 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 194 ms 600 KB Output is correct
10 Correct 205 ms 604 KB Output is correct
11 Correct 139 ms 604 KB Output is correct
12 Correct 141 ms 600 KB Output is correct
13 Correct 77 ms 604 KB Output is correct
14 Correct 77 ms 688 KB Output is correct
15 Correct 66 ms 604 KB Output is correct
16 Correct 65 ms 604 KB Output is correct
17 Correct 131 ms 36692 KB Output is correct
18 Correct 155 ms 36948 KB Output is correct
19 Correct 142 ms 36576 KB Output is correct
20 Correct 130 ms 36432 KB Output is correct
21 Correct 129 ms 36692 KB Output is correct
22 Correct 155 ms 37688 KB Output is correct
23 Correct 144 ms 37460 KB Output is correct
24 Correct 135 ms 37456 KB Output is correct
25 Correct 257 ms 49232 KB Output is correct
26 Correct 211 ms 48464 KB Output is correct
27 Correct 240 ms 48464 KB Output is correct
28 Correct 236 ms 49236 KB Output is correct
29 Correct 236 ms 48304 KB Output is correct
30 Correct 210 ms 48536 KB Output is correct
31 Correct 203 ms 48460 KB Output is correct
32 Correct 207 ms 48808 KB Output is correct
33 Execution timed out 2029 ms 36976 KB Time limit exceeded
34 Halted 0 ms 0 KB -