結果
問題 | No.614 壊れたキャンパス |
ユーザー |
![]() |
提出日時 | 2017-12-13 13:46:21 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
RE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 1,898 bytes |
コンパイル時間 | 956 ms |
コンパイル使用メモリ | 92,800 KB |
実行使用メモリ | 171,936 KB |
最終ジャッジ日時 | 2024-12-14 10:17:30 |
合計ジャッジ時間 | 27,857 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 7 WA * 3 RE * 2 TLE * 8 |
ソースコード
#include <iostream>#include <map>#include <utility>#include <vector>#include <queue>#include <algorithm>#include <tuple>#include <assert.h>using namespace std;using int64 = long long;using P = pair<int, int>;using State = pair<int64, P>;using Edge = tuple<int, int, int>; // (次の棟, 渡り廊下のある階(自分), 渡り廊下のある階(相手))const int maxn = 300000;const int64 inf = (1LL << 50);vector<Edge> G[maxn];map<P, int64> dist;int main() {int N, M, K, S, T;cin >> N >> M >> K >> S >> T;assert(2 <= N and N <= 200000);assert(0 <= M and M <= 200000);assert(1 <= K and K <= 2e9);assert(1 <= S and S <= K);assert(1 <= T and T <= K);for (int i = 0; i < M; i++) {int a, b;int64 c;cin >> a >> b >> c;assert(1 <= a and a <= N - 1);assert(1 <= b and b <= K);assert(1 <= c and c <= K);P p1 = {a, b},p2 = {a + 1, c};dist[p1] = dist[p2] = inf;G[a].emplace_back(a + 1, b, c);G[a + 1].emplace_back(a, c, b);}dist[{N, T}] = inf;priority_queue<State, vector<State>, greater<State>> Q;Q.push({0, {1, S}});dist[{1, S}] = 0;int64 ans = inf;while (!Q.empty()) {auto q = Q.top(); Q.pop();int64 d = q.first;int p = q.second.first;int h = q.second.second;if (dist[{p, h}] < d) continue;if (p == N) ans = min(ans, abs(h - T) + d);for (Edge& e : G[p]) {int next, cur_s, nxt_s;tie(next, cur_s, nxt_s) = e;if (dist[{next, nxt_s}] > abs(cur_s - h) + d) {dist[{next, nxt_s}] = abs(cur_s - h) + d;Q.push({dist[{next, nxt_s}], {next, nxt_s}});}}}if (ans >= inf) cout << -1 << endl;else cout << ans << endl;return 0;}