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 int long long
using namespace std;
signed main() {
int N, M, S, T, U, V; cin >> N >> M >> S >> T >> U >> V;
vector<pair<int,int> > adj[N+1];
for(int i=0; i<M; i++) {
int a, b, c; cin >> a >> b >> c;
adj[a].push_back({b, c});
adj[b].push_back({a, c});
}
int dist[N+1]; int vis[N+1];
for(int i=1; i<=N; i++) dist[i] = 1e18, vis[i] = 0;
dist[S] = 0;
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >pq;
for(int i=1; i<=N; i++) pq.push({dist[i], i});
while(pq.size()) {
pair<int,int> t=pq.top(); pq.pop();
if(!vis[t.second]) {
vis[t.second] = 1;
for(auto x : adj[t.second]) {
if(!vis[x.first] && dist[x.first] > dist[t.second] + x.second) {
dist[x.first] = dist[t.second] + x.second;
pq.push({dist[x.first], x.first});
}
}
}
}
int dist2[N+1]; int vis2[N+1];
for(int i=1; i<=N; i++) dist2[i] = 1e18, vis2[i] = 0;
dist2[T] = 0;
for(int i=1; i<=N; i++) pq.push({dist2[i], i});
while(pq.size()) {
pair<int,int> t=pq.top(); pq.pop();
if(!vis2[t.second]) {
vis2[t.second] = 1;
for(auto x : adj[t.second]) {
if(!vis2[x.first] && dist2[x.first] > dist2[t.second] + x.second) {
dist2[x.first] = dist2[t.second] + x.second;
pq.push({dist2[x.first], x.first});
}
}
}
}
int dist3[N+1]; int vis3[N+1];
for(int i=1; i<=N; i++) dist3[i] = 1e18, vis3[i] = 0;
dist3[V] = 0;
for(int i=1; i<=N; i++) pq.push({dist3[i], i});
while(pq.size()) {
pair<int,int> t=pq.top(); pq.pop();
if(!vis3[t.second]) {
vis3[t.second] = 1;
for(auto x : adj[t.second]) {
if(!vis3[x.first] && dist3[x.first] > dist3[t.second] + x.second) {
dist3[x.first] = dist3[t.second] + x.second;
pq.push({dist3[x.first], x.first});
}
}
}
}
int ans = dist[V];
for(int i=1; i<=N; i++) {
if(dist[i] + dist2[i] == dist[T]) {
ans = min(ans, dist3[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... |