/*
* @Author: RMQuan
* @Date: 2025-08-05 07:51:50
* @Last Modified by: RMQuan
* @Last Modified time: 2025-08-05 10:27:02
*/
/*idea :
*/
#include <bits/stdc++.h>
#define int long long
#define ll long long
#define INTMAX INT_MAX
#define INTMIN INT_MIN
#define LONGMAX LLONG_MAX
#define LONGMIN LLONG_MIN
#define fi first
#define se second
#define memfull(a,b) memset(a,b,sizeof(a));
#define endl '\n'
#define file(a) freopen(a".inp","r",stdin); freopen(a".out","w",stdout);
using namespace std;
const int MOD=1e9+7;
const int maxn=1e5+7;
const int inf=1e18;
vector<pair<int,int> > adj[maxn];
int dis[maxn][4],n,m;
bool vst[maxn];
void dij(int s,int id)
{
for (int i=1;i<=n;i++)
{
dis[i][id]=inf;
vst[i]=false;
}
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > q;
q.push({0,s});
dis[s][id]=0;
while (q.size())
{
int u=q.top().se;
q.pop();
if (vst[u])continue;
vst[u]=true;
for (auto v:adj[u])
{
if (dis[v.fi][id]>dis[u][id]+v.se)
{
dis[v.fi][id]=dis[u][id]+v.se;
q.push({dis[v.fi][id],v.fi});
}
}
}
}
int S,T,U,V;
bool cmp(int x,int y)
{
return dis[x][0]<dis[y][0];
}
int dp[maxn][2];
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m>>S>>T>>U>>V;
for (int i=1;i<=m;i++)
{
int u,v,w;
cin>>u>>v>>w;
adj[u].push_back({v,w});
adj[v].push_back({u,w});
}
dij(S,0);
dij(T,1);
dij(U,2);
dij(V,3);
vector<int> topo;
for (int i=1;i<=n;i++)topo.push_back(i);
sort(topo.begin(),topo.end(),cmp);
for (int i=1;i<=n;i++)dp[i][0]=dp[i][1]=inf;
int ans=dis[V][2];
for (auto i:topo)
{
if (dis[i][0]+dis[i][1]==dis[T][0])
{
dp[i][0]=dis[i][2];
dp[i][1]=dis[i][3];
for (auto v:adj[i])
{
if (dis[v.fi][0]+v.se+dis[i][1]==dis[T][0])
{
dp[i][0]=min(dp[i][0],dp[v.fi][0]);
dp[i][1]=min(dp[i][1],dp[v.fi][1]);
}
}
ans=min({ans,dp[i][0]+dis[i][3],dp[i][1]+dis[i][2]});
}
}
cout<<ans;
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... |