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;
using ll = long long;
using pli = pair<ll, int>;
const ll inf = 1e18;
const int N = 100100;
int n, m, S, T, U, V;
vector<pli> adj[N];
ll ans;
ll dU[N];
ll dV[N];
ll dS[N];
ll mdU[N];
ll mdV[N];
void dijkstra(int source, ll dist[N]) {
priority_queue<pli, vector<pli>, greater<pli>> pq;
for (int i = 1; i <= n; ++i) {
dist[i] = inf;
}
dist[source] = 0;
pq.emplace(0, source);
while (!pq.empty()) {
ll d, w;
int u, v;
tie(d, u) = pq.top();
pq.pop();
if (d != dist[u]) continue;
for (pli to : adj[u]) {
tie(w, v) = to;
if (d + w < dist[v]) {
dist[v] = d + w;
pq.emplace(dist[v], v);
}
}
}
}
void dijkstra2() {
priority_queue<pli, vector<pli>, greater<pli>> pq;
for (int i = 1; i <= n; ++i) {
dS[i] = inf;
}
dS[S] = 0;
mdU[S] = dU[S];
mdV[S] = dV[S];
pq.emplace(0, S);
while (!pq.empty()) {
ll d, w;
int a, b;
tie(d, a) = pq.top();
pq.pop();
if (d != dS[a]) continue;
for (pli to : adj[a]) {
// a -> b
tie(w, b) = to;
if (d + w < dS[b]) {
dS[b] = d + w;
mdU[b] = min(mdU[a], dU[b]);
mdV[b] = min(mdV[a], dV[b]);
pq.emplace(dS[b], b);
} else if (d + w == dS[b]) {
mdU[b] = min(mdU[b], mdU[a]);
mdV[b] = min(mdV[b], mdV[a]);
}
}
}
}
int main() {
cin.tie(0)->sync_with_stdio(false);
// #ifndef _DEBUG
// freopen("piepie.in", "r", stdin);
// freopen("piepie.out", "w", stdout);
// #endif
cin >> n >> m >> S >> T >> U >> V;
for (int i = 0; i < m; ++i) {
int a, b, w;
cin >> a >> b >> w;
adj[a].emplace_back(w, b);
adj[b].emplace_back(w, a);
}
dijkstra(U, dU);
dijkstra(V, dV);
dijkstra2();
ans = min(dU[V], dV[U]);
ans = min(ans, mdU[T] + mdV[T]);
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... |