Submission #1010117

#TimeUsernameProblemLanguageResultExecution timeMemory
1010117SG2AlokCommuter Pass (JOI18_commuter_pass)C++17
100 / 100
263 ms29276 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define endl '\n' #define hitaf ios_base::sync_with_stdio(false); cin.tie(NULL); #define fi first #define se second const ll MOD = 998244353; const ll INF = 4500000000000000000LL; // dp[i][0] = par -> child, dp[i][1] = child -> par int n, m, s, t, u, V; ll dp[100005][2]; vector<pair<int, int>> adj[100005]; vector<int> p[100005], child[100005]; ll dist[100005], distU[100005], distV[100005]; bool vis[100005]; ll dfs1(ll u){ if(dp[u][0] != -1) return dp[u][0]; dp[u][0] = distV[u]; for(auto v: p[u]){ dp[u][0] = min(dp[u][0], dfs1(v)); } return dp[u][0]; } ll dfs2(ll u){ if(dp[u][1] != -1) return dp[u][1]; dp[u][1] = distV[u]; for(auto v: child[u]){ dp[u][1] = min(dp[u][1], dfs2(v)); } return dp[u][1]; } int main(){ hitaf cin >> n >> m; cin >> s >> t; cin >> u >> V; for(int i = 1; i <= m; i++){ int x, y, w; cin >> x >> y >> w; adj[x].push_back({y, w}); adj[y].push_back({x, w}); } for(int i = 1; i <= n; i++){ dist[i] = INF; distU[i] = INF; distV[i] = INF; } vector<bool> vis1(n + 2), vis2(n + 2), vis3(n + 2); priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq; pq.push({0, s}); dist[s] = 0; while(!pq.empty()){ auto now = pq.top(); pq.pop(); if(dist[now.se] < now.fi || vis1[now.se]) continue; vis1[now.se] = true; for(auto v: adj[now.se]){ if(dist[v.fi] < now.fi + v.se) continue; dist[v.fi] = now.fi + v.se; pq.push({dist[v.fi], v.fi}); } } priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq2; pq2.push({0, u}); distU[u] = 0; while(!pq2.empty()){ auto now = pq2.top(); pq2.pop(); if(distU[now.se] < now.fi || vis2[now.se]) continue; vis2[now.se] = true; for(auto v: adj[now.se]){ if(distU[v.fi] < now.fi + v.se) continue; distU[v.fi] = now.fi + v.se; pq2.push({distU[v.fi], v.fi}); } } priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq3; pq3.push({0, V}); distV[V] = 0; while(!pq3.empty()){ auto now = pq3.top(); pq3.pop(); if(distV[now.se] < now.fi || vis3[now.se]) continue; vis3[now.se] = true; for(auto v: adj[now.se]){ if(distV[v.fi] < now.fi + v.se) continue; distV[v.fi] = now.fi + v.se; pq3.push({distV[v.fi], v.fi}); } } queue<int> q; q.push(t); while(!q.empty()){ ll now = q.front(); q.pop(); for(auto v: adj[now]){ if(dist[v.fi] + v.se == dist[now]){ p[now].push_back(v.fi); child[v.fi].push_back(now); if(vis[v.fi]) continue; vis[v.fi] = true; q.push(v.fi); } } } for(int i = 1; i <= n; i++){ dp[i][1] = -1; dp[i][0] = -1; } ll ans = distU[V]; for(int i = 1; i <= n; i++){ if(!p[i].empty() || !child[i].empty()){ //return 0; ans = min(ans, distU[i] + min(dfs1(i), dfs2(i))); } } //for(auto x: child[7]) cout << x << " "; //cout << endl; cout << ans << endl; return 0; } /* 7 1 2 9 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...