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 int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
const int N = 2e5 + 7, INF = 1e18 + 7;
struct Edge { int v, c; }; vector <Edge> g[N];
void dj1(int S, int dist[N]) {
for (int i = 0; i < N; ++i) dist[i] = INF;
set <ii> ms; ms.insert({0, S}); dist[S] = 0;
while (ms.size()) {
int u = ms.begin()->second; ms.erase(ms.begin());
for (auto e : g[u]) if (dist[u] + e.c < dist[e.v]) { ms.erase({dist[e.v], e.v}); dist[e.v] = dist[u] + e.c; ms.insert({dist[e.v], e.v}); }
}
}
void dj2(int S, int dist[N], vector <int> rg[N]) {
for (int i = 0; i < N; ++i) dist[i] = INF;
set <ii> ms; ms.insert({0, S}); dist[S] = 0;
while (ms.size()) {
int u = ms.begin()->second; ms.erase(ms.begin());
for (auto e : g[u]) {
if (dist[u] + e.c < dist[e.v]) {
ms.erase({dist[e.v], e.v}); dist[e.v] = dist[u] + e.c; ms.insert({dist[e.v], e.v});
rg[e.v] = {u};
}
else if (dist[u] + e.c == dist[e.v]) {
rg[e.v].app(u);
}
}
}
}
int n, m, S, T, U, V, distU[N], distV[N], distS[N], ans = INF;
vector <int> dag[N], rg[N];
bool used[N], t[N]; int toU[N], toV[N];
void dfs(int u) {
used[u] = 1;
t[u] = u == T; toU[u] = distU[u]; toV[u] = distV[u];
for (int v : dag[u]) {
if (!used[v]) dfs(v);
if (t[v]) { toU[u] = min(toU[u], toU[v]); toV[u] = min(toV[u], toV[v]); t[u] = 1; }
}
if (t[u]) {
ans = min(ans, distU[u] + toV[u]);
ans = min(ans, distV[u] + toU[u]);
}
}
signed main() {
#ifdef HOME
freopen("input.txt", "r", stdin);
#else
ios_base::sync_with_stdio(0); cin.tie(0);
#endif
cin >> n >> m >> S >> T >> U >> V;
for(int i = 0; i < m; ++i) {
int u, v, c; cin >> u >> v >> c; g[u].app({v,c}); g[v].app({u,c});
}
dj1(U,distU);dj1(V,distV);dj2(S,distS,rg);
for (int i = 1; i <= n; ++i) for (int v : rg[i]) dag[v].app(i);
ans = distU[V];
dfs(S);
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... |