제출 #207392

#제출 시각아이디문제언어결과실행 시간메모리
207392SaboonCommuter Pass (JOI18_commuter_pass)C++14
100 / 100
587 ms27300 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int,int> pii; const int maxn = 1e5 + 10; const int inf = 1e9; int n, m; vector<pair<int,ll>> g[maxn]; vector<int> G[maxn]; ll dp[maxn], pd[maxn]; bool mark[maxn]; int Q[maxn], tail, head; vector<int> builddag(int s, int t){ tail = 0, head = 0; mark[t] = 1, Q[head ++] = t; vector<int> ret; while (tail < head){ int v = Q[tail ++]; ret.push_back(v); for (auto ed : g[v]){ int u = ed.first, w = ed.second; if (!mark[u] and dp[u] + w == dp[v]) mark[u] = 1, Q[head ++] = u; if (dp[u] + w == dp[v]) G[v].push_back(u); } } sort(ret.begin(), ret.end(), [] (int v, int u){ return dp[v] < dp[u];} ); return ret; } void dijkstra(int src){ memset(dp, 63, sizeof dp); dp[src] = 0; set<pair<ll,int>> s; s.insert({dp[src], src}); while (!s.empty()){ int v = (*s.begin()).second; s.erase(s.begin()); for (auto ed : g[v]){ int u = ed.first, w = ed.second; if (dp[u] > dp[v] + w){ s.erase({dp[u], u}); dp[u] = dp[v] + w; s.insert({dp[u], u}); } } } } ll dpfr[maxn], dpto[maxn]; int main(){ ios_base::sync_with_stdio(false); int m; int s, t; int fr, to; cin >> n >> m >> s >> t >> fr >> to; for (int i = 0; i < m; i++){ int v, u, c; cin >> v >> u >> c; g[v].push_back({u, c}); g[u].push_back({v, c}); } dijkstra(fr); // keep shortest path data in dp for (int i = 1; i <= n; i++) dpfr[i] = dp[i]; dijkstra(to); // keep shortest path data in dp for (int i = 1; i <= n; i++) dpto[i] = dp[i]; dijkstra(s); // keep shortest path data in dp ll answer = dpfr[to]; vector<int> tpl = builddag(s, t); // topological sort of shortest path's DAG s to t for (int i = 0; i < (int)tpl.size(); i++){ int v = tpl[i]; dp[v] = dpfr[v], pd[v] = dpto[v]; for (auto u : G[v]){ dp[v] = min(dp[v], dp[u]); pd[v] = min(pd[v], pd[u]); } ll now = min(dp[v] + dpto[v], pd[v] + dpfr[v]); if (answer == -1 or answer > now) answer = now; } cout << answer << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...