#include <bits/stdc++.h>
using namespace std;
#define name "commuter"
#define MAXN 200005
#define pb push_back
#define pf push_front
#define ll long long
#define ii pair<int, int>
#define fs first
#define sc second
#define ill pair<int, ll>
#define lli pair<ll, int>
#define llll pair<ll, ll>
#define all(v) v.begin(),v.end()
#define uni(v) v.erase(unique(all(v)),v.end())
#define bit(n,i) (((n)>>(i))&1)
#define FOR(i,a,b) for (int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for (int i=(a),_b=(b); i>=_b; i--)
#define MASK(i) (1LL<<(i))
const ll INF=1e18;
const int MOD=1e9+7;
void add(int &u, int v){
u+=v;
if (u>=MOD) u-=MOD;
}
void sub(int &u, int v){
u-=v;
if (u<0) u+=MOD;
}
void minimize(ll &u, ll v){
u=min(u,v);
}
void maximize(int &u, int v){
u=max(u,v);
}
long long Rand(long long l, long long r){
ll tmp=0;
FOR(i,1,4) tmp=((tmp<<15)^(((1<<15)-1)&rand()));
return l+tmp%(r-l+1);
}
int n,m;
vector<ii> adj[MAXN];
vector<int> child[MAXN],par[MAXN];
int S,T,U,V;
ll d[3][MAXN];
priority_queue<lli,vector<lli>,greater<lli>> pq;
void dijkstra(int type, int s){
FOR(i,1,n) d[type][i]=INF;
d[type][s]=0;
pq.push({0,s});
while (pq.size()){
ii tmp=pq.top();
pq.pop();
ll w=tmp.fs;
int u=tmp.sc;
if (w>d[type][u]) continue;
for (ii pairs:adj[u]){
int v=pairs.fs;
int wei=pairs.sc;
if (d[type][v]>=d[type][u]+1LL*wei){
if (type==0){
if (d[type][v]>d[type][u]+1LL*wei){
par[v].clear();
}
par[v].pb(u);
}
if (d[type][v]>d[type][u]+1LL*wei){
d[type][v]=d[type][u]+1LL*wei;
pq.push({d[type][v],v});
}
}
}
}
}
ll ans;
ll f[MAXN],g[MAXN];
void dfs(int type, int u){
if (type==1){
for (int v:child[u]){
f[v]=min(f[u],d[1][v]);
dfs(type,v);
}
}
else{
for (int v:par[u]){
g[v]=min(g[u],d[2][v]);
dfs(type,v);
}
}
}
bool visited[MAXN];
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
/*
srand(time(0));
n=Rand(100000,100000);
m=Rand(200000,200000);
S=Rand(1,n);
T=Rand(1,n);
U=Rand(1,n);
V=Rand(1,n);
cout<<n<<" "<<m<<"\n";
cout<<S<<" "<<T<<"\n";
cout<<U<<" "<<V<<"\n";
*/
cin>>n>>m;
cin>>S>>T;
cin>>U>>V;
FOR(i,1,m){
int x,y,w;
/*
x=Rand(1,n);
y=Rand(1,n);
w=Rand(1,(int)1e9);
cout<<x<<" "<<y<<" "<<w<<"\n";
*/
cin>>x>>y>>w;
adj[x].pb({y,w});
adj[y].pb({x,w});
}
dijkstra(0,S);
dijkstra(1,U);
dijkstra(2,V);
queue<int> Q;
Q.push(T);
visited[T]=1;
while (Q.size()){
int u=Q.front();
Q.pop();
for (int p:par[u]){
child[p].pb(u);
if (!visited[p]){
visited[p]=1;
Q.push(p);
}
}
}
ll ans=d[1][V];
memset(f,0x3f,sizeof(f));
memset(g,0x3f,sizeof(g));
f[S]=d[1][S];
g[T]=d[2][T];
dfs(1,S);
dfs(2,T);
FOR(i,1,n) minimize(ans,f[i]+g[i]);
///
memset(f,0x3f,sizeof(f));
memset(g,0x3f,sizeof(g));
swap(S,T);
FOR(i,1,n) swap(par[i],child[i]);
f[S]=d[1][S];
g[T]=d[2][T];
dfs(1,S);
dfs(2,T);
FOR(i,1,n) minimize(ans,f[i]+g[i]);
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... |