제출 #926783

#제출 시각아이디문제언어결과실행 시간메모리
926783LonlyRCommuter Pass (JOI18_commuter_pass)C++17
100 / 100
211 ms42140 KiB
#include<bits/stdc++.h> #define int long long #define ii pair<int,int> #define ff first #define ss second using namespace std; const int maxn = 2e5 + 5; int n, m, s, t, a, b, ans = 1e18; int d[4][maxn], vis[maxn]; ii dp[maxn]; vector<ii> adj[maxn]; vector<int> dag[maxn]; void bfs(int x) /// dijkstra { int y; if (x == t) y = 1; else if (x == a) y = 2; else y = 3; memset(d[y], 0x3f, sizeof d[y]); d[y][x] = 0; priority_queue<ii, vector<ii>, greater<ii>> pq; pq.emplace(0, x); while (pq.size()) { int u, v; tie(v, u) = pq.top(); pq.pop(); if (v != d[y][u]) continue; for (auto p : adj[u]) if (d[y][p.ff] > v + p.ss) pq.emplace(d[y][p.ff] = v + p.ss, p.ff); } } void dfs(int x) { if (vis[x]) return; vis[x] = 1; for (auto p : adj[x]) if (d[1][x] == d[1][p.ff] + p.ss) dag[x].emplace_back(p.ff), dfs(p.ff); } ii cal(int x) { if (vis[x]) return dp[x]; vis[x] = 1; dp[x].ff = d[3][x]; dp[x].ss = d[2][x]; for (int i : dag[x]) { int u, v; tie(u, v) = cal(i); dp[x].ff = min(dp[x].ff, u); dp[x].ss = min(dp[x].ss, v); } ans = min(ans, d[2][x] + dp[x].ff); ans = min(ans, d[3][x] + dp[x].ss); return dp[x]; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); // freopen("test.inp", "r", stdin); // freopen("test.out", "w", stdout); cin >> n >> m >> s >> t >> a >> b; for (int i = 1, u, v, w; i <= m; i++) cin >> u >> v >> w, adj[u].emplace_back(v, w), adj[v].emplace_back(u, w); bfs(t); bfs(a); bfs(b); dfs(s); memset(vis, 0, sizeof vis); cal(s); ans = min(ans, d[2][b]); 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...