이 제출은 이전 버전의 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];
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])
{
ng[v].push_back(u);
if (!indag[v]) dfs(v);
}
}
vector<int> topo;
bool visited[MAX];
void toposort(int u)
{
visited[u] = true;
for (int v : ng[u]) if (!visited[v]) toposort(v);
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);
vector<vector<int>> dp(n + 2, vector<int>(2, 1e18));
toposort(s);
std::reverse(topo.begin(), topo.end());
for (int i = 1; i <= n; i++)
{
dp[i][0] = fu[i];
dp[i][1] = fv[i];
}
for (int u : topo)
for (int v : ng[u])
{
dp[v][0] = min(dp[v][0], dp[u][0]);
dp[v][1] = min(dp[v][1], dp[u][1]);
}
for (int i = 1; i <= n; i++) if (indag[i]) ans = min({ans, dp[i][0] + fv[i], dp[i][1] + fu[i]});
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... |