This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//https://oj.uz/problem/view/JOI18_commuter_pass
#include<bits/stdc++.h>
const int N = 1e5 + 5;
const long long inf = 1e18 + 7;
using namespace std;
typedef pair <int, long long> ii;
vector <ii> adj[N];
vector <long long> dS, dU, dV;
int n, m, U, V, S, T;
long long MinU[N], MinV[N], ans;
bool visitT[N], visit[N];
void dijkstra(int S, vector <long long>& d){
d.assign(n+1, inf); d[S] = 0;
priority_queue <ii, vector <ii>, greater <ii>> mq; mq.push({d[S], S});
while (mq.size()){
ii z = mq.top(); mq.pop();
int u = z.second; long long val = z.first;
if (val != d[u]) continue;
for (int i = 0; i < adj[u].size(); i++){
int v = adj[u][i].first; long long cost = adj[u][i].second;
if (d[v] > d[u] + cost){
d[v] = d[u] + cost;
mq.push({d[v], v});
}
}
}
}
void dfs(int u){
if (visit[u]) return;
visit[u] = true;
MinU[u] = MinV[u] = inf;
if (u == T) visitT[u] = true;
for (int i = 0; i < adj[u].size(); i++){
int v = adj[u][i].first; long long cost = adj[u][i].second;
if (dS[v] != dS[u] + cost) continue;
dfs(v);
if (visitT[v] == true){
visitT[u] = true;
MinU[u] = min(MinU[u], MinU[v]);
MinV[u] = min(MinV[u], MinV[v]);
}
}
if (visitT[u] == true){
ans = min(ans, MinU[u] + dV[u]);
ans = min(ans, MinV[u] + dU[u]);
MinU[u] = min(MinU[u], dU[u]);
MinV[u] = min(MinV[u], dV[u]);
}
}
int main(){
scanf("%d %d %d %d %d %d", &n, &m, &S, &T, &U, &V);
for (int i = 1; i <= m; i++){
int u, v; long long c;
scanf("%d %d %lld", &u, &v, &c);
adj[u].push_back({v, c});
adj[v].push_back({u, c});
}
dijkstra(S, dS); dijkstra(U, dU); dijkstra(V, dV);
ans = dU[V]; dfs(S);
printf("%lld", ans);
}
Compilation message (stderr)
commuter_pass.cpp: In function 'void dijkstra(int, std::vector<long long int>&)':
commuter_pass.cpp:23:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < adj[u].size(); i++){
^
commuter_pass.cpp: In function 'void dfs(int)':
commuter_pass.cpp:38:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < adj[u].size(); i++){
^
commuter_pass.cpp: In function 'int main()':
commuter_pass.cpp:57:55: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d %d %d %d", &n, &m, &S, &T, &U, &V);
^
commuter_pass.cpp:60:40: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %lld", &u, &v, &c);
^
# | 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... |