// #include <dbg.h>
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
using ull = unsigned long long;
template <typename T> using vstack = stack<T, vector<T>>;
namespace {
struct Edge {
uint b;
ull c;
};
inline void djikstra(vector<ull> &dst, uint u, const auto &edges) {
vstack<pair<ull, uint>> st;
dst[u] = 0;
st.push({0, u});
while (!st.empty()) {
auto [d, v] = st.top();
st.pop();
for (Edge edge : edges[v]) {
if (dst[edge.b] > d + edge.c) {
dst[edge.b] = d + edge.c;
st.push({dst[edge.b], edge.b});
}
}
}
}
} // namespace
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
uint n, m;
cin >> n >> m;
uint s, t;
cin >> s >> t;
s--;
t--;
uint u, v;
cin >> u >> v;
u--;
v--;
vector<vector<Edge>> edges(n);
for (uint i = 0; i < m; i++) {
uint a, b;
ull c;
cin >> a >> b >> c;
a--;
b--;
edges[a].push_back({b, c});
edges[b].push_back({a, c});
}
vector<bool> special(n, false);
vector<uint> order;
order.reserve(n);
// Djikstra
vector<vector<uint>> prev(n);
vector<ull> dst(n, -1ull);
vstack<pair<ull, uint>> st;
dst[s] = 0;
st.push({0, s});
while (!st.empty()) {
auto [d, v] = st.top();
st.pop();
order.push_back(v);
for (Edge edge : edges[v]) {
if (dst[edge.b] > d + edge.c) {
dst[edge.b] = d + edge.c;
st.push({dst[edge.b], edge.b});
prev[edge.b].assign(1, v);
} else if (dst[edge.b] == d + edge.c) {
prev[edge.b].push_back(v);
}
}
}
vstack<uint> to_v;
to_v.push(t);
while (!to_v.empty()) {
uint v = to_v.top();
to_v.pop();
special[v] = true;
for (uint p : prev[v]) {
to_v.push(p);
}
}
vector<ull> dst_u(n, -1ull);
djikstra(dst_u, u, edges);
vector<ull> dst_v(n, -1ull);
djikstra(dst_v, v, edges);
ull best_dst = -1ull;
vector<ull> disc_dst;
for (bool do_s : {false, true}) {
if (do_s) {
swap(dst_u, dst_v);
swap(u, v);
}
disc_dst.assign(n, -1ull);
for (uint v : order) {
if (!special[v])
continue;
disc_dst[v] = dst_u[v];
for (uint p : prev[v]) {
disc_dst[v] = min(disc_dst[v], disc_dst[p]);
}
best_dst = min(best_dst, disc_dst[v] + dst_v[v]);
}
}
cout << best_dst << '\n';
}
| # | 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... |