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;
typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for(ll i = (ll)a; i <= (ll)b; i++)
#define DEC(i, a, b) for(ll i = (ll)a; i >= (ll)b; i--)
typedef pair<ll, ll> pi;
typedef pair<pi, ll> pii;
#define f first
#define s second
#define pb push_back
#define all(v) v.begin(), v.end()
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
ll n, m, s, t, u, v, a, b, c, ans = 1e17;
ll dist[4][100005]; // s, t, u, v;
vector<pi> adj[100005];
vector<ll> dag[100005], revdag[100005];
vector<pii> edges;
void dijkstra(ll source, ll id) {
priority_queue<pi, vector<pi>, greater<pi> > pq;
dist[id][source] = 0; pq.push(pi(0LL, source));
while (!pq.empty()) {
auto [d, x] = pq.top(); pq.pop();
if (d != dist[id][x]) continue;
for (auto it:adj[x]) {
if (dist[id][it.f] == -1 or dist[id][it.f] > dist[id][x] + it.s) {
dist[id][it.f] = dist[id][x] + it.s;
pq.push(pi(dist[id][it.f], it.f));
}
}
}
}
ll dfs(ll x, ll p, bool d) {
ll best = dist[3][x];
for (auto it: (d ? dag[x] : revdag[x])) {
if (it == p) continue;
best = min(best, dfs(it, x, d));
}
ans = min(ans, dist[2][x] + best);
//cout << x << " " << best << "\n";
return best;
}
int main() {
fastio; cin >> n >> m >> s >> t >> u >> v;
FOR(i, 1, m) {
cin >> a >> b >> c;
adj[a].pb(pi(b, c));
adj[b].pb(pi(a, c));
edges.pb(pii(pi(a, b), c));
}
memset(dist, -1, sizeof dist);
dijkstra(s, 0); dijkstra(t, 1);
dijkstra(u, 2); dijkstra(v, 3);
for (auto it:edges) {
a = it.f.f, b = it.f.s, c = it.s;
if (dist[0][a] == -1 or dist[0][b] == -1 or dist[1][a] == -1 or dist[1][b] == -1) continue;
if (dist[1][a] + dist[0][b] + c == dist[0][t]) swap(a, b);
if (dist[0][a] + dist[1][b] + c == dist[0][t]) {
dag[a].pb(b);
revdag[b].pb(a);
}
}
dfs(s, -1, 1); dfs(t, -1, 0);
cout << min(ans, dist[3][u]);
}
# | 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... |