이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <tuple>
template <class T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
using lint = long long;
constexpr lint INF = 1LL << 50;
struct Edge {
int dst;
lint cost;
explicit Edge(int dst, lint cost) : dst(dst), cost(cost) {}
};
using Graph = std::vector<std::vector<Edge>>;
std::vector<lint> dijkstra(const Graph& graph, int r) {
std::vector<lint> dist(graph.size(), INF);
dist[r] = 0;
MinHeap<std::pair<lint, int>> que;
que.emplace(dist[r], r);
while (!que.empty()) {
lint d;
int v;
std::tie(d, v) = que.top();
que.pop();
if (dist[v] < d) continue;
for (auto e : graph[v]) {
int u = e.dst;
lint nd = d + e.cost;
if (dist[u] > nd) {
dist[u] = nd;
que.emplace(nd, u);
}
}
}
return dist;
}
void solve() {
int n, m, s, t, a, b;
std::cin >> n >> m >> s >> t >> a >> b;
--s, --t, --a, --b;
Graph graph(n);
while (m--) {
int u, v;
lint c;
std::cin >> u >> v >> c;
--u, --v;
graph[u].emplace_back(v, c);
graph[v].emplace_back(u, c);
}
auto ds = dijkstra(graph, s),
da = dijkstra(graph, a),
db = dijkstra(graph, b);
std::vector<int> vs(n);
std::iota(vs.begin(), vs.end(), 0);
std::sort(vs.begin(), vs.end(),
[&](int u, int v) { return ds[u] < ds[v]; });
std::vector<std::vector<lint>>
dist(3, std::vector<lint>(n, INF));
for (auto v : vs) {
dist[0][v] = da[v];
dist[1][v] = db[v];
dist[2][v] = da[v] + db[v];
for (auto e : graph[v]) {
int u = e.dst;
if (ds[u] + e.cost > ds[v]) continue;
for (int k = 0; k < 3; ++k) {
dist[k][v] = std::min(dist[k][v], dist[k][u]);
}
}
dist[2][v] = std::min({dist[2][v],
dist[0][v] + db[v],
dist[1][v] + da[v]});
}
std::cout << std::min(da[b], dist[2][t]) << std::endl;
}
int main() {
std::cin.tie(nullptr);
std::cout.tie(nullptr);
std::ios::sync_with_stdio(false);
solve();
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... |