제출 #1351077

#제출 시각아이디문제언어결과실행 시간메모리
1351077cpismayilmmdv985Commuter Pass (JOI18_commuter_pass)C++20
100 / 100
180 ms31948 KiB
#ifdef LOCAL
#include ".debug.hpp"
#else
#define debug(...) 42
#endif
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
using ordered_set = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;

const int MXN = 1e5 + 5;
vector<array<int64_t, 2>> adj[MXN], DAG[MXN];
vector<int> par[MXN];
const int64_t INF = 1e18;
int64_t N, M, S, T, U, V, dist[4][MXN], dp[2][MXN]; // dp[0] -> U, dp[1] -> V
bool isDAG[MXN], vis[MXN];

void dijkstra(vector<array<int64_t, 2>> graph[], const int &root, const int &type) { // type = 0 -> S, 1 -> T, 2 -> U, 3 -> V
   for (int i = 1; i <= N; i++)  dist[type][i] = INF, vis[i] = false;
   priority_queue<array<int64_t, 2>, vector<array<int64_t, 2>>, greater<array<int64_t, 2>>> pq;
   pq.push({0, root}), dist[type][root] = 0;
   while (!pq.empty()) {
      int node = pq.top()[1]; pq.pop();
      if (vis[node]) continue;
      vis[node] = true;
      for (auto &[child, cost] : graph[node])
         if (dist[type][child] > dist[type][node] + cost)   dist[type][child] = dist[type][node] + cost, pq.push({dist[type][child], child});
   }
}

signed main() {
#ifndef VOID
   cin.tie(nullptr)->sync_with_stdio(false);
#endif

   cin >> N >> M >> S >> T >> U >> V;
   for (int i = 0, u, v, w; i < M; i++)   cin >> u >> v >> w, adj[u].push_back({v, w}), adj[v].push_back({u, w});
   dijkstra(adj, S, 0), dijkstra(adj, T, 1); dijkstra(adj, U, 2), dijkstra(adj, V, 3);
   for (int i = 1; i <= N; i++) {
      for (auto &[j, w] : adj[i])  if (dist[0][i] + w + dist[1][j] == dist[0][T]) {
         DAG[i].push_back({j, w}), par[j].push_back(i), isDAG[i] = isDAG[j] = true;
      }
   }
   vector<array<int64_t, 2>> order;
   for (int i = 1; i <= N; i++)  order.push_back({dist[0][i], i});
   sort(order.begin(), order.end());
   memset(dp, 0x3f, sizeof(dp));
   for (auto &[d, node] : order) {
      dp[0][node] = dist[2][node], dp[1][node] = dist[3][node];
      for (auto &p : par[node])  dp[0][node] = min(dp[0][node], dp[0][p]), dp[1][node] = min(dp[1][node], dp[1][p]);
   }
   int64_t ans = dist[2][V];
   for (int i = 1; i <= N; i++)
      if (isDAG[i])  ans = min({ans, dp[0][i] + dist[3][i], dp[1][i] + dist[2][i]});
   cout << ans;

   return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...