#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define pi pair<int,int>
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
template<class T> using upq = priority_queue<T, vector<T>, greater<T>>;
template<class T> int lwrbound(const vector<T>& a, const T& b, const int s = 0){return int(lower_bound(s + all(a), b) - a.begin());}
template<class T> int uprbound(const vector<T>& a, const T& b, const int s = 0){return int(upper_bound(s + all(a), b) - a.begin());}
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define ROF(i, a, b) for(int i = (a); i >= (b); i--)
#define sumof(x) accumulate(all(x), 0ll)
#define dbg(x) "[" << #x " = " << (x) << "]"
#define el "\n"
using ll = long long;
using ld = long double;
template<class T> bool ckmx(T& a, const T b){return (a < b ? a = b, true : false);}
template<class T> bool ckmn(T& a, const T b){return (a > b ? a = b, true : false);}
const int N = 2e5 + 5;
const int MOD = 1e9 + 7;
const ll INF = numeric_limits<ll>::max() / 20;
struct Edge{
int u, v; ll w;
Edge(int u = 0, int v = 0, ll w = 0): u(u), v(v), w(w) {}
};
void Main()
{
int n,m; cin >> n >> m;
int S,T,U,V; cin >> S >> T >> U >> V;
vector<Edge> E(m + 1); vector<vi> adj(n + 1, vi());
FOR(i, 1, m){
int u,v,w; cin >> u >> v >> w;
adj[u].eb(i);
adj[v].eb(i);
E[i] = Edge(u, v, w);
}
auto dijk = [&](vector<ll>& dist, int source) -> void{
FOR(i, 1, n){
dist[i] = (i == source ? 0 : INF);
}
priority_queue<pair<ll, int>> pq; pq.emplace(0, source);
while(sz(pq)){
pair<ll, int> eq = pq.top(); pq.pop();
int x = eq.se;
if(-eq.fi > dist[x]) continue;
for(int id : adj[x]){
int v = E[id].u ^ E[id].v ^ x; ll w = E[id].w;
if(ckmn(dist[v], dist[x] + w)) pq.emplace(-dist[v], v);
}
}
};
vector<vector<ll>> dist(4, vector<ll>(n + 1));
dijk(dist[0], S), dijk(dist[1], T);
vector<vi> dag(n + 1, vi()); ll mDist = dist[0][T];
FOR(i, 1, m){
int u = E[i].u, v = E[i].v; ll w = E[i].w;
if(dist[0][u] + w + dist[1][v] == mDist){
dag[u].eb(i);
}
if(dist[1][u] + w + dist[0][v] == mDist){
dag[v].eb(i);
}
}
vi topo;
{
vi inDeg(n + 1);
FOR(i, 1, n){
for(int id : dag[i]){
int v = E[id].u ^ E[id].v ^ i;
++inDeg[v];
}
}
queue<int> source; FOR(i, 1, n) if(!inDeg[i]) source.push(i);
while(sz(source)){
int x = source.front(); source.pop();
topo.eb(x);
for(int id : dag[x]){
int v = E[id].u ^ E[id].v ^ x;
--inDeg[v];
if(!inDeg[v]) source.push(v);
}
}
}
dijk(dist[2], U), dijk(dist[3], V);
vector<vector<ll>> dp(n + 1, vector<ll>(4, INF));
for(int x : topo){
ckmn(dp[x][0], dist[2][x]);
ckmn(dp[x][1], dist[3][x]);
for(int id : dag[x]){
int v = E[id].u ^ E[id].v ^ x;
ckmn(dp[v][0], dp[x][0]);
ckmn(dp[v][1], dp[x][1]);
}
}
reverse(all(topo));
for(int x : topo){
ckmn(dp[x][2], dist[2][x]);
ckmn(dp[x][3], dist[3][x]);
for(int id : dag[x]){
int v = E[id].u ^ E[id].v ^ x;
ckmn(dp[x][2], dp[v][2]);
ckmn(dp[x][3], dp[v][3]);
}
}
ll ans = INF;
FOR(x, 1, n){
ckmn(ans, dist[2][x] + dist[3][x]);
ckmn(ans, dp[x][0] + dp[x][3]);
ckmn(ans, dp[x][1] + dp[x][2]);
}
cout << ans << el;
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define name "InvMOD"
if(fopen(name".INP", "r")){
freopen(name".INP", "r", stdin);
freopen(name".OUT", "w", stdout);
}
int t = 1; while(t--) Main();
return 0;
}
Compilation message (stderr)
commuter_pass.cpp: In function 'int32_t main()':
commuter_pass.cpp:164:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
164 | freopen(name".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
commuter_pass.cpp:165:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
165 | freopen(name".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |