이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const ll INF = 1e18, MOD = 1e9 + 7, P = 239;
const ld PI = 3.1415926535897938462643383279502, EPS = 1e-7;
template <typename T>
inline ll sz(const T &a) {
return a.size();
}
template <typename T, typename C>
istream &operator >> (istream &in, pair<T, C> &a) {
return in >> a.first >> a.second;
}
template <typename T, typename C>
ostream &operator << (ostream &out, pair<T, C> a) {
return out << a.first << ' ' << a.second;
}
template <typename T>
istream & operator >> (istream &in, vector<T> &a) {
for (auto &i : a) {
in >> i;
}
return in;
}
template <typename T>
ostream &operator << (ostream &out, vector<T> a) {
for (auto i : a) {
out << i << ' ';
}
return out;
}
template <typename T>
void prnt(vector<T> a, string c = "\n") {
for (T i : a) {
cout << i << c;
}
}
const ll MAXN = 2e5 + 1;
vector<pair<ll, ll>> g[MAXN];
ll dp[MAXN][2];
void dijkstra(vector<ll> &d, ll start) {
fill(d.begin(), d.end(), INF);
set<pair<ll, ll>> q;
q.insert({0, start});
d[start] = 0;
while (!q.empty()) {
auto [dist, v] = *q.begin();
q.erase(q.begin());
if (dist > d[v]) {
continue;
}
for (auto [u, w] : g[v]) {
if (d[u] > d[v] + w) {
d[u] = d[v] + w;
q.insert({d[u], u});
}
}
}
}
vector<ll> g2[MAXN], order;
bitset<MAXN> used, good;
void dfs(ll v) {
used[v] = 1;
for (auto u : g2[v]) {
if (!used[u]) {
dfs(u);
}
}
order.push_back(v);
}
void solve() {
ll n, m, s, t, u, v;
cin >> n >> m >> s >> t >> u >> v;
for (ll i = 0; i < m; ++i) {
ll a, b, w;
cin >> a >> b >> w;
g[a].push_back({b, w});
g[b].push_back({a, w});
}
vector<ll> du(n + 1), dv(n + 1), ds(n + 1), dt(n + 1);
dijkstra(du, u);
dijkstra(dv, v);
dijkstra(ds, s);
dijkstra(dt, t);
for (ll i = 1; i <= n; ++i) {
if (ds[i] + dt[i] == ds[t]) {
good[i] = 1;
}
}
for (ll i = 1; i <= n; ++i) {
if (!good[i]) {
continue;
}
for (auto [j, w] : g[i]) {
if (good[j] && ds[i] + w + dt[j] == ds[t]) {
g2[i].push_back(j);
}
}
}
dfs(s);
std::reverse(order.begin(), order.end());
fill(dp[0], dp[0] + MAXN * 2, INF);
dp[s][0] = ds[u];
dp[s][1] = ds[v];
ll ans = dv[u];
for (auto i : order) {
for (auto j : g2[i]) {
dp[j][0] = min(dp[i][0], du[j]);
dp[j][1] = min(dp[i][1], dv[j]);
}
}
for (ll i = 1; i <= n; ++i) {
ans = min(ans, min(dp[i][0] + dv[i], dp[i][1] + du[i]));
}
cout << ans;
}
signed main() {
#ifdef LOCAL
freopen("inp.txt", "r", stdin);
freopen("out.txt", "w", stdout);
freopen("err.txt", "w", stderr);
auto start_time = clock();
#endif
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
ll times = 1;
// cin >> times;
while (times--)
solve(), cout << '\n';
#ifdef LOCAL
auto end_time = clock();
cerr << setprecision(3) << fixed << "Execution time: " << (end_time - start_time) * (ll) 1e3 / CLOCKS_PER_SEC << " ms\n";
#endif
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... |