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>
using namespace std;
#define fi first
#define se second
#define ll long long
#define mp(x, y) make_pair(x, y)
#define sz(v) ((int) (v).size())
#define all(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define BIT(x, y) (((x) >> (y)) & 1)
#define pb push_back
#define heap priority_queue
#define max_rng *max_element
#define min_rng *min_element
#define rep(i, n) for(int i = 1, _n = (n); i <= _n; ++i)
#define forn(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i)
#define ford(i, a, b) for(int i = (a), _b = (b); i >= _b; --i)
template <class X, class Y>
inline bool maximize(X &x, Y y) {
return (x < y ? x = y, true : false);
}
template <class X, class Y>
inline bool minimize(X &x, Y y) {
return (x > y ? x = y, true : false);
}
template <class X>
inline void compress(vector<X> &a) {
sort(all(a));
a.resize(unique(all(a)) - a.begin());
}
int ctz(ll x) { return __builtin_ctzll(x); }
int lg(ll x) { return 63 - __builtin_clzll(x); }
int popcount(ll x) { return __builtin_popcount(x); }
const ll oo = (ll) 1e17;
const int INF = (int) 1e9 + 7, MOD = (int) 1e9 + 7;
const int N = 1e5 + 15;
int n, m;
int S, T, U, V;
vector<pair<int, int>> g[N];
ll fu[N], fv[N];
ll f[N], dp[N][2];
void dijkstra(int node, ll f[]) {
fill(f, f + 1 + n, oo);
heap<pair<ll, int>> pq;
pq.push(mp(0, node));
f[node] = 0;
while (sz(pq)){
pair<ll, int> top = pq.top();
pq.pop();
int u = top.second;
ll dist = -top.first;
if (f[u] != dist) continue;
for (pair<int, int> i : g[u]) {
ll nxt = dist + i.second;
if (minimize(f[i.first], nxt)) {
pq.push(mp(-nxt, i.first));
}
}
}
return;
}
int main(void) {
// think what before starting to code
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
cin >> S >> T >> U >> V;
rep(i, m) {
int u, v, w;
cin >> u >> v >> w;
g[u].pb(mp(v, w));
g[v].pb(mp(u, w));
}
dijkstra(U, fu);
dijkstra(V, fv);
ll ans = fu[V];
memset(f, 0x3f, sizeof f);
memset(dp, 0x3f, sizeof dp);
// (dist, u)
heap<array<ll, 2>> pq;
pq.push({0, S});
f[S] = 0;
dp[S][0] = fu[S]; dp[S][1] = fv[S];
while (sz(pq)) {
array<ll, 2> top = pq.top();
pq.pop();
int u = top[1];
ll dist = -top[0];
if (f[u] != dist) continue;
// minimize(ans, dp[u][0] + fv[u]);
// minimize(ans, dp[u][1] + fu[u]);
// cout << u << ' ' << dist << ' ' << ans << ' ' << dp[u][0] << ' ' << dp[u][1] << '\n';
for (pair<int, int> i : g[u]) {
int v = i.first;
ll new_dist = dist + i.second;
if (minimize(f[v], new_dist)) {
dp[v][0] = min(dp[u][0], fu[v]);
dp[v][1] = min(dp[u][1], fv[v]);
pq.push({-new_dist, v});
}
else if (f[v] == new_dist) {
if (min(dp[u][0], fu[v]) + min(dp[u][1], fv[v]) < dp[v][0] + dp[v][1]) {
dp[v][0] = min(dp[u][0], fu[v]);
dp[v][1] = min(dp[u][1], fv[v]);
}
}
}
}
cout << min(ans, dp[T][0] + dp[T][1]);
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... |