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>
/* #define int long long */
#define ll long long
#define fi first
#define se second
#define ve vector
using namespace std;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define all(x) x.begin(), x.end()
const int MAXN = 1e6+10;
ve<pll> g[MAXN];
ve<ll> ds, da, db, dt;
ll dpl[MAXN], dpr[MAXN], dp[MAXN];
bool vis[MAXN];
void djkstra(int s, ve<ll>& d){
set<pll> st;
st.insert({0, s});
d[s] = 0;
while(!st.empty()){
int v = st.begin()->se;
st.erase(st.begin());
for(auto to : g[v]){
if(d[to.fi] > d[v] + to.se){
if(st.find({d[to.fi], to.fi}) != st.end())
st.erase({d[to.fi], to.fi});
d[to.fi] = d[v] + to.se;
st.insert({d[to.fi], to.fi});
}
}
}
}
void dfsl(int v){
vis[v] = 1;
dpl[v] = da[v];
for(auto& to : g[v]){
if(ds[v] - to.se == ds[to.fi]){
if(!vis[to.fi])
dfsl(to.fi);
dpl[v] = min(dpl[v], dpl[to.fi]);
}
}
}
void dfsr(int v){
vis[v] = 1;
dpr[v] = da[v];
for(auto& to : g[v])
if(dt[v] - to.se == dt[to.fi]){
if(!vis[to.fi])
dfsr(to.fi);
dpr[v] = min(dpr[v], dpr[to.fi]);
}
dp[v] = min(dpl[v], dpr[v]);
}
signed main(){
int n, m;
cin >> n >> m;
int s, t, a, b;
cin >> s >> t >> a >> b;
s--; t--; a--; b--;
for(int i = 0; i < m; i++){
ll u, v, w;
cin >> u >> v >> w;
u--; v--;
g[u].push_back({v, w});
g[v].push_back({u, w});
}
ds.resize(n, 1e18);
da.resize(n, 1e18);
db.resize(n, 1e18);
dt.resize(n, 1e18);
fill(dpl, dpl + n, 1e18);
fill(dpr, dpr + n, 1e18);
fill(dp, dp + n, 1e18);
djkstra(s, ds);
djkstra(a, da);
djkstra(b, db);
djkstra(t, dt);
dfsl(t);
memset(vis, 0, sizeof(vis));
dfsr(s);
ll res = da[b];
for(int i = 0; i < n; i++){
res = min(res, dp[i] + db[i]);
}
cout << res;
}
# | 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... |