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;
#define int long long
#define MAX 100007
int f[MAX];
vector<pair<int, int>> g[MAX];
vector<int> fu, fv, par[MAX], ng[MAX], rng[MAX];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
bool indag[MAX];
void dfs(int u)
{
indag[u] = true;
for (int v : par[u]) if (!indag[v]) dfs(v);
}
vector<int> topo;
bool visited[MAX];
void toposort(int u, bool cur)
{
visited[u] = true;
for (int v : (!cur ? ng[u] : rng[u])) if (!visited[v]) toposort(v, cur);
topo.push_back(u);
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
// freopen("test.inp", "r", stdin); freopen("test.out", "w", stdout);
int n, m, s, t, st, ed;
cin >> n >> m >> s >> t >> st >> ed;
while (m--)
{
int u, v, w;
cin >> u >> v >> w;
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
auto dij = [&](int start, vector<int> &cur) {
cur.assign(n + 2, 1e18);
cur[start] = 0;
q.emplace(0, start);
while (!q.empty())
{
int w, u;
tie(w, u) = q.top();
q.pop();
if (cur[u] != w) continue;
for (auto [v, c] : g[u])
if (cur[u] + c < cur[v]) q.emplace(cur[v] = cur[u] + c, v);
}
};
dij(st, fu); dij(ed, fv);
int ans = fu[ed];
memset(f, 0x3f, sizeof f);
f[s] = 0;
q.emplace(0, s);
while (!q.empty())
{
int w, u;
tie(w, u) = q.top();
q.pop();
if (f[u] != w) continue;
for (auto [v, c] : g[u])
if (f[u] + c < f[v]) par[v] = {u}, q.emplace(f[v] = f[u] + c, v);
else if (f[u] + c == f[v]) par[v].push_back(u);
}
dfs(t);
for (int i = 1; i <= n; i++) if (indag[i])
for (auto [v, c] : g[i])
if (indag[v])
{
ng[i].push_back(v);
rng[v].push_back(i);
}
auto cal = [&](int start, int end) -> int {
vector<vector<int>> f(n + 2, vector<int>(2, 1e18));
memset(visited, false, n + 2);
topo.clear();
toposort(start, start != s);
std::reverse(topo.begin(), topo.end());
f[0][0] = f[0][1] = 1e18;
f[start][0] = fu[start], f[start][1] = fv[start];
memset(visited, false, n + 2);
for (int u : topo)
for (int v : (start == s ? ng[u] : rng[u]))
if (!visited[v])
{
visited[v] = true;
f[v][0] = min(fu[v], f[u][0]);
f[v][1] = min(fv[v], f[u][1]);
q.emplace(0, v);
}
else if (min(fu[v], f[u][0]) + min(fv[v], f[u][1]) <= f[v][0] + f[v][1])
{
f[v][0] = min(fu[v], f[u][0]);
f[v][1] = min(fv[v], f[u][1]);
}
return f[end][0] + f[end][1];
};
ans = min({ans, cal(s, t), cal(t, s)});
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... |