이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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<pii>>;
using Dist = vector<LL>;
using Node = tuple<LL, int>; // <cost, node_id>
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<Node, vector<Node>, greater<Node>> pq;
for (auto &src : src_nodes) {
D[src] = 0LL;
pq.push({D[src], src});
}
while (!pq.empty()) {
LL cur;
int x;
tie(cur, x) = pq.top();
pq.pop();
if (D[x] > cur) continue;
for (auto &it : G[x]) {
LL cost;
int y;
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;
// Case 1: Travel U -> V via stations in the railway pass
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 pass_dist = compute_shortest_path(G, shortest_path_nodes, N);
// Case 2: Travel U -> V via direct shortest path
// WA #1
Dist direct_dist = compute_shortest_path(G, {U}, N);
//cout << pass_dist[U] + pass_dist[V] << endl;
//cout << direct_dist[V] << endl;
LL ans = min(
pass_dist[U] + pass_dist[V], // Case #1
direct_dist[V] // Case #2
);
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... |