This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// PhuThuyRuntime <3
// A secret makes a woman woman
#include <bits/stdc++.h>
using namespace std;
#define eb emplace_back
#define ef emplace_front
#define pb push_back
#define pf push_front
#define all(v) v.begin(), v.end()
#define ins insert
#define lb lower_bound
#define ub upper_bound
#define fo(i, l, r) for(int i = l; i <= r; i++)
#define foi(i, l, r) for(int i = l; i >= r; i--)
#define elif else if
#define el cout << "\n";
#define pii pair<int, int>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define pil pair<int, ll>
#define fi first
#define se second
#define in(x) freopen(x, "r", stdin)
#define out(x) freopen(x, "w", stdout)
#define ll long long
#define ull unsigned long long
#define pob pop_back
#define bs binary_search
#define vi vector<int>
#define vii vector<pair<int, int>>
#define getbit(i, j) (i >> j) & 1
#define offbit(i, j) (1 << j) ^ i
#define onbit(i, j) (1 << j) | i
const int N = 1e5 + 2;
const ll mod = 1e9 + 7;
const int inf = INT_MAX;
const int base = 31;
const long double EPS = 1e-9;
const long double pi = acos(-1.0);
int n, m, S, T, U, V;
vii adj[N];
vector<pair<pii, int>> edges;
void inp(){
cin >> n >> m >> S >> T >> U >> V;
fo(i, 1, m){
int u, v, c; cin >> u >> v >> c;
edges.push_back({{u, v}, c});
adj[u].eb(v, c);
adj[v].eb(u, c);
}
}
ll dp[N], dpU[N], dpV[N];
void dij(int start, ll DP[]){
fo(i, 1, n) DP[i] = 1e18;
priority_queue<pli, vector<pli>, greater<pli>> q;
DP[start] = 0;
q.push({0, start});
while(!q.empty()){
ll val = q.top().fi;
int u = q.top().se;
q.pop();
if(val != DP[u]) continue;
for(auto [v, w] : adj[u]){
if(DP[v] > DP[u] + w){
DP[v] = DP[u] + w;
q.push({DP[v], v});
}
}
}
}
ll tot = 1e18;
int exist[N];
void predfs(int u){
if(exist[u] != -1) return;
bool ok = (u == T);
for(auto [v, w] : adj[u]){
predfs(v);
ok = ok || exist[v];
}
exist[u] = ok;
}
ll ans[N];
void dfs(int u, ll dp1[], ll dp2[]){
if(ans[u] != -1) return;
ans[u] = 1e18;
if(exist[u]) ans[u] = dp2[u];
for(auto [v, w] : adj[u]){
dfs(v, dp1, dp2);
ans[u] = min(ans[u], ans[v]);
}
tot = min(tot, dp1[u] + ans[u]);
}
void sol(){
dij(S, dp);
// fo(i, 1, n) cout << dp[i] << ' ';
// el
dij(U, dpU);
dij(V, dpV);
tot = dpU[V];
fo(i, 1, n) adj[i].clear();
for(auto [it, w] : edges){
auto [u, v] = it;
if(dp[u] == dp[v] + w){
adj[v].eb(u, w);
// cout << v << ' ' << u, el
}
if(dp[v] == dp[u] + w){
adj[u].eb(v, w);
// cout << u << ' ' << v, el
}
}
memset(exist, -1, sizeof(exist));
predfs(S);
memset(ans, -1, sizeof(ans));
dfs(S, dpU, dpV);
memset(ans, -1, sizeof(ans));
dfs(S, dpV, dpU);
cout << tot;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
inp();
sol();
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... |