#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<ll, ll>
const int nx = 1e5+5;
const ll INF = 1e18;
int n, m, s, t, U, V;
vector<int> pa(nx, -1);
vector<pii> adj[nx];
priority_queue<pii, vector<pii>, greater<pii>> pq;
vector<ll> findpass(int src)
{
vector<ll> dist(nx, INF);
dist[src] = 0;
pq.push({0, src});
while(!pq.empty())
{
auto [cw, u] = pq.top();
pq.pop();
if(cw > dist[u]) continue;
for(auto [v, nw] : adj[u])
{
if(dist[v] > dist[u] + nw)
{
pa[v] = u;
dist[v] = dist[u] + nw;
pq.push({dist[v], v});
}
}
}
vector<ll> pass;
int idx = t;
while(idx != -1)
{
int v = pa[idx];
pass.push_back(idx);
idx = v;
}
return pass;
}
ll dijkstra(int src, vector<ll> pass)
{
vector<ll> dist(nx, INF);
dist[src] = 0;
pq.push({0, src});
while(!pq.empty())
{
auto [cw, u] = pq.top();
pq.pop();
if(cw > dist[u]) continue;
for(auto [v, nw] : adj[u])
{
if(dist[v] > dist[u] + nw)
{
dist[v] = dist[u] + nw;
pq.push({dist[v], v});
}
}
}
ll mn = INF;
for(auto free : pass) mn = min(mn, dist[free]);
return mn;
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin >> n >> m >> s >> t >> 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});
}
vector<ll> pass = findpass(s);
ll distu = dijkstra(U, pass);
ll distv = dijkstra(V, pass);
ll UtoV = dijkstra(U, {V});
// cout << distu << ' ' << distv << ' ' << UtoV << '\n';
cout << min(distu + distv, UtoV);
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... |