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 <iostream>
#include <vector>
#include <queue>
#include <utility>
#include <tuple>
#include <cmath>
using namespace std;
vector<long long> min_dist(vector<vector<pair<int, long long>>> &adj, int source, int n) {
vector<long long> depth(n, -1);
using T = pair<long long, int>;
priority_queue<T, vector<T>, greater<T>> q;
q.push({ 0, source });
while (!q.empty()) {
auto [ d, v ] = q.top();
q.pop();
if (depth[v] != -1) continue;
depth[v] = d;
for (auto [ u, w ] : adj[v]) {
q.push({ d + w, u });
}
}
return depth;
}
struct Path {
long long cost;
long long u_dist;
long long v_dist;
friend bool operator>(const Path &l, const Path &r) {
if (l.cost == r.cost) {
return (l.u_dist > r.u_dist) && (l.v_dist > r.v_dist);
}
return l.cost > r.cost;
}
friend bool operator<(const Path &l, const Path &r) {
return r > l;
}
};
int main() {
int n, m, s, t, u, v;
cin >> n >> m >> s >> t >> u >> v;
s--; t--; u--; v--;
vector<vector<pair<int, long long>>> adj(n);
for (int i = 0; i < m; i++) {
int a, b;
long long c;
cin >> a >> b >> c;
a--; b--;
adj[a].push_back({ b, c });
adj[b].push_back({ a, c });
}
vector<long long> min_u = min_dist(adj, u, n);
vector<long long> min_v = min_dist(adj, v, n);
vector<Path> ans(n);
vector<long long> dist(n);
vector<bool> visited(n);
using T = pair<Path, int>;
priority_queue<T, vector<T>, greater<T>> q;
q.push({ { 0, min_u[s], min_v[s] }, s });
while (!q.empty()) {
auto [ path, r ] = q.top();
q.pop();
if (!visited[r]) {
visited[r] = true;
ans[r] = path;
dist[r] = path.u_dist + path.v_dist;
}
if (ans[r] < path) continue;
dist[r] = min(dist[r], path.u_dist + path.v_dist);
for (auto [ p, w ] : adj[r]) {
q.push({ { path.cost + w, min(path.u_dist, min_u[p]), min(path.v_dist, min_v[p]) }, p });
}
}
cout << (ans[t].u_dist + ans[t].v_dist);
return 0;
}
# | 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... |