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 <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <deque>
#include <set>
#include <utility>
#include <array>
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,tune=native")
using namespace std;
#define ALL(x) x.begin(), x.end()
#define ShinLena cin.tie(nullptr)->sync_with_stdio(false);
using ll = long long;
#define N 200005
int n, m, s, t, cs, ct, head[N], e, se, shead[N];
struct edge
{
int w, v, next;
} g[N<<1], sg[N<<1];
ll dp_s[N], dp_t[N], dp_cs[N], dp_ct[N], ans = 1e18;
void add_edge(int u, int v, int w, struct edge *g, int *e, int *h)
{
int p = (*e)++;
g[p] = {w, v, h[u]}; h[u] = p;
}
void add_biedge(int u, int v, int w, struct edge *g, int *e, int *h)
{
add_edge(u, v, w, g, e, h), add_edge(v, u, w, g, e, h);
}
void dijkstra(ll *d, int s)
{
set<pair<ll, int>> h;
for (h.emplace(d[s] = 0, s); h.size(); )
{
auto [c, u] = *h.begin(); h.erase(h.begin());
for (auto i = head[u]; i != -1; i = g[i].next)
{
auto [w, v, _] = g[i];
if (c + w < d[v]) h.erase({d[v], v}), h.emplace(d[v] = c + w, v);
}
}
}
void init()
{
memset(dp_s, 63, sizeof dp_s);
memset(dp_t, 63, sizeof dp_t);
memset(dp_cs, 63, sizeof dp_cs);
memset(dp_ct, 63, sizeof dp_ct);
memset(head, -1, sizeof head);
memset(shead, -1, sizeof shead);
ShinLena;
cin >> n >> m >> s >> t >> cs >> ct;
for (int u, v, w, i = 0; i < m; ++i) cin >> u >> v >> w, add_biedge(u, v, w, g, &e, head);
}
void build_shortest_path_graph()
{
dijkstra(dp_s, s);
dijkstra(dp_t, t);
for (int u = 1; u <= n; ++u)
{
for (int j = head[u]; j != -1; j = g[j].next)
{
auto [w, v, _] = g[j];
if (dp_s[u] + dp_t[v] + w == dp_s[t] || w + dp_s[v] + dp_t[u] == dp_s[t])
add_biedge(u, v, w, sg, &se, shead);
}
}
}
int low[N], tin[N], timer = 1, bridge[N<<1][2], bs;
void dfs(int u, int p)
{
low[u] = tin[u] = timer++;
for (auto j = shead[u]; j != -1; j = sg[j].next)
{
auto [w, v, _] = sg[j];
if (v == p) continue;
if (tin[v])
{
low[u] = min(low[u], tin[v]);
}
else
{
dfs(v, u);
low[u] = min(low[u], low[v]);
if (low[v] > tin[u])
bridge[bs][0] = u, bridge[bs][1] = v, ++bs;
}
}
}
void find_path()
{
dijkstra(dp_cs, cs);
dijkstra(dp_ct, ct);
ll d1 = 1e18, d2 = 1e18;
for (int i = 0; i < bs; ++i)
{
auto u = bridge[i][0], v = bridge[i][1];
d1 = min({d1, dp_cs[u], dp_cs[v]});
d2 = min({d2, dp_ct[u], dp_ct[v]});
}
ans = min(dp_cs[ct], d1 + d2);
}
int main()
{
init();
build_shortest_path_graph();
dfs(s, s);
find_path();
cout << ans;
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... |