제출 #380266

#제출 시각아이디문제언어결과실행 시간메모리
380266penguinhackerCommuter Pass (JOI18_commuter_pass)C++14
100 / 100
479 ms19584 KiB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ar array

const int mxN = 100000;
int n, m, s1, t1, s2, t2, id[mxN];
vector<pair<int, int>> adj[mxN];
ll d1[mxN], d2[mxN], d3[mxN], d4[mxN], pre[mxN], suf[mxN];
bool vis[mxN];

void dijk(int s, ll d[]) {
	memset(vis, 0, n);
	memset(d, 0x3f, 8 * n);
	d[s] = 0;
	priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
	pq.emplace(0, s);
	while(!pq.empty()) {
		int u = pq.top().second; pq.pop();
		if (vis[u])
			continue;
		vis[u] = 1;
		for (pair<int, int> v : adj[u])
			if (d[u] + v.second < d[v.first])
				pq.emplace(d[v.first] = d[u] + v.second, v.first);
	}
}

void solve(ll dp[], ll d[], ll du[]) {
	sort(id, id + n, [&](int a, int b) {return d[a] < d[b];});
	for (int i = 0; i < n; ++i) {
		dp[id[i]] = du[id[i]];
		for (pair<int, int> j : adj[id[i]])
			if (d[j.first] + j.second == d[id[i]])
				dp[id[i]] = min(dp[id[i]], dp[j.first]);
	}
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> m >> s1 >> t1 >> s2 >> t2, --s1, --t1, --s2, --t2;
	for (int i = 0, u, v, w; i < m; ++i) {
		cin >> u >> v >> w, --u, --v;
		adj[u].emplace_back(v, w);
		adj[v].emplace_back(u, w);
	}
	dijk(s1, d1);
	dijk(t1, d2);
	dijk(s2, d3);
	dijk(t2, d4);
	iota(id, id + n, 0);
	ll spath = d1[t1];
	ll ans = d3[t2];
	for (int rep = 0; rep < 2; ++rep) {
		solve(pre, d1, d3);
		solve(suf, d2, d4);
		//for (int i = 0; i < n; ++i)
		//	cout << pre[i] << " " << suf[i] << "\n";
		for (int i = 0; i < n; ++i)
			if (d1[i] + d2[i] == spath)
				ans = min(ans, pre[i] + suf[i]);
		for (int i = 0; i < n; ++i)
			swap(d3[i], d4[i]);
	}
	cout << ans;
	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...