This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("Ofast,unroll-loops,fast-math,O3")
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <fstream>
#include <unordered_map>
#define ll long long
#define int long long
#define all(v) v.begin(), v.end()
#define nl '\n'
#define pb push_back
#define sz(s) (int)(s).size()
#define f first
#define s second
using namespace std;
const ll N = 1e5 + 50, MX = 1e18;
bool was[N];
void solve(){
ll n,m;
cin >> n >> m;
vector <pair <int, int> > g[n + 1];
ll s, t, u, v;
cin >> s >> t >> u >> v;
for(int i = 1; i <= n; i++){
ll a, b, x;
cin >> a >> b >> x;
g[a].pb({b,x});
g[b].pb({a, x});
}
if(n <= 300) {
ll ds[n + 1], dt[n + 1], du[n + 1], dv[n + 1];
if (true) {
for (int i = 1; i <= n; i++) {
was[i] = 0;
ds[i] = dt[i] = MX;
}
ds[s] = 0;
priority_queue<pair<int, int> > q;
q.push({0, s});
while (!q.empty()) {
ll v = q.top().s;
q.pop();
if (was[v])continue;
was[v] = 1;
for (auto [to, w]: g[v]) {
if (ds[v] + w < ds[to]) {
ds[to] = ds[v] + w;
q.push({-ds[to], to});
}
}
}
}
if (true) {
for (int i = 1; i <= n; i++) {
was[i] = 0;
du[i] = MX;
}
du[u] = 0;
priority_queue<pair<int, int>> q;
q.push({0, u});
while (!q.empty()) {
ll v = q.top().s;
q.pop();
if (was[v])continue;
was[v] = 1;
for (auto [to, w]: g[v]) {
if (du[v] + w < du[to]) {
du[to] = du[v] + w;
q.push({-du[to], to});
}
}
}
//cout << du[v] << nl;
}
if (true) {
for (int i = 1; i <= n; i++) {
was[i] = 0;
dv[i] = MX;
}
dv[v] = 0;
priority_queue<pair<int, int>> q;
q.push({0, v});
while (!q.empty()) {
ll v = q.top().s;
q.pop();
if (was[v])continue;
was[v] = 1;
for (auto [to, w]: g[v]) {
if (dv[v] + w < dv[to]) {
dv[to] = dv[v] + w;
q.push({-dv[to], to});
}
}
}
}
if (true) {
for (int i = 1; i <= n; i++) {
was[i] = 0;
dt[i] = MX;
}
ds[s] = 0;
dt[t] = 0;
priority_queue<pair<int, int>> q;
q.push({0, t});
while (!q.empty()) {
ll v = q.top().s;
q.pop();
if (was[v])continue;
was[v] = 1;
for (auto [to, w]: g[v]) {
if (dt[v] + w < dt[to]) {
dt[to] = dt[v] + w;
q.push({-dt[to], to});
}
}
}
}
ll ans = du[v];
//cout << ds[t] << nl;
//cout << ds[3] << " " << dt[5] << nl;
for (int i = 1; i <= n; i++) {
ll di[n + 1];
for (int j = 1; j <= n; j++) {
was[j] = 0;
di[j] = MX;
}
di[i] = 0;
priority_queue<pair<int, int>> q;
q.push({0, i});
while (!q.empty()) {
ll v = q.top().s;
q.pop();
if (was[v])continue;
was[v] = 1;
for (auto [to, w]: g[v]) {
if (di[v] + w < di[to]) {
di[to] = di[v] + w;
q.push({-di[to], to});
}
}
}
for (int j = 1; j <= n; j++) {
if (ds[i] + dt[j] + di[j] == ds[t] || ds[j] + dt[i] + di[j] == ds[t]) {
ans = min(ans, du[i] + dv[j]);
}
}
}
cout << ans << nl;
}
}
signed main(){
//freopen("lca.in", "r", stdin);
//freopen("lca.out", "w", stdout);
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
ll ql=1;
//cin >> ql;
//tst++;
while(ql--){
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... |