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;
using ll = long long;
#define fi first
#define si second
typedef pair<ll,int> pi;
typedef tuple<int,int,int> ti;
template<typename T> bool chmin(T &a, T b){return (b < a) ? a = b, 1 : 0;}
template<typename T> bool chmax(T &a, T b){return (b > a) ? a = b, 1 : 0;}
void debug_out() {cerr<<endl;}
template <typename Head, typename... Tail>
void debug_out(Head _H, Tail... _T) {cerr<<" "<<to_string(_H);debug_out(_T...);}
#define debug(...) cerr<<"["<<#__VA_ARGS__<<"]:",debug_out(__VA_ARGS__)
const int N = 100010;
const ll oo = 1e18;
int n,m,S,T,U,V;
vector<pi> g[N];
vector<int> chi[N],par[N];
int vis[N];
vector<ll> ds,dt,du,dv,dp1,dp2;
void sp(int src, vector<ll>& dist) {
priority_queue<pi,vector<pi>,greater<pi>>q;
dist.resize(n+1,-1);
dist[src]=0;
q.push({0,src});
for (int i = 1; i <= n; ++i) par[i].clear();
while (q.size()) {
auto [d,x] = q.top(); q.pop();
if (dist[x]!=d) continue;
for (auto i: g[x]) {
ll nd = i.si+d;
if (dist[i.fi]==-1||dist[i.fi]>nd) {
dist[i.fi]=nd;
chi[x].clear(); chi[x].push_back(i.fi);
par[i.fi].clear(); par[i.fi].push_back(x);
q.push({nd,i.fi});
} else if (dist[i.fi]==nd) {
chi[x].push_back(i.fi);
par[i.fi].push_back(x);
}
}
}
}
vector<int> order;
void dfs(int x) {
if (vis[x]) return;
vis[x]=1;
for (auto i: par[x]) {
dfs(i);
}
order.push_back(x);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin.exceptions(ios::badbit|ios::failbit);
cin>>n>>m>>S>>T>>U>>V;
for (int i = 1; i <= m; ++i) {
int u,v,c; cin >>u>>v>>c;
g[u].push_back({v,c});
g[v].push_back({u,c});
}
sp(U,du);sp(V,dv);sp(T,dt);sp(S,ds);
dp1.resize(n+1,oo), dp2.resize(n+1,oo);
dfs(T);
for (auto i: order) {
chmin(dp1[i],dv[i]);
for (auto v: par[i]) {
chmin(dp1[i],dp1[v]);
}
}
reverse(order.begin(),order.end());
for (auto i: order) {
chmin(dp2[i],dv[i]);
for (auto v: chi[i]) {
chmin(dp2[i],dp2[v]);
}
}
ll ans = du[V];
for (int i = 1; i <= n; ++i) {
if (vis[i]) {
ll w1 = du[i];
if (ds[U]+du[i]+dt[i]==ds[T]||ds[i]+du[i]+dt[U]==ds[T]) w1 = 0;
ll w2 = min(dp1[i],dp2[i]);
if (ds[V]+dv[i]+dt[i]==ds[T]||ds[i]+dv[i]+dt[V]==ds[T]) w2 = 0;
chmin(ans, w1+w2);
}
}
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... |