제출 #598725

#제출 시각아이디문제언어결과실행 시간메모리
598725AsymmetryCommuter Pass (JOI18_commuter_pass)C++17
100 / 100
264 ms21496 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);

	vector<LL> odl(n, INF);
	vector<int> odw(n, 0);
	vector<array<LL, 3>> dp(n, array<LL, 3> {INF, INF, 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 &i : v[x]) {
			if (odl[i.first] > odl[x] + i.second) {
				odl[i.first] = odl[x] + i.second;
				pq.push({-odl[i.first], i.first});
				dp[i.first] = dp[x];
			}
			if (odl[i.first] == odl[x] + i.second) {
				REP (j, 3) {
					dp[i.first][j] = min(dp[i.first][j], dp[x][j]);
				}
			}
		}
	}
	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...