結果
問題 | No.614 壊れたキャンパス |
ユーザー |
![]() |
提出日時 | 2017-12-29 21:28:52 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 997 ms / 2,000 ms |
コード長 | 2,479 bytes |
コンパイル時間 | 1,337 ms |
コンパイル使用メモリ | 83,056 KB |
実行使用メモリ | 82,216 KB |
最終ジャッジ日時 | 2024-12-21 10:37:25 |
合計ジャッジ時間 | 12,448 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 20 |
ソースコード
#include <iostream>#include <algorithm>#include <vector>#include <algorithm>#include <queue>#include <functional>#define int long longusing namespace std;typedef pair<int, int> P;int INF = 1e+15;int n, m, k, s, t;int a[200000], b[200000], c[200000];vector<P> points;vector<int> heights[200001];vector<int> edgeTo[400002];vector<int> edgeCost[400002];int dijkstra(int start, int goal) {static int minCost[400002];static priority_queue<P, vector<P>, greater<P>> que;int i;for (i = 0; i < points.size(); i++) minCost[i] = INF;que.push(P(0, start));while (!que.empty()) {P now = que.top(); que.pop();int cst = now.first;int pos = now.second;if (minCost[pos] <= cst) continue;minCost[pos] = cst;if (pos == goal) break;for (i = 0; i < edgeTo[pos].size(); i++) {que.push(P(edgeCost[pos][i] + cst, edgeTo[pos][i]));}}return minCost[goal];}signed main() {int i, j;cin >> n >> m >> k >> s >> t;for (i = 0; i < m; i++) {cin >> a[i] >> b[i] >> c[i];points.push_back(P(a[i], b[i]));points.push_back(P(a[i] + 1, c[i]));heights[a[i]].push_back(b[i]);heights[a[i] + 1].push_back(c[i]);}points.push_back(P(1, s));points.push_back(P(n, t));sort(points.begin(), points.end());points.erase(unique(points.begin(), points.end()), points.end());heights[1].push_back(s);heights[n].push_back(t);for (i = 1; i <= n; i++) sort(heights[i].begin(), heights[i].end());for (i = 0; i < m; i++) {int from = lower_bound(points.begin(), points.end(), P(a[i], b[i])) - points.begin();int to = lower_bound(points.begin(), points.end(), P(a[i] + 1, c[i])) - points.begin();edgeTo[from].push_back(to);edgeCost[from].push_back(0);}for (i = 1; i <= n; i++) {for (j = 0; j + 1 < heights[i].size(); j++) {int from = lower_bound(points.begin(), points.end(), P(i, heights[i][j])) - points.begin();int to = lower_bound(points.begin(), points.end(), P(i, heights[i][j + 1])) - points.begin();edgeTo[from].push_back(to);edgeTo[to].push_back(from);edgeCost[from].push_back(heights[i][j + 1] - heights[i][j]);edgeCost[to].push_back(heights[i][j + 1] - heights[i][j]);}}int start = lower_bound(points.begin(), points.end(), P(1, s)) - points.begin();int goal = lower_bound(points.begin(), points.end(), P(n, t)) - points.begin();int minCost = dijkstra(start, goal);if (minCost >= INF) cout << -1 << endl;else cout << minCost << endl;return 0;}