#include <bits/stdc++.h>
using namespace std;
#define int long long
#define str string
#define endl '\n'
#define fs first
#define ss second
#define all(a) a.begin(), a.end()
#define print(a) for(auto x : a) cout << x << ' '; cout << endl;
#define printmp(a) for(auto x : a) cout << x[0] << ' ' << x[1] << endl;
const int mod = 1e9 + 7;
void solve(){
int n, m, s, t, u, v;
cin >> n >> m >> s >> t >> u >> v;
vector<vector<array<int, 2>>> graph(n + 1);
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});
}
vector<int> d1(n + 1, 1e18), d2(n + 1, 1e18);
set<array<int, 2>> st;
st.insert({0, u});
d1[u] = 0;
while(!st.empty()){
array<int, 2> x = *st.begin();
st.erase(st.begin());
for(auto y : graph[x[1]]){
if(d1[y[0]] > x[0] + y[1]){
st.erase({d1[y[0]], y[0]});
d1[y[0]] = x[0] + y[1];
st.insert({d1[y[0]], y[0]});
}
}
}
d2[v] = 0;
st.insert({0, v});
while(!st.empty()){
array<int, 2> x = *st.begin();
st.erase(st.begin());
for(auto y : graph[x[1]]){
if(d2[y[0]] > x[0] + y[1]){
st.erase({d2[y[0]], y[0]});
d2[y[0]] = x[0] + y[1];
st.insert({d2[y[0]], y[0]});
}
}
}
vector<int> mn(n + 1, 1e18), s1(n + 1), s2(n + 1);
set<array<int, 3>> stt;
stt.insert({0ll, d1[s] + d2[s], s});
mn[s] = 0;
s1[s] = d1[s];
s2[s] = d2[s];
while(!stt.empty()){
array<int, 3> x = *stt.begin();
stt.erase(stt.begin());
for(auto y : graph[x[2]]){
if(mn[y[0]] > x[0] + y[1]){
stt.erase({mn[y[0]], s1[y[0]] + s2[y[0]], y[0]});
s1[y[0]] = min(d1[y[0]], s1[x[2]]);
s2[y[0]] = min(d2[y[0]], s2[x[2]]);
mn[y[0]] = x[0] + y[1];
stt.insert({mn[y[0]], s1[y[0]] + s2[y[0]], y[0]});
}
else if(mn[y[0]] == x[0] + y[1]){
if(s1[y[0]] + s2[y[0]] > min(d2[y[0]], s2[x[2]]) + min(d1[y[0]], s1[x[2]])){
stt.erase({mn[y[0]], s1[y[0]] + s2[y[0]], y[0]});
s1[y[0]] = min(d1[y[0]], s1[x[2]]);
s2[y[0]] = min(d2[y[0]], s2[x[2]]);
stt.insert({mn[y[0]], s1[y[0]] + s2[y[0]], y[0]});
}
}
}
}
cout << min(d1[v], s1[t] + s2[t]);
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int t = 1;
// cin >> t;
while(t --){
solve();
cout << endl;
}
}
# | 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... |