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 <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <climits>
using namespace std;
int n, m, s, t, u, v;
vector<vector<int>> parents;
vector<bool> on_sp;
vector<vector<pair<int, int>>> adj;
vector<bool> visited;
void dfs(int s){
if(visited[s])
return;
visited[s] = true;
on_sp[s] = true;
for(auto u : parents[s])
dfs(u);
}
void dijkstras_ST(){
priority_queue<pair<long long, int>> q;
vector<long long>dist(n+1,LONG_LONG_MAX);
q.push({0,s});
dist[s] = 0;
while(!q.empty()){
int node = q.top().second;
long long dist_node = -q.top().first;
q.pop();
if(visited[node])
continue;
visited[node] = true;
for(auto edge : adj[node]){
int next = edge.first;
int weight = edge.second;
if(dist[node] + weight < dist[next]){
dist[next] = dist[node] + weight;
parents[next].clear();
parents[next].push_back(node);
q.push({-dist[next],next});
}else if(dist[node] + weight == dist[next]){
parents[next].push_back(node);
}
}
}
}
void dijkstras_UV(int start, vector<long long> &dist){
priority_queue<pair<long long, int>> q;
q.push({0,start});
dist[start] = 0;
while(!q.empty()){
int node = q.top().second;
long long dist_node = -q.top().first;
q.pop();
if(visited[node])
continue;
visited[node] = true;
for(auto edge : adj[node]){
int next = edge.first;
int weight = edge.second;
if(dist[node] + weight < dist[next]){
dist[next] = dist[node] + weight;
q.push({-dist[next],next});
}
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
freopen("in.txt","r",stdin);
cin >> n >> m >> s >> t >> u >> v;
adj = vector<vector<pair<int, int>>>(n+1);
parents = vector<vector<int>>(n+1);
on_sp = vector<bool>(n+1);
visited = vector<bool>(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});
}
dijkstras_ST();
fill(visited.begin(), visited.end(),false);
dfs(t);
fill(visited.begin(), visited.end(), false);
vector<long long> distU(n+1, LONG_LONG_MAX);
dijkstras_UV(u, distU);
fill(visited.begin(), visited.end(), false);
vector<long long> distV(n+1, LONG_LONG_MAX);
dijkstras_UV(v, distV);
long long minu = LONG_LONG_MAX, minv = LONG_LONG_MAX;
for(int i = 1; i <= n; i++)
if(on_sp[i])
minu = min(minu, distU[i]);
for(int i = 1; i <= n; i++)
if(on_sp[i])
minv = min(minv, distV[i]);
long long ans = min(minu+minv,distU[v]);
cout << ans << '\n';
return 0;
}
Compilation message (stderr)
commuter_pass.cpp: In function 'void dijkstras_ST()':
commuter_pass.cpp:28:19: warning: unused variable 'dist_node' [-Wunused-variable]
28 | long long dist_node = -q.top().first;
| ^~~~~~~~~
commuter_pass.cpp: In function 'void dijkstras_UV(int, std::vector<long long int>&)':
commuter_pass.cpp:53:19: warning: unused variable 'dist_node' [-Wunused-variable]
53 | long long dist_node = -q.top().first;
| ^~~~~~~~~
commuter_pass.cpp: In function 'int main()':
commuter_pass.cpp:72:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | freopen("in.txt","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
# | 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... |