이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<pair<int, int>> adj[100001];
ll minCostS[100001], minCostU[100001], minCostV[100001];
int main()
{
cin.tie(NULL) -> sync_with_stdio(false);
int N, M, S, T, U, V;
cin >> N >> M >> S >> T >> U >> V;
for (int i = 0; i <= N; ++i)
minCostS[i] = minCostU[i] = minCostV[i] = LLONG_MAX;
minCostS[S] = minCostU[U] = minCostV[V] = 0;
while (M--)
{
int a, b, x;
cin >> a >> b >> x;
adj[a].push_back({b, x});
adj[b].push_back({a, x});
}
using E = pair<ll, int>;
priority_queue<E, vector<E>, greater<E>> q;
q.push({0, S});
while (!q.empty())
{
auto [cost, node] = q.top();
q.pop();
if (cost != minCostS[node])
continue;
for (auto [i, x] : adj[node])
{
if (cost + x < minCostS[i])
q.push({minCostS[i] = cost + x, i});
}
}
vector<bool> visited(N + 1), commuter(N + 1);
queue<int> qq;
qq.push(T);
while (!qq.empty())
{
int node = qq.front();
qq.pop();
visited[node] = commuter[node] = true;
for (auto [i, x] : adj[node])
if (minCostS[i] + x == minCostS[node] && !visited[i])
qq.push(i), visited[i] = true;
}
ll minU = LLONG_MAX, minV = LLONG_MAX;
q.push({0, U});
while (!q.empty())
{
auto [cost, node] = q.top();
q.pop();
if (cost != minCostU[node])
continue;
if (commuter[node])
minU = min(minU, cost);
for (auto [i, x] : adj[node])
{
if (cost + x < minCostU[i])
q.push({minCostU[i] = cost + x, i});
}
}
q.push({0, V});
while (!q.empty())
{
auto [cost, node] = q.top();
q.pop();
if (cost != minCostV[node])
continue;
if (commuter[node])
minV = min(minV, cost);
for (auto [i, x] : adj[node])
{
if (cost + x < minCostV[i])
q.push({minCostV[i] = cost + x, i});
}
}
cout << min(minCostU[V], minU + minV) << endl;
return 0;
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
*/
# | 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... |