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;
const int mxN = 1e5+2;
const long long inf = 1e18;
vector<pair<int, int>> g[mxN];
vector<long long> dists(mxN), distu(mxN), distv(mxN), mn(mxN), dist(mxN);
bool visited[mxN];
void dijkstra(int s, vector<long long> &dist) {
fill(visited, visited+mxN, 0);
fill(dist.begin(), dist.end(), inf);
dist[s] = 0;
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<>> pq;
pq.push({0, s});
while (!pq.empty()) {
auto [x, u] = pq.top();
pq.pop();
if (visited[u]) continue;
visited[u] = 1;
for (auto [v, w] : g[u]) {
if (dist[v] > x + w) {
dist[v] = x + w;
pq.push({dist[v], v});
}
}
}
}
int n, m;
long long f(int S, int T, int U, int V) {
dijkstra(S, dists); dijkstra(U, distu); dijkstra(V, distv);
fill(visited, visited+mxN, 0);
fill(mn.begin(), mn.end(), inf);
fill(dist.begin(), dist.end(), inf);
dist[T] = 0;
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<>> pq;
pq.push({0, T});
while (!pq.empty()) {
auto [x, u] = pq.top();
pq.pop();
if (visited[u]) continue;
visited[u] = 1;
mn[u] = distv[u];
for (auto [v, w] : g[u]) {
if (x == dist[v] + w) {
mn[u] = min(mn[v], mn[u]);
}
if (dist[v] > w + x) {
dist[v] = w + x;
pq.push({dist[v], v});
}
}
}
long long ans = distu[V], anss = inf;
for (int i = 1; i <= n; ++i) {
if (dists[i] + dist[i] == dists[T]) ans = min(ans, distu[i] + mn[i]);
}
return ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
int S, T, U, V;
cin >> S >> T >> U >> V;
for (int i = 0; i < m; ++i) {
int u, v, w;
cin >> u >> v >> w;
g[u].push_back({v, w}), g[v].push_back({u, w});
}
cout << min(f(S, T, U, V), f(T, S, U, V)) << '\n';
}
Compilation message (stderr)
commuter_pass.cpp: In function 'long long int f(int, int, int, int)':
commuter_pass.cpp:54:31: warning: unused variable 'anss' [-Wunused-variable]
54 | long long ans = distu[V], anss = inf;
| ^~~~
# | 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... |