Submission #598248

#TimeUsernameProblemLanguageResultExecution timeMemory
598248AsymmetryCommuter Pass (JOI18_commuter_pass)C++17
0 / 100
314 ms23676 KiB
#include<bits/stdc++.h>
using namespace std;
using LL=long long;
#define FOR(i,l,r) for(int i=(l);i<=(r);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) int(x.size())
template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';}
template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';}
#ifdef DEBUG
#define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<'\n';}(x)
#else
#define debug(...) {}
#endif

int main() {
	cin.tie(0)->sync_with_stdio(0);

	const LL INF = 1e18;

	int n, m, S, T, U, V;
	cin >> n >> m >> S >> T >> U >> V;
	--S;
	--T;
	--U;
	--V;
	vector<vector<pair<int, int>>> v(n);
	REP (i, m) {
		int a, b, c;
		cin >> a >> b >> c;
		--a;
		--b;
		v[a].emplace_back(b, c);
		v[b].emplace_back(a, c);
	}

	auto wylicz = [&](int st, vector<LL> &odl) {
		REP (i, n) {
			odl[i] = INF;
		}
		odl[st] = 0;
		priority_queue<pair<LL, int>> pq;
		pq.push({0, st});
		while (!pq.empty()) {
			LL d = -pq.top().first;
			int x = pq.top().second;
			pq.pop();
			if (d != odl[x]) {
				continue;
			}
			for (auto &[a, b] : v[x]) {
				if (odl[a] > odl[x] + b) {
					odl[a] = odl[x] + b;
					pq.push({-odl[a], a});
				}
			}
		}
		return odl;
	};

	vector<LL> odlU(n), odlV(n);
	wylicz(U, odlU);
	wylicz(V, odlV);

	debug(odlU);
	debug(odlV);

	vector<LL> odl(n, INF);
	vector<int> odw(n, 0);
	vector<vector<LL>> dp(n, vector<LL> (3, INF));
	priority_queue<pair<LL, int>> pq;
	pq.push({0, S});
	odl[S] = 0;
	while (!pq.empty()) {
		int x = pq.top().second;
		pq.pop();
		if (odw[x]) {
			continue;
		}
		odw[x] = 1;
		dp[x][2] = min({dp[x][0] + odlV[x], dp[x][1] + odlU[x], odlU[x] + odlV[x], dp[x][2]});
		dp[x][0] = min(dp[x][0], odlU[x]);
		dp[x][1] = min(dp[x][1], odlV[x]);
		for (auto &[a, b] : v[x]) {
			if (odl[a] >= odl[x] + b) {
				odl[a] = odl[x] + b;
				REP (i, 3) {
					dp[a][i] = min(dp[a][i], dp[x][i]);
				}
				pq.push({-odl[a], a});
			}
		}
	}
	debug(dp);
	cout << min(dp[T][2], odlU[V]) << endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...