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>
#define ll long long
#define ld long double
#define pb push_back
#define fi first
#define se second
#define en '\n'
#define sp ' '
#define tb '\t'
#define ri(n) int n; cin >> n
#define rl(n) ll n; cin >> n
#define rs(s) string s; cin >> s
#define rc(c) char c; cin >> c
#define rv(v) for (auto &x : v) cin >> x
#define pven(v) for (auto x : v) cout << x << en
#define pv(v) for (auto x : v) cout << x << sp; cout << en
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define yes cout << "YES" << en
#define no cout << "NO" << en
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define ssort(a, b) if (a < b) swap(a, b)
#define bitcnt(a) __builtin_popcountll(a)
#define bithigh(a) 63-__builtin_clzll(a)
#define lg bithigh
ll highpow(ll a) { return 1LL << (ll)lg(a); }
using namespace std;
const ll LINF = 4e18;
const int mxN = 2e5+10, INF = 2e9, mod = (1 ? 1e9+7 : 998244353);
ll n, m, S, T, U, V, dp[mxN], dist[2][mxN], indeg[mxN];
vector<array<ll, 2> > g[mxN];
vector<int> b[mxN], dag[mxN], topo;
void Dijkstra(int S, ll* dist = nullptr){
priority_queue<array<ll, 2>,
vector<array<ll, 2> >,
greater<array<ll, 2> > > pq;
bool vis[n]; memset(vis, 0, sizeof(vis));
if (!dist) dist = new ll[n];
for (int i = 0; i < n; i++){ dist[i] = LINF; b[i].clear(); }
dist[S] = 0;
pq.push({0, S});
while (pq.size()){
auto [d, s] = pq.top(); pq.pop();
if (vis[s] || d > dist[s]) continue;
vis[s] = 1;
for (auto [u, w] : g[s]){
if (d + w <= dist[u]){
dist[u] = d + w;
pq.push({d+w, u});
if (d + w < dist[u]) b[u].clear();
b[u].pb(s);
}
}
}
}
void dfs(int s){
indeg[s] = 0;
for (int u : b[s]){
dag[u].pb(s);
indeg[s]++;
if (!~indeg[u]) dfs(u);
}
}
ll DP(){
for (int s = 0; s < n; s++) dp[s] = LINF;
ll ans = LINF;
for (int s : topo){
dp[s] = dist[0][s];
for (int u : dag[s]) smin(dp[s], dp[u]);
smin(ans, dp[s] + dist[1][s]);
}
return ans;
}
void Solve(){
cin >> n >> m >> S >> T >> U >> V;
S--; T--; U--; V--;
for (int i = 0; i < m; i++){
ri(u); ri(v); rl(w);
u--; v--;
g[u].pb({v, w});
g[v].pb({u, w});
}
Dijkstra(U, dist[0]);
Dijkstra(V, dist[1]);
Dijkstra(S);
memset(indeg, -1, sizeof(indeg));
dfs(T);
queue<int> q;
for (int s = 0; s < n; s++)
if (!indeg[s])
q.push(s);
while (q.size()){
int s = q.front(); q.pop();
topo.pb(s);
for (int u : dag[s]){
indeg[u]--;
if (!indeg[u]) q.push(u);
}
}
reverse(all(topo));
ll ans = dist[0][V];
smin(ans, DP());
swap(dist[0], dist[1]);
smin(ans, DP());
cout << ans << en;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0); cerr.tie(0);
cout << setprecision(12) << fixed;
cerr << setprecision(12) << fixed;
cerr << "Started!" << endl;
int t = 1;
//cin >> t;
while (t--)
Solve();
return 0;
}
Compilation message (stderr)
commuter_pass.cpp: In function 'long long int highpow(long long int)':
commuter_pass.cpp:26:22: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
26 | #define bithigh(a) 63-__builtin_clzll(a)
| ^
commuter_pass.cpp:27:12: note: in expansion of macro 'bithigh'
27 | #define lg bithigh
| ^~~~~~~
commuter_pass.cpp:28:38: note: in expansion of macro 'lg'
28 | ll highpow(ll a) { return 1LL << (ll)lg(a); }
| ^~
# | 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... |