This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int n,m;cin>>n>>m;
	int S,T;cin>>S>>T;
	--S;--T;
	int U,V;cin>>U>>V;
	--U;--V;
	vector<vector<pair<int,long long>>>adj(n);
	for (int i = 0;i<m;++i){
		int x,y,z;cin>>x>>y>>z;
		--x;--y;
		adj[x].push_back({y,z});
		adj[y].push_back({x,z});
	}
	const long long mxn = 1e15;
	queue<pair<int,int>>q;
	vector<vector<long long>>dist(n,vector<long long>(2,mxn));
	q.push({U,0});
	q.push({V,1});
	dist[U][0] = 0;
	dist[V][1] = 0;
	while(!q.empty()){
		auto u = q.front();
		q.pop();
		for (auto x:adj[u.first]){
			if (dist[x.first][u.second] > dist[u.first][u.second] + x.second){
				dist[x.first][u.second] = dist[u.first][u.second] + x.second;
				q.push({x.first,u.second});
			}
		}
	}
	vector<int>parent(n,-1);
	long long ans = dist[U][1];
	vector<long long>dis(n,mxn);
	auto bfs = [&](){
		queue<int>qq;
		qq.push(S);
		dis[S] = 0;
		while(!qq.empty()){
			auto u = qq.front();
			qq.pop();
			for (auto x:adj[u]){
				if (dis[x.first] > dis[u] + x.second){
					dis[x.first] = dis[u] + x.second;
					qq.push(x.first);
				}
			}
		}
		return dis[T];
	};
	long long minny = bfs();
	function<void(int,long long)>dfs = [&](int u,long long dd){
		if (u == T){
			if (dd > minny)return;
			vector<long long>d(2,mxn);
			while(u!=-1){
				d[0] = min(d[0],dist[u][0]);
				d[1] = min(d[1],dist[u][1]);
				//cout<<u<<" "<<dist[u][0]<<" "<<dist[u][1]<<'\n';
				u = parent[u];
			}
			ans = min(ans,d[0] + d[1]);
			return;
		}
		for (auto x:adj[u]){
			if (parent[x.first]==-1 && dd + x.second <=dis[x.first]){
				parent[x.first] = u;
				dfs(x.first,dd + x.second);
				parent[x.first] = -1;
			}
		}
	};
	dfs(S,0);
	cout<<ans<<'\n';
	return 0;
}
| # | 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... |