이 제출은 이전 버전의 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 << 60;
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;
}
Dist S_dist, T_dist, U_dist, V_dist;
LL ST_shortest_path_cost;
Graph G_ST; // edges consisting of edges used in any shortest paths
LL dfs(Graph &G, int x, LL u_min_dist, LL v_min_dist) {
// x : current node
// DFS - traverse all possible shortest paths
LL ans = INF;
// u -> some nodes visited -> x -> v
u_min_dist = min(u_min_dist, U_dist[x]);
ans = min(ans, u_min_dist + V_dist[x]);
// v -> some nodes visited -> x -> u
v_min_dist = min(v_min_dist, V_dist[x]);
ans = min(ans, v_min_dist + U_dist[x]);
for (auto &edge : G[x]) {
int y = edge.first;
// S -> x -> y -> T is a possible S-T shortest path
//cout << x << "->" << y << endl;
ans = min(ans, dfs(G, y, u_min_dist, v_min_dist));
}
return ans;
}
void dfs_2(int x, Dist &u_min_dist, Dist &U_dist) {
// Already tried
if (u_min_dist[x] != INF) return;
// DFS - traverse all possible shortest paths
// x : current node
// V ~~> some node visited before -> x ~~> U
u_min_dist[x] = U_dist[x];
for (auto &edge : G_ST[x]) {
// S -> x -> y -> T is a possible S-T shortest path
//cout << x << "->" << y << endl;
int y = edge.first;
dfs_2(y, u_min_dist, U_dist);
u_min_dist[x] = min(u_min_dist[x], u_min_dist[y]);
}
}
LL solve() {
Dist u_min_dist(N + 1, INF);
Dist v_min_dist(N + 1, INF);
dfs_2(S, u_min_dist, U_dist);
dfs_2(S, v_min_dist, V_dist);
LL ans = INF;
FOE(x, 1, N) {
ans = min(ans, u_min_dist[x] + V_dist[x]);
ans = min(ans, v_min_dist[x] + U_dist[x]);
}
return ans;
}
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});
}
S_dist = compute_shortest_path(G, {S}, N);
T_dist = compute_shortest_path(G, {T}, N);
U_dist = compute_shortest_path(G, {U}, N);
V_dist = compute_shortest_path(G, {V}, N);
//cout << src_dist[T] << " " << dst_dist[S] << endl;
ST_shortest_path_cost = S_dist[T];
assert (S_dist[T] == T_dist[S]);
// Case 1: Travel U -> some nodes along a shortest path -> V
G_ST.resize(N + 1);
FOE(x, 1, N) {
for (auto &edge : G[x]) {
int y, cost;
tie(y, cost) = edge;
if (S_dist[x] + cost + T_dist[y] == ST_shortest_path_cost) {
G_ST[x].PB(edge);
}
}
}
LL ans = solve();
//cout << ans << endl;
// Case 2: Travel U -> V via direct shortest path
ans = min(ans, U_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... |