제출 #1354681

#제출 시각아이디문제언어결과실행 시간메모리
1354681temporary1Commuter Pass (JOI18_commuter_pass)C++17
100 / 100
190 ms19772 KiB
#include <bits/stdc++.h>
using namespace std;

#define f first
#define s second
#define ll long long
#define pii pair<int,int>
#define pli pair<ll,int>
#define pll pair<ll,ll>
#define tiii tuple<int,int,int>
#define tiiii tuple<int,int,int,int>
#define pb push_back
#define eb emplace_back
#define emp emplace
#define mkp make_pair
#define mkt make_tuple
#define vctr vector
#define arr array
#define all(x) x.begin(), x.end()
#define amin(a,b) a = min(a,b)
#define amax(a,b) a = max(a,b)
#define brick(x) cout << #x << " = " << (x) << " | "
#define dbg(x) cout << #x << " = " << (x) << '\n'
#define vdbg(a) cout << #a << " = "; for(auto _x : a)cout << _x << ' '; cout << '\n'
#define adbg(a,n) cout << #a << " = "; for(int _i = 1; _i <= n; ++_i)cout << a[_i] << ' '; cout << '\n'
#define adbg0(a,n) cout << #a << " = "; for(int _i = 0; _i < n; ++_i)cout << a[_i] << ' '; cout << '\n'
mt19937 rng(static_cast<uint32_t>(chrono::steady_clock::now().time_since_epoch().count()));
int uid(int a, int b) { return uniform_int_distribution<int>(a,b)(rng); }
ll uld(ll a, ll b) { return uniform_int_distribution<ll>(a,b)(rng); }

const int MOD = 1e9+7; // 998244353;

ll dist1[100005];
ll dist2[100005];
vctr<pii> adj[100005];
ll dist[4][100005];

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n, m;
	cin >> n >> m;
	int s1, t1, s2, t2;
	cin >> s1 >> t1 >> s2 >> t2;
	for (int i = 1; i <= m; ++i) {
		int u, v, w;
		cin >> u >> v >> w;
		adj[u].eb(v,w);
		adj[v].eb(u,w);
	}
	{
		priority_queue<pli,vctr<pli>,greater<pli>> pq;
		fill(dist1+1,dist1+n+1,2e18);
		pq.emp(dist1[s1] = 0,s1);
		while (pq.size()) {
			auto [d,x] = pq.top();
			pq.pop();
			if (d > dist1[x])continue;
			for (auto [it,w] : adj[x]) {
				if (dist1[it] > dist1[x]+w) {
					pq.emp(dist1[it] = dist1[x]+w,it);
				}
			}
		}
	}
	{
		priority_queue<pli,vctr<pli>,greater<pli>> pq;
		fill(dist2+1,dist2+n+1,2e18);
		pq.emp(dist2[t1] = 0,t1);
		while (pq.size()) {
			auto [d,x] = pq.top();
			pq.pop();
			if (d > dist2[x])continue;
			for (auto [it,w] : adj[x]) {
				if (dist2[it] > dist2[x]+w) {
					pq.emp(dist2[it] = dist2[x]+w,it);
				}
			}
		}
	}
	fill(dist[0]+1,dist[0]+n+1,2e18);
	fill(dist[1]+1,dist[1]+n+1,2e18);
	fill(dist[2]+1,dist[2]+n+1,2e18);
	fill(dist[3]+1,dist[3]+n+1,2e18);
	priority_queue<pair<ll,pii>,vctr<pair<ll,pii>>,greater<pair<ll,pii>>> pq;
	pq.emp(dist[0][s2] = 0,mkp(s2,0));
	while (pq.size()) {
		auto [d,p] = pq.top();
		pq.pop();
		auto [x,z] = p;
		if (d > dist[z][x])continue;
		// brick(x);brick(z);dbg(d);
		if (z == 0) {
			for (auto [it,w] : adj[x]) {
				if (dist[0][it] > dist[0][x]+w) {
					pq.emp(dist[0][it] = dist[0][x]+w,mkp(it,0));
				}
				if (dist1[x]+w+dist2[it] == dist1[t1]) {
					if (dist[1][it] > dist[0][x]) {
						pq.emp(dist[1][it] = dist[0][x],mkp(it,1));
					}
				}
				if (dist2[x]+w+dist1[it] == dist1[t1]) {
					if (dist[3][it] > dist[0][x]) {
						pq.emp(dist[3][it] = dist[0][x],mkp(it,3));
					}
				}
			}
		} else if (z == 1) {
			for (auto [it,w] : adj[x]) {
				if (dist[2][it] > dist[1][x]+w) {
					pq.emp(dist[2][it] = dist[1][x]+w,mkp(it,2));
				}
				if (dist1[x]+w+dist2[it] == dist1[t1]) {
					if (dist[1][it] > dist[1][x]) {
						pq.emp(dist[1][it] = dist[1][x],mkp(it,1));
					}
				}
			}
		} else if (z == 3) {
			for (auto [it,w] : adj[x]) {
				if (dist[2][it] > dist[3][x]+w) {
					pq.emp(dist[2][it] = dist[3][x]+w,mkp(it,2));
				}
				if (dist2[x]+w+dist1[it] == dist1[t1]) {
					if (dist[3][it] > dist[3][x]) {
						pq.emp(dist[3][it] = dist[3][x],mkp(it,3));
					}
				}
			}
		} else {
			for (auto [it,w] : adj[x]) {
				if (dist[2][it] > dist[2][x]+w) {
					pq.emp(dist[2][it] = dist[2][x]+w,mkp(it,2));
				}
			}
		}
	}
	cout << min({dist[0][t2],dist[1][t2],dist[2][t2],dist[3][t2]}) << '\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...