Submission #749132

#TimeUsernameProblemLanguageResultExecution timeMemory
749132onebit1024Commuter Pass (JOI18_commuter_pass)C++17
100 / 100
371 ms35536 KiB
#include <bits/stdc++.h> using namespace std; #define int long long #define pb push_back #define all(c) c.begin(), c.end() #define endl "\n" const double PI=3.141592653589; void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifndef ONLINE_JUDGE #define dbg(x...) cerr << "LINE(" << __LINE__ << ") -> " <<"[" << #x << "] = ["; _print(x) #else #define dbg(x...) #endif int n,m; vector<vector<int>>nadj; vector<vector<pair<int,int>>>adj; int res; vector<int> dij(int s){ vector<int>dist(n+1,1e18); dist[s] = 0; priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq; pq.push({0,s}); while(!pq.empty()){ int d = pq.top().first, u = pq.top().second; pq.pop(); if(dist[u]!=d)continue; for(auto &[v,c] : adj[u]){ int cur_dist = dist[v], new_dist = dist[u]+c; if(new_dist < cur_dist){ dist[v] = new_dist; pq.push({new_dist, v}); } } } return dist; } vector<int>dists,distt,distu,distv; void solve() { cin >> n >> m; adj.resize(n+1); nadj.resize(n+1); int s,t,u,v; cin >> s >> t >> u >> v; for(int i = 1;i<=m;++i){ int u,v,c; cin >> u >> v >> c; adj[u].pb({v,c}); adj[v].pb({u,c}); } dists = dij(s); distt = dij(t); distu = dij(u); distv = dij(v); vector<int>deg(n+1),deg2(n+1); vector<vector<int>>rev(n+1); for(int i =1;i<=n;++i){ for(auto &[v,c] : adj[i]){ // check if edge i->v (directed) lies on shortest path from s->t if(dists[i]+c+distt[v] == dists[t]){ nadj[i].pb(v); rev[v].pb(i); deg2[i]++; deg[v]++; } } } res = distu[v]; queue<int>q; q.push(s); vector<int>vis(n+1),dp1(n+1),dp2(n+1); // dp1[i] min distance from v uptill node i for all nodes uptill i for(int i = 1;i<=n;++i){ dp1[i] = dp2[i] = distv[i]; } while(!q.empty()){ int u = q.front(); q.pop(); if(vis[u])continue; vis[u] = 1; for(int v : nadj[u]){ deg[v]--; dp1[v] = min(dp1[v],dp1[u]); if(deg[v]==0)q.push(v); } } q.push(t); vis = vector<int>(n+1); while(!q.empty()){ int u = q.front(); q.pop(); if(vis[u])continue; vis[u] = 1; for(int v : rev[u]){ deg2[v]--; dp2[v] = min(dp2[v],dp2[u]); if(deg2[v]==0)q.push(v); } } for(int i = 1;i<=n;++i){ if(vis[i])res = min(res, distu[i]+min(dp1[i],dp2[i])); } cout << res << endl; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); int T=1; for(int i = 1;i<=T;++i) { // cout << "Case #" << i << ": "; 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...