Submission #1032282

#TimeUsernameProblemLanguageResultExecution timeMemory
1032282belgianbotCommuter Pass (JOI18_commuter_pass)C++14
0 / 100
2103 ms19024 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> distU, distV;
vector<ll> memo;

ll dp (int pos) {
	if (pos == T) return memo[pos] = 0;
	
	if (memo[pos] != -1) return memo[pos];
	processed[pos] = true;
	
	ll ans = MAX + 1;
	for (auto x : adj[pos]) {
		if (!processed[x.fi]) {
			ans = min(ans, dp(x.fi) + x.se);
		}
	}
	
	processed[pos] = false;
	if (ans <= MAX) return memo[pos] = ans;
	else return ans;
}

pair<ll, ll> res(int pos, ll cost) {
	if (pos == T) {
		answer = min(answer, distU[T] + distV[T]);
		return {distU[T], distV[T]};
	}
		
	processed[pos] = true;
	pair<ll, ll> best = {MAX, MAX};
	for (auto x : adj[pos]) {
		if (!processed[x.fi]) {
			if (memo[x.fi] + x.se + cost == memo[S]) {
				pair<ll, ll> func = res(x.fi, cost + x.se);
				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 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); 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});
	}
		dp(S);
		return 0;
	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,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, 0);
	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...