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;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x,y) (lower_bound(all(x),y)-x.begin())
#define ubd(x,y) (upper_bound(all(x),y)-x.begin())
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) { return (rng() % (y+1-x)) + x; } //inclusivesss
string inline to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){ return a==0?llabs(b):gcd(b%a,a); }
typedef long long ll;
typedef long double ld;
#define FOR(i,s,e) for(ll i=s;i<=ll(e);++i)
#define DEC(i,s,e) for(ll i=s;i>=ll(e);--i)
typedef pair<ll,ll>pi; typedef pair<ll,pi>spi; typedef pair<pi,pi>dpi;
#define LLINF ((long long)1e18)
#define INF int(1e9+1e6)
#define MAXN (100006)
ll n,m,U,V,S,T,dist[MAXN],dist2[MAXN],best[2][MAXN],mi[2][MAXN],ans=LLINF;
vector<pi>v[MAXN];
int main(){
cin>>n>>m>>S>>T>>U>>V;
FOR(i,0,m-1){
ll a,b,c;cin>>a>>b>>c;
v[a].eb(b,c),v[b].eb(a,c);
}
priority_queue<pi,vector<pi>,greater<pi>> pq;
mmst(dist,-1);
dist[T]=0, pq.emplace(0,T);
while(pq.size()){
ll x=pq.top().s,d=pq.top().f; pq.pop();
if(dist[x]^d)continue;
for(auto i:v[x])if(dist[i.f]==-1||dist[i.f]>d+i.s){
dist[i.f]=d+i.s;
pq.emplace(dist[i.f],i.f);
}
}
FOR(i,1,n)assert(~dist[i]);//dist[i] will nvr be -1
// have to set ans to be shortest dist between U and V
for(auto sp:{U,V}){
assert(pq.empty());
ll alt = sp == U ? 0 : 1;
mmst(best[alt],-1);
best[alt][sp]=0, pq.emplace(0,sp);
while(pq.size()){
ll x=pq.top().s,d=pq.top().f;
pq.pop();
if(best[alt][x]^d)continue;
for(auto i:v[x])if(best[alt][i.f]==-1||best[alt][i.f]>d+i.s){
best[alt][i.f]=d+i.s;
pq.emplace(best[alt][i.f],i.f);
}
}
ans=best[alt][sp==U?V:U];
}
assert(pq.empty());
mmst(dist2,-1);
dist2[S]=0, mi[0][S]=best[0][S], mi[1][S]=best[1][S], pq.emplace(0,S);
while(pq.size()){
ll x=pq.top().s,d=pq.top().f;
pq.pop();
if(dist2[x]^d)continue;
for(auto i:v[x])if(dist2[i.f]==-1||dist2[i.f]>=d+i.s){
if(dist[i.f] + d + i.s == dist[S]) {
dist2[i.f]=d+i.s;
pq.emplace(dist2[i.f],i.f);
ans=min(ans,mi[0][x]+best[1][i.f]);
ans=min(ans,mi[1][x]+best[0][i.f]);
mi[0][i.f]=min(mi[0][x], best[0][i.f]);
mi[1][i.f]=min(mi[1][x], best[1][i.f]);
}
}
}
cout<<ans<<'\n';
}
# | 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... |