#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pii;
typedef pair<int, pii> pip;
typedef pair<pii, int> ppi;
typedef pair<pii, pii> ppp;
const int LLINF = 2e18 + 10;
const int MAXN = 2e5 + 10;
int n, house, school;
vector<pii> adj[MAXN];
int dist_ini[2][MAXN];
bool mrk_ini[2][MAXN];
int dist[4][MAXN];
bool mrk[4][MAXN];
void dijkstra1(int t, int source) {
for (int i = 1; i <= n; i++) {
dist_ini[t][i] = LLINF;
}
dist_ini[t][source] = 0;
priority_queue<pii> pq;
pq.push({0, source});
while (!pq.empty()) {
auto [d, v] = pq.top();
pq.pop();
if (mrk_ini[t][v]) continue;
mrk_ini[t][v] = true;
for (auto [w, c]: adj[v]) {
if (mrk_ini[t][w]) continue;
if (dist_ini[t][w] > dist_ini[t][v] + c) {
dist_ini[t][w] = dist_ini[t][v] + c;
pq.push({-dist_ini[t][w], w});
}
}
}
}
bool node_in_path(int v) {
return dist_ini[0][v] + dist_ini[1][v] == dist_ini[0][school];
}
bool edge_in_path(int t, int v, int w, int c) {
return dist_ini[t][v] + c == dist_ini[t][w];
}
void relax(priority_queue<pip> &pq, int tv, int v, int tw, int w, int c) {
if (dist[tw][w] > dist[tv][v] + c) {
dist[tw][w] = dist[tv][v] + c;
pq.push({-dist[tw][w], {tw, w}});
}
}
void dijkstra2(int source) {
for (int i = 1; i <= n; i++) {
dist[0][i] = dist[1][i] = dist[2][i] = dist[3][i] = LLINF;
}
dist[0][source] = 0;
priority_queue<pip> pq;
pq.push({0, {0, source}});
while (!pq.empty()) {
auto [d, p] = pq.top();
auto [t, v] = p;
pq.pop();
if (mrk[t][v]) continue;
mrk[t][v] = true;
for (auto [w, c]: adj[v]) {
bool both_in_path = node_in_path(v) && node_in_path(w);
if (t == 0) {
relax(pq, 0, v, 0, w, c);
if (both_in_path && edge_in_path(0, v, w, c)) {
relax(pq, 0, v, 1, w, 0);
}
if (both_in_path && edge_in_path(1, v, w, c)) {
relax(pq, 0, v, 2, w, 0);
}
} else if (t == 1) {
if (both_in_path && edge_in_path(0, v, w, c)) {
relax(pq, 1, v, 1, w, 0);
}
relax(pq, 1, v, 3, w, c);
} else if (t == 2) {
if (both_in_path && edge_in_path(1, v, w, c)) {
relax(pq, 2, v, 2, w, 0);
}
relax(pq, 2, v, 3, w, c);
} else {
relax(pq, 3, v, 3, w, c);
}
}
}
}
void solve() {
int m, u, v;
cin >> n >> m >> house >> school >> u >> v;
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
adj[a].push_back({b, c});
adj[b].push_back({a, c});
}
dijkstra1(0, house);
dijkstra1(1, school);
dijkstra2(u);
cout << min({ dist[0][v], dist[1][v], dist[2][v], dist[3][v] }) << endl;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
solve();
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... |