結果

問題 No.614 壊れたキャンパス
ユーザー ふーらくたるふーらくたる
提出日時 2017-12-13 13:46:21
言語 C++11
(gcc 11.4.0)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,898 bytes
コンパイル時間 986 ms
コンパイル使用メモリ 95,216 KB
実行使用メモリ 63,976 KB
最終ジャッジ日時 2023-08-21 02:24:45
合計ジャッジ時間 7,851 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
17,464 KB
testcase_01 AC 5 ms
10,472 KB
testcase_02 AC 6 ms
10,540 KB
testcase_03 RE -
testcase_04 WA -
testcase_05 AC 5 ms
10,404 KB
testcase_06 WA -
testcase_07 RE -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0