This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// In the name of God
// Hope is last to die
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define pb push_back
#define S second
#define F first
#define mp make_pair
#define smax(x, y) (x) = max((x), (y))
#define smin(x, y) (x) = min((x), (y))
#define all(x) (x).begin(), (x).end()
#define len(x) ((int)(x).size())
const int maxn = 1e5 + 5;
const ll inf = 1e18 + 7;
ll n, m, s, t, x, y, ans = inf, d[maxn][3], dp[maxn][3];
set<pll> st;
vector<pll> adj[maxn];
vector<int> g[maxn];
bool mark[maxn];
void dij(int v, int ind){
st.clear();
st.insert(mp(0, v));
d[v][ind] = 0;
while(st.size()){
ll v = (*st.begin()).S, dist = (*st.begin()).F;
st.erase(st.begin());
for(auto [u, w]: adj[v]){
if(d[v][ind] + w < d[u][ind]){
st.erase(mp(d[u][ind], u));
d[u][ind] = d[v][ind] + w;
st.insert(mp(d[u][ind], u));
}
}
}
}
void dfs(int v){
mark[v] = 1;
dp[v][1] = dp[v][2] = inf;
for(auto u: g[v]){
if(!mark[u]) dfs(u);
smin(dp[v][1], dp[u][1]);
smin(dp[v][2], dp[u][2]);
}
if(dp[v][1] == inf){
if(v == t){
dp[v][1] = d[v][1];
dp[v][2] = d[v][2];
}
}
else{
smin(dp[v][1], d[v][1]);
smin(dp[v][2], d[v][2]);
}
smin(ans, d[v][1] + dp[v][2]);
smin(ans, d[v][2] + dp[v][1]);
}
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
memset(d, 63, sizeof d);
cin >> n >> m >> s >> t >> x >> y;
for(int i = 1; i <= m; i++){
int u, v, w; cin >> u >> v >> w;
adj[u].pb(mp(v, w));
adj[v].pb(mp(u, w));
}
dij(s, 0);
dij(x, 1);
dij(y, 2);
for(int i = 1; i <= n; i++){
for(auto [u, w]: adj[i]){
if(d[i][0] + w == d[u][0]) g[i].pb(u);
}
}
dfs(s);
cout << min(ans, d[y][1]) << '\n';
return 0;
}
Compilation message (stderr)
commuter_pass.cpp: In function 'void dij(int, int)':
commuter_pass.cpp:34:33: warning: unused variable 'dist' [-Wunused-variable]
34 | ll v = (*st.begin()).S, dist = (*st.begin()).F;
| ^~~~
# | 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... |