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>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define bupol __builtin_popcount
#define int long long
#define ll long long
#define ld long double
#define fi first
#define se second
#define pb push_back
#define lf (id<<1)
#define rg ((id<<1)|1)
#define md ((l+r)>>1)
using namespace std;
const int MAXN = 2e5+5;
const int LOG = 20;
const int MOD = 1e9+7;
const int SQRT = 520;
const int INF = 1e18;
typedef pair<int,int> pii;
typedef pair<int,pii> ipii;
int n, m;
int s, t, x, y;
int u[MAXN], v[MAXN], c[MAXN];
vector <pii> adj[MAXN]; vector <int> dag[MAXN], dag2[MAXN];
int dis[MAXN], di[MAXN];
int deg[MAXN], deg2[MAXN];
priority_queue<pii, vector<pii>, greater<pii>> pq;
map<pii,int> ma;
void dji(int sta){
for(int i=1; i<=n; i++) dis[i] = INF;
pq.push({0, sta});
dis[sta] = 0;
while(!pq.empty()){
int tp = pq.top().se; int dist = pq.top().fi; pq.pop();
if(dis[tp] < dist) continue;
for(auto ed : adj[tp]){
int nx = ed.fi; int wei = ed.se;
if(dis[nx] > dist+wei){
dis[nx] = dist+wei;
pq.push({dis[nx], nx});
}
}
}
}
void dji2(int sta){
for(int i=1; i<=n; i++) di[i] = INF;
pq.push({0, sta});
di[sta] = 0;
while(!pq.empty()){
int tp = pq.top().se; int dist = pq.top().fi; pq.pop();
if(di[tp] < dist) continue;
for(auto ed : adj[tp]){
int nx = ed.fi; int wei = ed.se;
if(di[nx] > dist+wei){
di[nx] = dist+wei;
pq.push({di[nx], nx});
}
}
}
}
int dp[MAXN], dp2[MAXN];
void dfs(int nw){
dp[nw] = min(dp[nw], dis[nw]);
for(auto nx : dag[nw]){
dp[nx] = min(dp[nx], dp[nw]);
deg[nx]--;
if(deg[nx] == 0) dfs(nx);
}
}
void dfs2(int nw){
dp2[nw] = min(dp2[nw], dis[nw]);
for(auto nx : dag2[nw]){
dp2[nx] = min(dp2[nx], dp2[nw]);
deg2[nx]--;
if(deg2[nx] == 0) dfs2(nx);
}
}
signed main(){
//ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> m;
cin >> s >> t; cin >> x >> y;
for(int i=1; i<=m; i++){
cin >> u[i] >> v[i] >> c[i];
if(u[i] > v[i]) swap(u[i], v[i]);
ma[pii(u[i], v[i])] = i;
adj[u[i]].pb({v[i], c[i]}); adj[v[i]].pb({u[i], c[i]});
}
dji(s); dji2(t);
int mn = dis[t];
//cout << mn << " mn\n";
for(int i=1; i<=m; i++){
if(dis[u[i]]+di[u[i]] == mn && dis[v[i]]+di[v[i]] == mn){
if(dis[u[i]]+c[i] == dis[v[i]]){
dag[u[i]].pb(v[i]);
deg[v[i]]++;
dag2[v[i]].pb(u[i]);
deg2[u[i]]++;
} else if(dis[v[i]]+c[i] == dis[u[i]]){
dag[v[i]].pb(u[i]);
deg[u[i]]++;
dag2[u[i]].pb(v[i]);
deg2[v[i]]++;
}
}
}
// for(int i=1; i<=n; i++){
// cout << i << " in\n";
// for(auto in : dag[i]) cout << in << ' ';
// cout << '\n';
// }
dji(x); dji2(y); // dis x -> node, forloop dag
for(int i=1; i<=n; i++){
dp[i] = INF; dp2[i] = INF;
}
dfs(s); dfs2(t);
//for(int i=1; i<=n;i++) cout << i << ' ' << dis[i] << ' ' << dp[i] << ' '<< di[i]<< " i\n";
int ans = dis[y];
for(int i=1; i<=n; i++){
ans = min(ans, dp[i]+di[i]);
ans = min(ans, dp2[i]+di[i]);
}
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... |