Submission #227813

#TimeUsernameProblemLanguageResultExecution timeMemory
227813jiahngCommuter Pass (JOI18_commuter_pass)C++14
0 / 100
2090 ms28060 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll,ll> pi;
typedef vector <ll> vi;
typedef vector <pi> vpi;
#define f first
#define s second
#define FOR(i,s,e) for(ll i=s;i<=ll(e);++i)
#define DEC(i,s,e) for(ll i=s;i>=ll(e);--i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i,x) for (auto i: x)
#define mem(x,i) memset(x,i,sizeof x)
#define fast ios_base::sync_with_stdio(false),cin.tie(0)
#define maxn 200001
#define INF 1e10
#define int ll
typedef pair <pi,int> pii;

int N,M,S,T,U,V;
int distS[maxn],distV[maxn], distU[maxn],distT[maxn];

vpi adj[maxn];
vi dag[maxn];
int dp[maxn];
int minE[maxn];
int minS[maxn];
int dpf(int x){
	if (dp[x] != -1) return dp[x];
	
	dp[x] = INF;
	int E = INF;
	int mnS = INF;
	aFOR(i,dag[x]){
		E = min(E,minE[x]);
		mnS = min(mnS,minS[x]);
		dp[x] = min(dp[x],dpf(i));
	}
	
	dp[x] = min(dp[x],distU[x] + E);
	dp[x] = min(dp[x],distV[x] + mnS);
	dp[x] = min(dp[x],distU[x] + distV[x]);
	return dp[x];
}

int dfs(int x){ //to find minE
	minE[x] = distV[x];
	minS[x] = distU[x];
	aFOR(i,dag[x]){
		dfs(i);
		minE[x] = min(minE[x],minE[i]);
		minS[x] = min(minS[x],minS[i]);
	}
	return minE[x];
}
int32_t main(){
	fast;
	
	cin>>N>>M>>S>>T>>U>>V;
	vector <pii> edges;
	
	FOR(i,0,M-1){
		int a,b,c; cin>>a>>b>>c;
		adj[a].pb(pi(b,c)); adj[b].pb(pi(a,c));
		edges.pb(pii(pi(a,b),c));
	}
	
	
	int rt = S;
		
	priority_queue <pi> pq;
	mem(distS,-1);
	distS[rt] = 0;
	pq.push(pi(0,rt));
	
	while (!pq.empty()){
		pi cur = pq.top(); pq.pop();
		
		if (distS[cur.s] != cur.f) continue;
		aFOR(i,adj[cur.s]){
			if (distS[i.f] == -1 || distS[i.f] > cur.f + i.s){
				distS[i.f] = cur.f + i.s;
				pq.push(pi(distS[i.f],i.f));
			}
		}
	}
	
	rt = T;
		
	mem(distT,-1);
	distT[rt] = 0;
	pq.push(pi(0,rt));
	
	while (!pq.empty()){
		pi cur = pq.top(); pq.pop();
		
		if (distT[cur.s] != cur.f) continue;
		aFOR(i,adj[cur.s]){
			if (distT[i.f] == -1 || distT[i.f] > cur.f + i.s){
				distT[i.f] = cur.f + i.s;
				pq.push(pi(distT[i.f],i.f));
			}
		}
	}
	
	rt = U;
		
	mem(distU,-1);
	distU[rt] = 0;
	pq.push(pi(0,rt));
	
	while (!pq.empty()){
		pi cur = pq.top(); pq.pop();
		
		if (distU[cur.s] != cur.f) continue;
		aFOR(i,adj[cur.s]){
			if (distU[i.f] == -1 || distU[i.f] > cur.f + i.s){
				distU[i.f] = cur.f + i.s;
				pq.push(pi(distU[i.f],i.f));
			}
		}
	}
	
	rt = V;
		
	mem(distV,-1);
	distV[rt] = 0;
	pq.push(pi(0,rt));
	
	while (!pq.empty()){
		pi cur = pq.top(); pq.pop();
		
		if (distV[cur.s] != cur.f) continue;
		aFOR(i,adj[cur.s]){
			if (distV[i.f] == -1 || distV[i.f] > cur.f + i.s){
				distV[i.f] = cur.f + i.s;
				pq.push(pi(distV[i.f],i.f));
			}
		}
	}
	return 0;
	aFOR(i,edges){
		int a = i.f.f, b = i.f.s, c = i.s;
		
		if (distS[a] + c + distT[b] == distS[T]){
			dag[a].pb(b);
			//cout<<a<<' '<<b<<'\n';
		}else if(distS[b] + c + distT[a] == distS[T]){
			dag[b].pb(a);
			//cout<<b<<' '<<a<<'\n';
		}
	}
	
	dfs(S);
	mem(dp,-1);
	int ans = min(distU[V],dpf(S));
	
	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...