제출 #1032507

#제출 시각아이디문제언어결과실행 시간메모리
1032507belgianbotCommuter Pass (JOI18_commuter_pass)C++14
0 / 100
183 ms23752 KiB
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
 
using namespace std;
 
ll MAX = 1e14 * 2;
int N, M, S, T, U, V;
ll answer;
vector<vector<pair<int,ll>>> adj;
vector<vector<int>> trajects;
vector<bool> processed;
vector<ll> distS, distT, distU, distV;
vector<pair<ll,ll>> memo;
 
pair<ll, ll> res(int pos) {
	if (pos == T) {
		answer = min(answer, distU[T] + distV[T]);
		return {distU[T], distV[T]};
	}
	if (memo[pos] != make_pair((ll)(-1), (ll)(-1))) return memo[pos];
		
	processed[pos] = true;
	pair<ll, ll> best = {MAX, MAX};
	for (auto x : adj[pos]) {
		if (!processed[x.fi]) {
			if (distT[x.fi] + distS[x.fi] == distS[T]) {
				pair<ll, ll> func = res(x.fi);
				if (distU[pos] < func.fi) { func.fi = distU[pos]; answer = min(answer, func.fi + func.se); }
				if (distV[pos] < func.se) { func.se = distV[pos]; answer = min(answer, func.fi + func.se); }
				best.fi = min(best.fi, func.fi);
				best.se = min(best.se, func.se);
				
			}
		}
	}
	processed[pos] = false;
	return memo[pos] = best;
	
}
				
		
			
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> N >> M >> S >> T >> U >> V;
	S--, T--, U--, V--;
	
	adj.resize(N); memo.resize(N, {-1, -1}); processed.resize(N, false);
	for (int i = 0; i < M; i++) {
		int a, b; ll c; cin >> a >> b >> c;
		a--, b--;
		adj[a].push_back({b,c});
		adj[b].push_back({a,c});
	}
	distS.resize(N, MAX); distT.resize(N, MAX); distU.resize(N, MAX); distV.resize(N, MAX);
	priority_queue < pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll,int>>> q;
	
	q.push({0, S}); distS[S] = 0;
	while (!q.empty()) {
		int x = q.top().se; q.pop();
		if (processed[x]) continue;
		
		processed[x] = true;
		for (auto w : adj[x]) {
			if (distS[w.fi] > distS[x] + w.se) {
				distS[w.fi] = distS[x] + w.se;
				q.push({distU[w.fi], w.fi});
			}
		}
	}
	processed.clear(); processed.resize(N, false);
	q.push({0,T}); distT[T] = 0;
	while (!q.empty()) {
		int x = q.top().se; q.pop();
		if (processed[x]) continue;
		
		processed[x] = true;
		for (auto w : adj[x]) {
			if (distT[w.fi] > distT[x] + w.se) {
				distT[w.fi] = distT[x] + w.se;
				q.push({distU[w.fi], w.fi});
			}
		}
	}
	processed.clear(); processed.resize(N,false);
	q.push({0,U}); distU[U] = 0;
	
	while (!q.empty()) {
		int x = q.top().se; q.pop();
		if (processed[x]) continue;
		
		processed[x] = true;
		for (auto w : adj[x]) {
			if (distU[w.fi] > distU[x] + w.se) {
				distU[w.fi] = distU[x] + w.se;
				q.push({distU[w.fi], w.fi});
			}
		}
	}
	
	processed.clear(); processed.resize(N, false);
	q.push({0, V}); distV[V] = 0;
	
	while (!q.empty()) {
		int x = q.top().se; q.pop();
		if (processed[x]) continue;
		
		processed[x] = true;
		for (auto w : adj[x]) {
			if (distV[w.fi] > distV[x] + w.se) {
				distV[w.fi] = distV[x] + w.se;
				q.push({distV[w.fi], w.fi});
			}
		}
	}
	processed.clear(); processed.resize(N, false);
	answer = distU[V];
 
	res(S);
	cout << min(answer, distU[S] + distV[S]) << '\n';
		
	return 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...