이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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);
}
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));
vector<bool> visited(n + 2, false);
visited[start] = true;
f[0][0] = f[0][1] = 1e18;
f[start][0] = fu[start], f[start][1] = fv[start];
q.emplace(0, start);
while (!q.empty())
{
auto tmp = q.top();
q.pop();
int w, u; tie(w, u) = tmp;
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... |