제출 #532169

#제출 시각아이디문제언어결과실행 시간메모리
532169MonarchuwuCommuter Pass (JOI18_commuter_pass)C++17
100 / 100
293 ms23328 KiB
#include<iostream> #include<algorithm> #include<vector> #include<queue> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, int> pli; #define ff first #define ss second const ll inf = 1e18; const int N = 1e5 + 9; int n, m, s, t, U, V; vector<pii> g[N]; vector<int> G[N]; ll dist[4][N]; void dijkstra(int u, ll dist[]) { fill(dist + 1, dist + n + 1, inf); priority_queue<pli> q; q.emplace(dist[u] = 0, u); while (!q.empty()) { ll d = -q.top().ff; u = q.top().ss; q.pop(); if (d != dist[u]) continue; for (pii v : g[u]) { if (dist[v.ff] > dist[u] + v.ss) { dist[v.ff] = dist[u] + v.ss; q.emplace(-dist[v.ff], v.ff); } } } } int idx[N]; bool cmp(int i, int j) { return dist[1][i] < dist[1][j]; } void init_idx() { for (int i = 1; i <= n; ++i) idx[i] = i; sort(idx + 1, idx + n + 1, cmp); } ll dp[2][N]; int main() { cin.tie(NULL)->sync_with_stdio(false); cin >> n >> m >> s >> t >> U >> V; for (int i = 0, u, v, c; i < m; ++i) { cin >> u >> v >> c; g[u].emplace_back(v, c); g[v].emplace_back(u, c); } dijkstra(s, dist[0]); dijkstra(t, dist[1]); for (int u = 1; u <= n; ++u) for (pii v : g[u]) if (dist[0][u] + v.ss + dist[1][v.ff] == dist[0][t]) G[u].push_back(v.ff); dijkstra(U, dist[2]); dijkstra(V, dist[3]); ll ans = dist[2][V]; init_idx(); for (int i = 1, u; i <= n; ++i) { u = idx[i]; //// U -> u -> v -> V // edge v -> V dp[0][u] = dist[3][u]; // edge u -> v for (int v : G[u]) dp[0][u] = min(dp[0][u], dp[0][v]); // edge U -> u ans = min(ans, dp[0][u] + dist[2][u]); //// U -> v -> u -> V // edge U -> v dp[1][u] = dist[2][u]; // edge v -> u for (int v : G[u]) dp[1][u] = min(dp[1][u], dp[1][v]); // edge u -> V ans = min(ans, dp[1][u] + dist[3][u]); } cout << ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...