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>
using namespace std;
typedef long long ll;
#define endl '\n'
#define hitaf ios_base::sync_with_stdio(false); cin.tie(NULL);
#define fi first
#define se second
const ll MOD = 998244353;
const ll INF = 1e18;
ll n, m, s, t, u, V;
vector<pair<ll, ll>> adj[100005];
bool vis[100005];
int main(){
//hitaf
cin >> n >> m;
cin >> s >> t;
cin >> u >> V;
vector<ll> dist(n + 2, INF), dist2(n + 2, INF);
for(int i = 1; i <= m; i++){
ll x, y, w;
cin >> x >> y >> w;
adj[x].push_back({y, w});
adj[y].push_back({x,w});
}
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
pq.push({0, s});
dist[s] = 0;
while(!pq.empty()){
auto now = pq.top();
pq.pop();
if(dist[now.se] < now.fi) continue;
for(auto v: adj[now.se]){
if(dist[v.fi] < now.fi + v.se) continue;
dist[v.fi] = now.fi + v.se;
pq.push({dist[v.fi], v.fi});
}
}
vector<ll> p(n + 2, -1);
queue<ll> q;
q.push(t);
while(!q.empty()){
ll now = q.front();
q.pop();
for(auto v: adj[now]){
if(dist[v.fi] + v.se == dist[now]){
p[now] = v.fi;
if(vis[v.fi]) continue;
vis[v.fi] = true;
q.push(v.fi);
}
}
}
vector<bool> vis2(n + 2);
priority_queue<pair<ll, pair<ll, ll>>, vector<pair<ll, pair<ll, ll>>>, greater<pair<ll, pair<ll, ll>>>> pq2;
pq2.push({0, {u, -1}});
while(!pq2.empty()){
auto now = pq2.top();
pq2.pop();
if(dist2[now.se.fi] < now.fi || vis2[now.se.fi]) continue;
vis2[now.se.fi] = true;
for(auto v: adj[now.se.fi]){
ll w = v.se;
if((p[now.se.fi] == v.fi && (now.se.se == 0 || now.se.se == -1)) || (p[v.fi] == now.se.fi && (now.se.se == 1 || now.se.se == -1))) w = 0;
//if(now.se.fi == 2) cout << w << " " << v.fi << endl;
if(dist2[v.fi] < now.fi + w) continue;
dist2[v.fi] = now.fi + w;
ll tmp = now.se.se;
if(tmp == -1){
if(p[now.se.fi] == v.fi) tmp = 0;
else if(p[v.fi] == now.se.fi) tmp = 1;
}
pq2.push({dist2[v.fi], {v.fi, tmp}});
}
}
//for(auto x: st) cout << x.fi << " " << x.se << endl;
cout << dist2[V] << endl;
}
/*
7 1 2 9 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... |