This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define endl '\n'
#define fi first
#define se second
#define For(i, l, r) for (int i = l; i < r; i++)
#define ForE(i, l, r) for (int i = l; i <= r; i++)
#define FordE(i, l, r) for (int i = l; i >= r; i--)
#define Fora(v, a) for (auto v: a)
#define bend(a) a.begin(), a.end()
#define isz(a) ((signed)a.size())
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef vector <int> vi;
typedef vector <pii> vpii;
typedef vector <vi> vvi;
template <class T>
using min_heap = priority_queue <T, vector <T>, greater <T>>;
const int N = 1e5 + 5;
const ll infll = (ll)1e18 + 7;
int n, m;
int s1, t1, s2, t2;
vpii adj[N];
ll dist[4][N];
ll D;
void bfs(int s, ll* dist){
fill(dist + 1, dist + n + 1, infll);
min_heap <pair <ll, int>> pq;
dist[s] = 0; pq.emplace(0, s);
while (!pq.empty()){
ll d; int u; tie(d, u) = pq.top(); pq.pop();
if (d != dist[u]){
continue;
}
Fora(&edge, adj[u]){
int v, w; tie(v, w) = edge;
if (dist[v] > d + w){
dist[v] = d + w;
pq.emplace(dist[v], v);
}
}
}
}
bool check_edge(int u, int v, int w){
return dist[0][u] + w + dist[1][v] == D or dist[0][v] + w + dist[1][u] == D;
}
int a[N];
ll ans = infll;
ll dp[N];
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// freopen("KEK.inp", "r", stdin);
// freopen("KEK.out", "w", stdout);
cin >> n >> m;
cin >> s1 >> t1 >> s2 >> t2;
ForE(i, 1, m){
int u, v, w; cin >> u >> v >> w;
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
}
bfs(s1, dist[0]);
bfs(t1, dist[1]);
bfs(s2, dist[2]);
bfs(t2, dist[3]);
D = dist[0][t1];
iota(a + 1, a + n + 1, 1);
sort(a + 1, a + n + 1, [&](int u, int v){
return dist[0][u] < dist[0][v];
});
fill(dp + 1, dp + n + 1, infll);
ForE(i, 1, n){
int u = a[i];
Fora(&edge, adj[u]){
int v, w; tie(v, w) = edge;
if (check_edge(u, v, w)){
dp[u] = min(dp[u], dp[v]);
}
}
dp[u] = min(dp[u], dist[2][u]);
ans = min(ans, dp[u] + dist[3][u]);
}
fill(dp + 1, dp + n + 1, infll);
ForE(i, 1, n){
int u = a[i];
Fora(&edge, adj[u]){
int v, w; tie(v, w) = edge;
if (check_edge(u, v, w)){
dp[u] = min(dp[u], dp[v]);
}
}
dp[u] = min(dp[u], dist[3][u]);
ans = min(ans, dp[u] + dist[2][u]);
}
cout << ans << endl;
}
/*
==================================================+
INPUT: |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
OUTPUT: |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
*/
# | 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... |