結果
問題 | No.614 壊れたキャンパス |
ユーザー |
|
提出日時 | 2020-08-18 20:48:34 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,576 ms / 2,000 ms |
コード長 | 3,397 bytes |
コンパイル時間 | 1,881 ms |
コンパイル使用メモリ | 125,884 KB |
最終ジャッジ日時 | 2025-01-13 03:27:58 |
ジャッジサーバーID (参考情報) |
judge3 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 20 |
ソースコード
#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <map>#include <tuple>#include <limits>template <class T>using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;template <class T>std::map<T, int> compress(std::vector<T>& v) {std::sort(v.begin(), v.end());v.erase(std::unique(v.begin(), v.end()), v.end());std::map<T, int> rev;for (int i = 0; i < (int)v.size(); ++i) rev[v[i]] = i;return rev;}template <class Cost = int>struct Edge {int src, dst;Cost cost;Edge(int src = -1, int dst = -1, Cost cost = 1): src(src), dst(dst), cost(cost){};bool operator<(const Edge<Cost>& e) const { return this->cost < e.cost; }bool operator>(const Edge<Cost>& e) const { return this->cost > e.cost; }};template <class Cost = int>struct Graph {std::vector<std::vector<Edge<Cost>>> graph;Graph(int n = 0) : graph(n) {}void span(bool direct, int src, int dst, Cost cost = 1) {graph[src].emplace_back(src, dst, cost);if (!direct) graph[dst].emplace_back(dst, src, cost);}int size() const { return graph.size(); }void clear() { graph.clear(); }void resize(int n) { graph.resize(n); }std::vector<Edge<Cost>>& operator[](int v) { return graph[v]; }std::vector<Edge<Cost>> operator[](int v) const { return graph[v]; }};template <class Cost>std::vector<Cost> dijkstra(const Graph<Cost>& graph, int s) {std::vector<Cost> dist(graph.size(), -1);dist[s] = 0;MinHeap<std::pair<Cost, int>> que;que.emplace(0, s);while (!que.empty()) {int v;Cost d;std::tie(d, v) = que.top();que.pop();if (d > dist[v]) continue;for (const auto& e : graph[v]) {if (dist[e.dst] != -1 &&dist[e.dst] <= dist[v] + e.cost) continue;dist[e.dst] = dist[v] + e.cost;que.emplace(dist[e.dst], e.dst);}}return dist;}using lint = long long;void solve() {int n, m, k, s, t;std::cin >> n >> m >> k >> s >> t;--s, --t;auto enc = [&](int x, int y) { return x + lint(y) * n; };std::vector<std::pair<lint, lint>> es;std::vector<std::vector<int>> yss(n);yss[0].push_back(s);yss[n - 1].push_back(t);while (m--) {int x, y1, y2;std::cin >> x >> y1 >> y2;--x, --y1, --y2;yss[x].push_back(y1);yss[x + 1].push_back(y2);es.emplace_back(enc(x, y1), enc(x + 1, y2));}std::vector<lint> vs;for (int x = 0; x < n; ++x) {auto& ys = yss[x];compress(ys);for (auto y : ys) vs.push_back(enc(x, y));}auto vrev = compress(vs);int nn = vs.size();Graph<lint> graph(nn);for (auto [u, v] : es) graph.span(true, vrev[u], vrev[v], 0);for (int x = 0; x < n; ++x) {auto& ys = yss[x];int l = ys.size();for (int i = 0; i + 1 < l; ++i) {int u = vrev[enc(x, ys[i])],v = vrev[enc(x, ys[i + 1])];graph.span(false, u, v, ys[i + 1] - ys[i]);}}auto ds = dijkstra(graph, vrev[enc(0, s)]);auto ans = ds[vrev[enc(n - 1, t)]];std::cout << ans << "\n";}int main() {std::cin.tie(nullptr);std::ios::sync_with_stdio(false);solve();return 0;}