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>
#define FOR(i,s,e) for(int i=(s);i<(int)(e);i++)
#define FOE(i,s,e) for(int i=(s);i<=(int)(e);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(x) (x).begin(), (x).end()
#define CLR(s) memset(s,0,sizeof(s))
#define PB push_back
#define ITER(v) __typeof((v).begin())
#define FOREACH(i,v) for(ITER(v) i=(v).begin();i!=(v).end();i++)
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL, LL> pll;
typedef map<int,int> mii;
typedef vector<int> vi;
const LL INF = 1LL << 40;
using Graph = vector<vector<pll>>;
using Dist = vector<LL>;
int N, M, S, T, U, V;
Graph G;
Dist compute_shortest_path(Graph &G, vector<int> src_nodes, int N) {
Dist D(N + 1, INF);
priority_queue<pll, vector<pll>, greater<pll>> pq;
for (auto &src : src_nodes) {
D[src] = 0;
pq.push({0, src});
}
while (!pq.empty()) {
int cur, x;
tie(cur, x) = pq.top();
pq.pop();
if (D[x] > cur) continue;
for (auto &it : G[x]) {
int y, cost;
tie(y, cost) = it;
if (cur + cost < D[y]) {
D[y] = cur + cost;
pq.push({D[y], y});
}
}
}
return D;
}
int main() {
int A, B, C;
cin >> N >> M >> S >> T >> U >> V;
G.resize(N + 1);
FOR(_, 0, M) {
cin >> A >> B >> C;
G[A].PB({B, C});
G[B].PB({A, C});
}
Dist src_dist = compute_shortest_path(G, {S}, N);
Dist dst_dist = compute_shortest_path(G, {T}, N);
//cout << src_dist[T] << " " << dst_dist[S] << endl;
LL shortest_path_cost = src_dist[T];
assert (src_dist[T] == dst_dist[S]);
vector<int> shortest_path_nodes;
FOE(x, 1, N)
if (src_dist[x] + dst_dist[x] == shortest_path_cost)
shortest_path_nodes.PB(x);
Dist final_dist = compute_shortest_path(G, shortest_path_nodes, N);
LL ans = final_dist[U] + final_dist[V];
cout << ans << endl;
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... |