Submission #530658

#TimeUsernameProblemLanguageResultExecution timeMemory
530658SlavicGCommuter Pass (JOI18_commuter_pass)C++17
100 / 100
438 ms23872 KiB
#include "bits/stdc++.h" using namespace std; #define ll long long #define forn(i,n) for(int i=0;i<n;i++) #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(),v.rend() #define pb push_back #define sz(a) (int)a.size() #define int long long const int N = 2e5 + 10; vector<pair<int, int>> adj[N]; ll ans = LLONG_MAX; int n, m, s, t, a, b; vector<int> order; vector<ll> dijkstra(int a) { order.clear(); priority_queue<pair<int,int>> q; vector<int> dist(n, (ll)1e18); vector<bool> vis(n, false); dist[a] = 0; q.push({0, a}); while(!q.empty()) { int u = q.top().second; q.pop(); if(vis[u]) continue; vis[u] = true; order.pb(u); for(auto x: adj[u]) { int w = x.second, v = x.first; if(dist[v] >= dist[u] + w) { dist[v] = dist[u] + w; q.push({-dist[v], v}); } } } return dist; } vector<ll> distA, distB, distS, distT; void solve() { cin >> n >> m >> s >> t >> a >> b; --s, --t, --a, --b; for(int i = 0;i < m; ++i) { int u, v, c; cin >> u >> v >> c; --u, --v; adj[u].pb({v, c}); adj[v].pb({u, c}); } distA = dijkstra(a), distB = dijkstra(b); distS = dijkstra(s), distT = dijkstra(t); ans = distA[b]; dijkstra(s); vector<int> dp(n, (ll)1e18); for(int node: order) { dp[node] = distA[node]; for(auto f : adj[node]) { int v = f.first, w = f.second; if(distS[v] + w == distS[node] && distS[v] + w + distT[node] == distS[t]) { dp[node] = min(dp[node], dp[v]); } ans = min(ans, dp[node] + distB[node]); } } dp.assign(n, (ll)1e18); for(int node: order) { dp[node] = distB[node]; for(auto f: adj[node]) { int v = f.first, w = f.second; if(distS[v] + w == distS[node] && distS[v] + w + distT[node] == distS[t]) { dp[node] = min(dp[node], dp[v]); } ans = min(ans, dp[node] + distA[node]); } } cout << ans << "\n"; } int32_t main() { ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); int t = 1; //cin >> t; while(t--) { solve(); } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...