結果

問題 No.614 壊れたキャンパス
ユーザー ふーらくたるふーらくたる
提出日時 2017-12-13 21:10:08
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 993 ms / 2,000 ms
コード長 1,726 bytes
コンパイル時間 889 ms
コンパイル使用メモリ 77,848 KB
実行使用メモリ 93,976 KB
最終ジャッジ日時 2023-08-21 02:28:09
合計ジャッジ時間 9,333 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
31,556 KB
testcase_01 AC 14 ms
31,744 KB
testcase_02 AC 14 ms
31,472 KB
testcase_03 AC 14 ms
31,476 KB
testcase_04 AC 14 ms
31,484 KB
testcase_05 AC 14 ms
31,500 KB
testcase_06 AC 14 ms
31,472 KB
testcase_07 AC 14 ms
31,600 KB
testcase_08 AC 647 ms
92,792 KB
testcase_09 AC 993 ms
92,768 KB
testcase_10 AC 509 ms
93,968 KB
testcase_11 AC 748 ms
93,440 KB
testcase_12 AC 765 ms
93,412 KB
testcase_13 AC 760 ms
93,800 KB
testcase_14 AC 670 ms
93,944 KB
testcase_15 AC 524 ms
93,732 KB
testcase_16 AC 556 ms
93,420 KB
testcase_17 AC 194 ms
93,976 KB
testcase_18 AC 314 ms
77,360 KB
testcase_19 AC 329 ms
79,916 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <map>
#include <utility>
#include <vector>
using namespace std;

#define int long long

using int64 = long long;
using P = pair<int, int>;   // (棟, 階)

const int maxn = 300000;
const int64 inf = (1LL << 60);

map<int, int64> table[maxn];
map<int, vector<int>> G[maxn];

signed main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int64 N, M, K, S, T;
    cin >> N >> M >> K >> S >> T;

    for (int i = 0; i < M; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        a--;

        G[a][b].push_back(c);
        table[a][b] = inf;
        table[a + 1][c] = inf;
    }

    if (N == 1 and M == 0) {
        cout << abs(S - T) << endl;
        return 0;
    }

    for (auto& mp : table[N - 1]) mp.second = abs(T - mp.first);
    for (int i = N - 1; i >= 0; i--) {
        int64 val = inf;
        for (auto& p : table[i]) {
            for (int to : G[i][p.first]) {
                p.second = min(p.second, table[i + 1][to]);
            }
            p.second = min(p.second, val + p.first);
            val = min(val, p.second - p.first);
        }
        /*
        for (auto it = table[i].begin(); it != table[i].end(); it++) {
            it->second = min(it->second, val + it->first);
            val = min(val, it->second - it->first);
        }
        */
        val = inf;
        for (auto it = table[i].rbegin(); it != table[i].rend(); it++) {
            it->second = min(it->second, val - it->first);
            val = min(val, it->second + it->first);
        }
    }

    int64 ans = inf;
    for (auto& mp : table[0]) ans = min(ans, mp.second + abs(S - mp.first));

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

    return 0;
}
0