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 = 1e16;
ll dist[4][100005]; // s, t, u, v;
vector<pi> adj[100005];
vector<ll> dag[100005];
vector<pii> edges;
bool vis[100005];
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));
}
}
}
}
pi dfs(ll x) {
vis[x] = 1;
ll ex = dist[3][x]; if (ex == -1) ex = 1e16;
ll en = dist[2][x]; if (en == -1) en = 1e16;
for (auto it:dag[x]) {
if (vis[it]) continue;
pi res = dfs(it);
ex = min(ex, res.f);
en = min(en, res.s);
}
if (dist[2][x] != -1 and ex != -1) ans = min(ans, dist[2][x] + ex);
if (dist[3][x] != -1 and en != -1) ans = min(ans, dist[3][x] + en);
return pi(ex, en);
}
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[1][a] != -1 and dist[0][b] != -1 and dist[1][a] + dist[0][b] + c == dist[0][t]) dag[b].pb(a);
else if (dist[0][a] != -1 and dist[1][b] != -1 and dist[0][a] + dist[1][b] + c == dist[0][t]) dag[a].pb(b);
}
dfs(s); 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... |