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 fi first
#define se second
#define ll long long
#define dbg(v) cerr<<#v<<" = "<<v<<'\n'
#define vi vector<int>
#define vl vector <ll>
#define pii pair<int,int>
#define mp make_pair
#define db long double
#define pb push_back
#define all(s) s.begin(),s.end()
template < class T > T smin(T &a,T b) {if (a > b) a = b;return a;}
template < class T > T smax(T &a,T b) {if (a < b) a = b;return a;}
const int N = (int)(1e6) + 5;
ll D1[N];
ll D2[N];
ll D3[N];
ll D4[N];
vector < pii > g[N];
int n;
void Go(ll D[],int S) {
for (int i = 1;i <= n;++i)
D[i] = 1e18;
D[S] = 0;
priority_queue < pair < ll , int > , vector < pair < ll , int > > , greater < pair < ll , int > > > Q;
Q.push(mp(0,S));
while (!Q.empty()) {
const ll cost = Q.top().fi;
const int node = Q.top().se;
Q.pop();
if (cost != D[node])
continue;
for (auto it : g[node])
if (D[it.fi] > D[node] + it.se)
D[it.fi] = D[node] + it.se,Q.push(mp(D[it.fi],it.fi));
}
}
int was[N];
ll d1[N];
ll d2[N];
int main(void)
{
int m;
int S,T,U,V;
cin>>n>>m>>S>>T>>U>>V;
while (m --) {
int a,b,c;
cin>>a>>b>>c;
g[a].pb(mp(b,c));
g[b].pb(mp(a,c));
}
Go(D1,S);
Go(D2,T);
Go(D3,U);
Go(D4,V);
vi points;
const ll dist = D1[T];
for (int i = 1;i <= n;++i)
if (D1[i] + D2[i] == dist)
points.pb(i);
ll ans = D3[V];
sort(all(points),[&](int x,int y) {
return D1[x] < D1[y];
});
for (auto it : points) {
d1[it] = D3[it];
d2[it] = D4[it];
for (auto edge : g[it]) {
if (D1[edge.fi] + edge.se + D2[it] == dist)
smin(d1[it],d1[edge.fi]),smin(d2[it],d2[edge.fi]);
}
smin(ans,d1[it] + D4[it]);
smin(ans,d2[it] + D3[it]);
}
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... |