結果
問題 | No.614 壊れたキャンパス |
ユーザー | startcpp |
提出日時 | 2017-12-29 21:28:52 |
言語 | C++11 (gcc 11.4.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 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 15 ms
26,752 KB |
testcase_01 | AC | 14 ms
26,880 KB |
testcase_02 | AC | 12 ms
26,752 KB |
testcase_03 | AC | 13 ms
26,880 KB |
testcase_04 | AC | 15 ms
26,752 KB |
testcase_05 | AC | 16 ms
26,624 KB |
testcase_06 | AC | 14 ms
27,008 KB |
testcase_07 | AC | 13 ms
27,008 KB |
testcase_08 | AC | 874 ms
75,996 KB |
testcase_09 | AC | 841 ms
78,052 KB |
testcase_10 | AC | 838 ms
75,140 KB |
testcase_11 | AC | 923 ms
76,052 KB |
testcase_12 | AC | 997 ms
76,028 KB |
testcase_13 | AC | 987 ms
75,884 KB |
testcase_14 | AC | 891 ms
76,372 KB |
testcase_15 | AC | 753 ms
75,348 KB |
testcase_16 | AC | 832 ms
75,680 KB |
testcase_17 | AC | 471 ms
71,912 KB |
testcase_18 | AC | 553 ms
82,216 KB |
testcase_19 | AC | 421 ms
73,128 KB |
ソースコード
#include <iostream> #include <algorithm> #include <vector> #include <algorithm> #include <queue> #include <functional> #define int long long using 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; }