結果

問題 No.614 壊れたキャンパス
ユーザー ふーらくたるふーらくたる
提出日時 2017-12-13 19:23:45
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 2,003 bytes
コンパイル時間 916 ms
コンパイル使用メモリ 96,040 KB
実行使用メモリ 818,692 KB
最終ジャッジ日時 2023-08-21 02:26:41
合計ジャッジ時間 15,603 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
10,348 KB
testcase_01 AC 5 ms
10,508 KB
testcase_02 AC 5 ms
10,356 KB
testcase_03 RE -
testcase_04 AC 5 ms
10,360 KB
testcase_05 AC 5 ms
10,408 KB
testcase_06 AC 6 ms
10,352 KB
testcase_07 RE -
testcase_08 AC 658 ms
35,616 KB
testcase_09 AC 586 ms
42,880 KB
testcase_10 AC 490 ms
35,420 KB
testcase_11 AC 1,526 ms
35,732 KB
testcase_12 AC 1,664 ms
35,956 KB
testcase_13 AC 1,603 ms
36,028 KB
testcase_14 AC 693 ms
35,520 KB
testcase_15 AC 408 ms
35,380 KB
testcase_16 AC 526 ms
35,316 KB
testcase_17 AC 503 ms
34,920 KB
testcase_18 MLE -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <map>
#include <utility>
#include <vector>
#include <queue>
#include <algorithm>
#include <tuple>
#include <assert.h>
#include <unordered_map>
using namespace std;

#define int long long

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);

int64 N, M, K, S, T;

vector<Edge> G[maxn];
unordered_map<int64, int64> dist;

signed main() {
    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[a * K + b] = dist[(a + 1) * K + c] = inf;

        G[a].emplace_back(a + 1, b, c);
        // G[a + 1].emplace_back(a, c, b);
    }

    dist[N * K + T] = inf;

    priority_queue<State, vector<State>, greater<State>> Q;
    Q.push({0, {1, S}});
    dist[1 * K + 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 * K + 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 * K + nxt_s] > abs(cur_s - h) + d) {
                dist[next * K + nxt_s] = abs(cur_s - h) + d;
                Q.push({dist[next * K + nxt_s], {next, nxt_s}});
            }
        }
    }

    if (ans >= inf) cout << -1 << endl;
    else cout << ans << endl;

    return 0;
}
0