//#pragma GCC optomize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("O3")
//#pragma GCC target("avx2")
//#pragma GCC target("sse,sse2,sse4.1,sse4.2")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define max_heap priority_queue<pair <ll, pair <ll, ll>>>
#define min_heap priority_queue<pair <ll, ll>, vector<pair <ll, ll>>, greater<pair <ll, ll>>>
//#define min_heap priority_queue<ll, vector<ll>, greater<ll>>
#define sariE cin.tie(NULL); cout.tie(NULL); ios_base::sync_with_stdio(false);
#define filE freopen("in.txt", "r", stdin); freopen("out1.txt", "w", stdout);
#define endl '\n'
#define md(a) (a % mod + mod) % mod
#define pb push_back
//cout << vectorprecision(5) << fixed << f;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll const maxn = 2e5 + 123;
ll const inf = 2e18;
ll const loG = 23;
//ll const mod = 1e9 + 7;
ll const mod = 998244353;
ll const sq = 350;
ll power(ll a, ll b, ll mod){if(b==0)return 1;if(b==1)return a;ll x = power(a, b / 2, mod);return (((x * x) % mod) * (b % 2 ? a : 1)) % mod;}
ll n, m, s, t, uu, vv, disu[maxn], disv[maxn], jell[maxn], posh[maxn];
bool mark[maxn];
vector <pair <ll, ll>> g[maxn];
vector <ll> par[maxn], child[maxn];
void djk(ll v, ll dis[maxn]){
for (int i = 1; i < n + 1; i++){
dis[i] = inf;
mark[i] = 0;
}
dis[v] = 0;
min_heap qu; qu.push({0, v});
while (qu.size()){
v = qu.top().second; qu.pop();
if (mark[v])
continue;
mark[v] = 1;
for (auto u : g[v]){
if (!mark[u.first] && dis[u.first] > dis[v] + u.second){
dis[u.first] = dis[v] + u.second;
qu.push({dis[u.first], u.first});
}
}
}
}
void babayab(ll v){
ll dis[maxn];
for (int i = 1; i < n + 1; i++){
dis[i] = inf;
}
dis[v] = 0;
min_heap qu; qu.push({0, v});
while (qu.size()){
v = qu.top().second; qu.pop();
for (auto u : g[v]){
if (dis[u.first] > dis[v] + u.second){
dis[u.first] = dis[v] + u.second;
par[u.first].clear(); par[u.first].push_back(v);
qu.push({dis[u.first], u.first});
}
else if (dis[u.first] == dis[v] + u.second){
par[u.first].push_back(v);
}
}
}
}
void bache(ll v){
for (int i = 1; i < n + 1; i++)
mark[i] = 0;
queue <ll> qu; qu.push(v);
while (qu.size()){
v = qu.front(); qu.pop();
if (mark[v])
continue;
mark[v] = 1;
for (ll u : par[v]){
qu.push(u);
child[u].push_back(v);
}
}
}
ll getjell(ll v){
if (jell[v] != -1)
return jell[v];
ll ret = disv[v];
for (ll u : child[v]){
ret = min(ret, getjell(u));
}
jell[v] = ret;
return ret;
}
ll getposh(ll v){
if (posh[v] != -1)
return posh[v];
ll ret = disv[v];
for (ll u : par[v]){
ret = min(ret, getposh(u));
}
posh[v] = ret;
return ret;
}
void Solve(){
cin >> n >> m >> s >> t >> uu >> vv;
for (int i = 1; i < m + 1; i++){
ll a, b, c; cin >> a >> b >> c;
g[a].push_back({b, c});
g[b].push_back({a, c});
}
djk(uu, disu);
djk(vv, disv);
babayab(s);
bache(t);
for (int i = 1; i < n + 1; i++){
jell[i] = -1;
posh[i] = -1;
}
ll ans = disu[vv];
for (int i = 1; i < n + 1; i++){
if (mark[i])
ans = min(ans, disu[i] + min(getjell(i), getposh(i)));
}
cout << ans << endl;
}
int main(){
sariE;// filE;
int test = 1;
//cin >> test;
while (test--) Solve();
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... |