#include <bits//stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long, null_type, less<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_multiset;
#define ll long long
#define ld long double
#define iloop(m, h) for (auto i = m; i != h; i += (m < h ? 1 : -1))
#define jloop(m, h) for (auto j = m; j != h; j += (m < h ? 1 : -1))
#define kloop(m, h) for (auto k = m; k != h; k += (m < h ? 1 : -1))
#define lloop(m, h) for (auto l = m; l != h; l += (m < h ? 1 : -1))
#define iloop_(m, h, g) for (auto i = m; i < h; i += g)
#define jloop_(m, h, g) for (auto j = m; j < h; j += g)
#define kloop_(m, h, g) for (auto k = m; k < h; k += g)
#define lloop_(m, h, g) for (auto l = m; l < h; l += g)
#define getchar_unlocked _getchar_nolock // comment before submission
#define pll pair<ll, ll>
#define plll pair<ll, pll>
#define pllll pair<pll, pll>
#define vll vector<ll>
#define qll queue<ll>
#define dll deque<ll>
#define pqll priority_queue<ll>
#define gll greater<ll>
#define INF 1000000000000000
#define MOD1 1000000007
#define MOD2 998244353
#define MOD3 1000000009
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
ll n, m, x[5];
pll a[100005];
vector<pll> adj[100005];
ll dst[100005][4];
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n >> m;
	iloop(0, 4) {cin >> x[i]; x[i]--;}
	ll t1, t2, t3;
	iloop(0, m) {
		cin >> t1 >> t2 >> t3;
		t1--, t2--;
		adj[t1].push_back({t3, t2});
		adj[t2].push_back({t3, t1});
	}
	priority_queue<pll, vector<pll>, greater<pll>> pq;
	lloop(0, 4) {
		iloop(0, n) dst[i][l] = INF;
		dst[x[l]][l] = 0;
		pq.push({0, x[l]});
		while (pq.size()) {
			auto nd = pq.top();
			pq.pop();
			if (nd.first != dst[nd.second][l]) continue;
			for (auto it : adj[nd.second]) if (it.first + nd.first < dst[it.second][l]) {
				dst[it.second][l] = it.first + nd.first;
				pq.push({dst[it.second][l], it.second});
			}
		}
	}
	ll ans = INF;
	iloop(0, n) a[i] = {dst[i][2], i};
	sort(a, a+n);
	bool vis[n];
	memset(vis, 0, sizeof(vis));
	queue<pll> q;
	iloop(0, n) {
		ll nd = a[i].second;
		if (vis[nd] || dst[nd][0] + dst[nd][1] > dst[x[0]][1]) continue;
		q.push({0, nd});
		while (q.size()) {
			auto tmp = q.front();
			q.pop();
			ans = min(ans, a[i].first + dst[tmp.second][3]);
			for (auto it : adj[tmp.second]) if (!vis[it.second] && dst[it.second][0] + dst[it.second][1] == dst[x[0]][1]) {
				if (dst[it.second][0] > dst[tmp.second][0] && tmp.first == -1) continue;
				if (dst[it.second][0] > dst[tmp.second][0]) q.push({1, it.second});
				if (dst[it.second][0] < dst[tmp.second][0] && tmp.first == 1) continue;
				if (dst[it.second][0] < dst[tmp.second][0]) q.push({-1, it.second});
				vis[it.second] = 1;
			}
		}
	}
	cout << min(ans, dst[x[2]][3]);
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |