Submission #459189

#TimeUsernameProblemLanguageResultExecution timeMemory
459189TeaTimeCommuter Pass (JOI18_commuter_pass)C++17
0 / 100
1064 ms36936 KiB
//#pragma GCC optimize("O3")
//#pragma GCC target("avx2")
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <unordered_map>

using namespace std;

#define fastInp cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);

typedef long long ll;
typedef long double ld;

const ll SZ = 1e5 + 100, INF = 1e9 * 1e9 + 100;

vector<vector<pair<ll, ll>>> gr;
ll n, m, s, t, u, v;
ll dist[SZ][2], d[SZ][3];
ll used[SZ][2];

void djikstra(int v, int ind) {
	priority_queue<pair<ll, ll>> st;

	for (int i = 0; i < n; i++) {
		dist[i][ind] = INF;
	}

	dist[v][ind] = 0;

	for (int i = 0; i < n; i++) st.push({ dist[i][ind], i });

	while (!st.empty()) {
		pair<ll, ll> v = (st.top());
		st.pop();
		if (used[v.second][ind]) continue;
		used[v.second][ind] = 1;

		for (auto to : gr[v.second]) {
			if (dist[to.first][ind] > v.first + to.second) {
				dist[to.first][ind] = v.first + to.second;
				st.push({ dist[to.first][ind], to.first });
			}
		}
	}
}

ll find_ans(int u, int v) {
	set<tuple<ll, ll, ll>> st;

	for (int i = 0; i < n; i++) {
		for (int k = 0; k < 3; k++) d[i][k] = INF;
	}

	d[v][0] = 0;

	for (int i = 0; i < n; i++) {
		for (int k = 0; k < 3; k++) st.insert({ d[i][k], i, k });
	}

	while (!st.empty()) {
		tuple<ll, ll, ll> v = (*st.begin());
		st.erase(st.begin());
		ll vrt = get<1>(v), k = get<2>(v), dst = get<0>(v);

		for (auto to : gr[vrt]) {
			
				if (k == 1) {
					if (d[to.first][k + 1] > dst + to.second) {
						st.erase({ d[to.first][k + 1], to.first, k + 1 });
						d[to.first][k + 1] = dst + to.second;
						st.insert({ d[to.first][k + 1], to.first, k + 1 });
					}
				} else {
					if (d[to.first][k] > dst + to.second) {
						st.erase({ d[to.first][k], to.first, k });
						d[to.first][k] = dst + to.second;
						st.insert({ d[to.first][k], to.first, k });
					}
				}
			
			int s2 = vrt, t2 = to.first;
			if (dist[s2][0] + dist[t2][1] + to.second == dist[t][0]) {
				if (k == 0 || k == 1) {
					if (d[to.first][1] > dst) {
						st.erase({ d[to.first][1], to.first, 1 });
						d[to.first][1] = dst;
						st.insert({ d[to.first][1], to.first, 1 });
					}
				}
			}
		}
	}

	return min(min(d[u][0], d[u][1]), d[u][2]);
}

int main() {
	fastInp;

	cin >> n >> m;

	cin >> s >> t;

	cin >> u >> v;

	s--; t--; u--; v--;

	gr.resize(n);

	for (int i = 0; i < m; i++) {
		ll u, v, c;
		cin >> u >> v >> c;
		u--; v--;
		gr[u].push_back({ v, c });
		gr[v].push_back({ u, c });
	}
	
	djikstra(s, 0);
	djikstra(t, 1);

	ll a = find_ans(u, v);
	ll b = find_ans(v, u);

	cout << min(a, b);

	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...