#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define FOD(i, a, b) for(int i = a; i >= b; i--)
#define int long long
#define fi first
#define se second
#define ll long long
#define db double
#define ii pair<int,int>
#define pb push_back
#define MASK(i) (1LL << i)
#define sq(i) (1LL * (i) * (i))
#define task "khoile08"
const ll INF = 1e18;
const int inf = 1e9;
const int N = 1e5 + 5;
int n, m, S, T, U, V, ans;
vector<ii> g[N];
ll d[4][N], dp[N];
void Dijkstra(int sr, int t) {
priority_queue<ii,vector<ii>,greater<ii>> pq;
pq.push({0, sr});
FOR(i, 1, n) d[t][i] = INF;
d[t][sr] = 0;
while(!pq.empty()) {
ll cost = pq.top().fi;
int u = pq.top().se;
pq.pop();
if(cost != d[t][u]) continue;
for(ii H : g[u]) {
int v = H.fi;
int w = H.se;
if(d[t][v] > d[t][u] + w) {
d[t][v] = d[t][u] + w;
pq.push({d[t][v], v});
}
}
}
}
void Dfs(int u, int t) {
dp[u] = d[0][u];
for(auto H : g[u]) {
int v = H.fi;
int w = H.se;
if(d[t][u] == d[t][v] + w) { // on sp
Dfs(v, t);
dp[u] = min(dp[u], dp[v]);
}
}
ans = min(ans, dp[u] + d[1][u]);
}
void Solve() {
Dijkstra(U, 0);
Dijkstra(V, 1);
Dijkstra(S, 2);
Dijkstra(T, 3);
ans = INF;
FOR(i, 1, n) dp[i] = INF;
Dfs(T, 2);
FOR(i, 1, n) dp[i] = INF;
Dfs(S, 3);
cout << min(d[0][V], ans) << '\n';
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int tc = 1;
// cin >> tc;
while(tc--) {
cin >> n >> m >> S >> T >> U >> V;
FOR(i, 1, m) {
int u, v, w;
cin >> u >> v >> w;
g[u].pb({v, w});
g[v].pb({u, w});
}
Solve();
}
}
# | 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... |