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 X first
#define Y second
#define ll long long
#define FOR(i,a,b) for(int i = a ; i < b ; ++i)
const int N = 1e5 + 1;
const ll inf = 1e15;
typedef pair<ll,ll> ii;
vector<ii> g[N];
ll fS[N], fT[N];
ll fU[N], fV[N];
int deg[N];
void dijkstra(int S,ll *d) {
FOR(i,1,N) d[i] = inf;
priority_queue<ii,vector<ii>,greater<ii> > pq;
d[S] = 0;
pq.push(ii(0,S));
while (pq.size()) {
int u = pq.top().Y;
ll du = pq.top().X;
pq.pop();
if (du != d[u])
continue;
for(ii e : g[u]) {
int v = e.X;
int C = e.Y;
if (d[v] > du + C) {
d[v] = du + C;
pq.push({d[v],v});
}
}
}
}
ll dp1[N];
ll dp2[N];
ll ans = inf;
vector<int> fr[N];
vector<int> to[N];
void dfs(int u) {
dp1[u] = fU[u];
dp2[u] = fV[u];
for(int v : to[u]) dp1[u] = min(dp1[u],dp1[v]);
for(int v : to[u]) dp2[u] = min(dp2[u],dp2[v]);
for(int v : fr[u]) {
deg[v]--;
if (deg[v] == 0)
dfs(v);
}
ans = min(ans,dp1[u] + fV[u]);
ans = min(ans,dp2[u] + fU[u]);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, m; cin >> n >> m;
int S, T; cin >> S >> T;
int U, V; cin >> U >> V;
FOR(i,0,m) {
int x; cin >> x;
int y; cin >> y;
int c; cin >> c;
g[x].push_back(ii(y,c));
g[y].push_back(ii(x,c));
}
dijkstra(S,fS);
dijkstra(T,fT);
dijkstra(U,fU);
dijkstra(V,fV);
FOR(i,1,n + 1)
for(ii e : g[i]) {
int v = e.X;
int C = e.Y;
if (fS[i] + fT[v] + C == fS[T])
fr[i].push_back(v),
to[v].push_back(i),
deg[v]++;
}
dfs(S); cout << min(ans,fU[V]) << endl;
}
/*
8 8
5 7
6 8
1 2 2
2 3 3
3 4 4
1 4 1
1 5 5
2 6 6
3 7 7
4 8 8
*/
# | 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... |