#include "bits/stdc++.h"
using namespace std;
#define int long long
const int INF = 1e18;
int n, m;
int s, t, u, v;
vector<pair<int, int>> graph[100005];
vector<vector<int>> shortest_path_edges;
// Find shortest path from src to all nodes
vector<int> dijkstra(int src) {
vector<int> dist(n + 1, INF);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
dist[src] = 0;
pq.push({0, src});
while (!pq.empty()) {
int d = pq.top().first;
int node = pq.top().second;
pq.pop();
if (d > dist[node]) continue;
for (auto edge : graph[node]) {
int next = edge.first;
int weight = edge.second;
if (dist[node] + weight < dist[next]) {
dist[next] = dist[node] + weight;
pq.push({dist[next], next});
}
}
}
return dist;
}
// Mark all edges that could be part of any shortest path from S to T
void markShortestPathEdges(vector<int>& distFromS, vector<int>& distToT) {
shortest_path_edges.resize(n + 1, vector<int>(n + 1, 0));
for (int i = 1; i <= n; i++) {
for (auto edge : graph[i]) {
int j = edge.first;
int weight = edge.second;
// If this edge is part of any shortest path from S to T
if (distFromS[i] + weight + distToT[j] == distFromS[t] ||
distFromS[j] + weight + distToT[i] == distFromS[t]) {
shortest_path_edges[i][j] = 1;
shortest_path_edges[j][i] = 1;
}
}
}
}
// Find minimum cost from U to V with commuter pass
int findMinCostUtoV(vector<int>& distFromS, vector<int>& distToT) {
// Build a new graph where edges in shortest path are free
vector<pair<int, int>> new_graph[100005];
for (int i = 1; i <= n; i++) {
for (auto edge : graph[i]) {
int j = edge.first;
int weight = edge.second;
if (shortest_path_edges[i][j]) {
// Edge is part of shortest path, so it's free
new_graph[i].push_back({j, 0});
} else {
// Edge is not part of shortest path
new_graph[i].push_back({j, weight});
}
}
}
// Run Dijkstra from U to find minimum cost to V
vector<int> dist(n + 1, INF);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
dist[u] = 0;
pq.push({0, u});
while (!pq.empty()) {
int d = pq.top().first;
int node = pq.top().second;
pq.pop();
if (d > dist[node]) continue;
for (auto edge : new_graph[node]) {
int next = edge.first;
int weight = edge.second;
if (dist[node] + weight < dist[next]) {
dist[next] = dist[node] + weight;
pq.push({dist[next], next});
}
}
}
return dist[v];
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
cin >> s >> t >> u >> v;
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
graph[a].push_back({b, c});
graph[b].push_back({a, c});
}
// Find shortest paths
vector<int> distFromS = dijkstra(s);
vector<int> distToT(n + 1, INF);
// Calculate distance to T by running Dijkstra from T
vector<int> distFromT = dijkstra(t);
for (int i = 1; i <= n; i++) {
distToT[i] = distFromT[i];
}
// Mark all edges that can be part of any shortest path from S to T
markShortestPathEdges(distFromS, distToT);
// Find minimum cost from U to V
int result = findMinCostUtoV(distFromS, distToT);
cout << result << 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... |