#include <bits/stdc++.h>
using namespace std;
#define int long long
using ll = long long;
const ll INF = 1e18;
struct Edge {
int u,v;
ll w;
};
int n,m;
int S,T,X,Y;
vector<pair<int,ll>> adj[100005];
vector<Edge> edges;
vector<ll> dijkstra(int src) {
vector<ll> 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 [du,u] = pq.top(); pq.pop();
if(du != dist[u]) continue;
for(auto [v,w] : adj[u]) {
if(dist[v] > du + w) {
dist[v] = du + w;
pq.push({dist[v], v});
}
}
}
return dist;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
cin >> S >> T >> X >> Y;
edges.resize(m);
for(int i=0;i<m;i++) {
int u,v; ll w;
cin >> u >> v >> w;
adj[u].push_back({v,w});
adj[v].push_back({u,w});
edges[i] = {u,v,w};
}
// Tính distS và distT
auto distS = dijkstra(S);
auto distT = dijkstra(T);
ll shortestST = distS[T];
// Xây đồ thị mới
vector<vector<pair<int,ll>>> g(n+1);
for(auto e : edges) {
int u=e.u, v=e.v; ll w=e.w;
ll nw = w; // mặc định
if(distS[u] + w + distT[v] == shortestST ||
distS[v] + w + distT[u] == shortestST) {
nw = 0; // cạnh thuộc 1 đường ngắn nhất S-T
}
g[u].push_back({v,nw});
g[v].push_back({u,nw});
}
// Dijkstra X->Y trên đồ thị mới
vector<ll> dist(n+1, INF);
priority_queue<pair<ll,int>, vector<pair<ll,int>>, greater<>> pq;
dist[X]=0;
pq.push({0,X});
while(!pq.empty()){
auto [du,u]=pq.top(); pq.pop();
if(du!=dist[u]) continue;
for(auto [v,w]: g[u]){
if(dist[v]>du+w){
dist[v]=du+w;
pq.push({dist[v],v});
}
}
}
cout << dist[Y];
}
# | 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... |