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 pb push_back
#define pf push_front
using namespace std;
#define F first
#define S second
typedef long long ll;
#define pii pair <int, int>
#define pll pair <ll, ll>
typedef long double ld;
const ll N = 2 * 1e5 + 10;
const ll mod = 998244353;
ll um(ll a, ll b){
return ((1LL * a * b) % mod + mod) % mod;
}
ll subr(ll a, ll b){
return ((1LL * a - b) % mod + mod) % mod;
}
ll used[N], cnt[N], was[N], n, m;
vector <pll> q[N];
void create(ll from, ll to, vector <ll>&d){
d[from] = 0;
set <pll> st;
st.insert({d[from], from});
while((ll)st.size() != 0){
auto [place, v] = *st.begin();
st.erase(st.begin());
if(d[v] != place) continue;
for(auto u : q[v]){
if(d[u.F] == -1 || d[u.F] > d[v] + u.S){
d[u.F] = d[v] + u.S;
st.insert({d[u.F], u.F});
}
}
}
}
vector <ll> d, ans;
set <pll> st;
ll nw[N], mn[N];
void last(ll v){
used[v] = true;
if(nw[v] < ans[v]){
ans[v] = nw[v];
st.insert({ans[v], v});
}
for(auto u : q[v]){
if(used[u.F]) continue;
if(d[u.F] + u.S == d[v]) last(u.F);
}
}
void prelast(int v){
was[v] = true;
for(auto u : q[v]){
if(was[u.F]) continue;
if(d[u.F] + u.S == d[v]) prelast(u.F);
}
}
void dfs(ll v, ll dir){
used[v] = true;
mn[v] = min(mn[v], ans[v]);
nw[v] = min(nw[v], mn[v]);
for(auto u : q[v]){
if(used[u.F] || was[u.F] == 0) continue;
if(d[v] + dir * u.S == d[u.F]){
mn[u.F] = min(mn[u.F], mn[v]);
if(--cnt[u.F] == 0) dfs(u.F, dir);
}
}
}
void calc(int v, int dir){
for(ll i = 1; i <= n; i++){
if(!was[i]) continue;
for(auto u : q[i]){
if(!was[u.F]) continue;
if(d[u.F] == dir * u.S + d[i]) cnt[u.F]++;
}
mn[i] = LLONG_MAX;
}
dfs(v, dir);
for(ll i = 1; i <= n; i++){
used[i] = false;
cnt[i] = 0;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
//freopen("bank.in", "r", stdin);
//freopen("bank.out", "w", stdout);
ll from, to, fir, sec;
cin >> n >> m >> from >> to >> fir >> sec;
for(ll i = 0, a, b, c; i < m; i++){
cin >> a >> b >> c;
q[a].pb({b, c});
q[b].pb({a, c});
}
vector <ll> d1, d2;
for(ll i = 0; i <= n; i++){
d1.pb(-1);
d2.pb(-1);
nw[i] = LLONG_MAX;
}
create(from, to, d1);
create(fir, sec, d2);
swap(d1, d);
swap(d2, ans);
prelast(to);
// for(int i = 1; i <= n; i++){
// cout << ans[i] << " ";
// }
// cout << endl;
calc(from, 1);
calc(to, -1);
last(to);
while((ll)st.size() != 0){
auto [place, v] = *st.begin();
st.erase(st.begin());
if(ans[v] != place) continue;
for(auto u : q[v]){
if(ans[u.F] == -1 || ans[u.F] > ans[v] + u.S){
ans[u.F] = ans[v] + u.S;
st.insert({ans[u.F], u.F});
}
}
}
cout << ans[sec];
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... |