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;
#define mp make_pair
#define pb push_back
typedef long long int LLI;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vpii;
vpii adjList[100000];
LLI distS[100000],distT[100000],distU[100000],distV[100000];
priority_queue<pair<LLI,int> > H;
int findDist(int s,LLI *dist,int N) {
int i;
for (i = 0; i < N; i++) dist[i] = -1;
dist[s] = 0,H.push(mp(0,s));
while (!H.empty()) {
int u = H.top().second;
LLI d = -H.top().first;
H.pop();
if (d > dist[u]) continue;
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i].first;
if ((dist[v] == -1) || (d+adjList[u][i].second < dist[v])) {
dist[v] = d+adjList[u][i].second;
H.push(mp(-dist[v],v));
}
}
}
return 0;
}
LLI dist1[100000],dist2[100000];
int visited[100000];
LLI doDFS(int u,LLI *a,LLI *b,LLI *c) {
if (visited[u]) return c[u];
int i;
visited[u] = 1,c[u] = b[u];
for (i = 0; i < adjList[u].size(); i++) {
int v = adjList[u][i].first;
if (a[u] == a[v]+adjList[u][i].second) c[u] = min(c[u],doDFS(v,a,b,c));
}
return c[u];
}
int main() {
int i;
int N,M;
int S,T,U,V;
int A,B,C;
scanf("%d %d %d %d %d %d",&N,&M,&S,&T,&U,&V);
S--,T--,U--,V--;
for (i = 0; i < M; i++) {
scanf("%d %d %d",&A,&B,&C);
A--,B--;
adjList[A].pb(mp(B,C));
adjList[B].pb(mp(A,C));
}
findDist(S,distS,N),findDist(T,distT,N);
findDist(U,distU,N),findDist(V,distV,N);
LLI ans = distU[V];
for (i = 0; i < N; i++) doDFS(i,distS,distU,dist1);
fill(visited,visited+N,0);
for (i = 0; i < N; i++) doDFS(i,distT,distV,dist2);
for (i = 0; i < N; i++) {
if (distS[i]+distT[i] == distS[T]) ans = min(ans,dist1[i]+dist2[i]);
}
fill(visited,visited+N,0);
for (i = 0; i < N; i++) doDFS(i,distS,distV,dist1);
fill(visited,visited+N,0);
for (i = 0; i < N; i++) doDFS(i,distT,distU,dist2);
for (i = 0; i < N; i++) {
if (distS[i]+distT[i] == distS[T]) ans = min(ans,dist1[i]+dist2[i]);
}
printf("%lld\n",ans);
return 0;
}
Compilation message (stderr)
commuter_pass.cpp: In function 'int findDist(int, LLI*, int)':
commuter_pass.cpp:23:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < adjList[u].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~
commuter_pass.cpp: In function 'LLI doDFS(int, LLI*, LLI*, LLI*)':
commuter_pass.cpp:39:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < adjList[u].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~
commuter_pass.cpp: In function 'int main()':
commuter_pass.cpp:50:10: 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:53:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d",&A,&B,&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... |