#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18;
const int N = 1e5 + 5;
vector<pair<int, ll>> adj[N];
int n, m, a, b, c, d;
vector<pair<int, int>> edges;
map<pair<int, int>, int> edge_id;
ll w[N * 2];
bool is_vip[N * 2];
vector<pair<int, int>> dag[N]; // shortest path DAG
ll distA[N];
void dijkstra(int src, ll* cost, ll* dist) {
fill(dist, dist + n + 1, INF);
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> pq;
dist[src] = 0;
pq.push({0, src});
while (!pq.empty()) {
auto [d, u] = pq.top(); pq.pop();
if (d > dist[u]) continue;
for (auto [v, eid] : adj[u]) {
ll c = cost[eid];
if (dist[v] > dist[u] + c) {
dist[v] = dist[u] + c;
pq.push({dist[v], v});
}
}
}
}
void build_shortest_path_dag(int a, int b) {
// Build shortest path DAG from A to B
for (int u = 1; u <= n; u++) {
for (auto [v, eid] : adj[u]) {
if (distA[u] + w[eid] == distA[v]) {
dag[u].push_back({v, eid});
}
}
}
}
void mark_vip_edges(int a, int b) {
// Mark all edges in any shortest path from A to B as VIP using BFS/DFS
vector<bool> visited(n + 1, false);
queue<int> q;
q.push(a);
visited[a] = true;
while (!q.empty()) {
int u = q.front(); q.pop();
for (auto [v, eid] : dag[u]) {
is_vip[eid] = true;
if (!visited[v]) {
visited[v] = true;
q.push(v);
}
}
}
}
vector<int> dijkstra_path(int src, int dst, ll* cost) {
vector<ll> dist(n + 1, INF);
vector<int> prev(n + 1, -1);
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> pq;
dist[src] = 0;
pq.push({0, src});
while (!pq.empty()) {
auto [d, u] = pq.top(); pq.pop();
if (d > dist[u]) continue;
for (auto [v, eid] : adj[u]) {
ll c = cost[eid];
if (dist[v] > dist[u] + c) {
dist[v] = dist[u] + c;
prev[v] = u;
pq.push({dist[v], v});
}
}
}
vector<int> path;
int cur = dst;
while (cur != -1) {
path.push_back(cur);
cur = prev[cur];
}
reverse(path.begin(), path.end());
return path;
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> m >> a >> b >> c >> d;
for (int i = 1; i <= m; i++) {
int u, v; ll ww;
cin >> u >> v >> ww;
adj[u].push_back({v, i});
adj[v].push_back({u, i});
edges.push_back({u, v});
edge_id[{u, v}] = i;
edge_id[{v, u}] = i;
w[i] = ww;
}
ll cost1[N * 2];
for (int i = 1; i <= m; i++) cost1[i] = w[i];
dijkstra(a, cost1, distA);
build_shortest_path_dag(a, b);
mark_vip_edges(a, b);
ll cost2[N * 2];
for (int i = 1; i <= m; i++) cost2[i] = is_vip[i] ? 0 : w[i];
ll distC[N];
dijkstra(c, cost2, distC);
cout << distC[d] << 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... |