結果

問題 No.614 壊れたキャンパス
ユーザー sekiya9311
提出日時 2017-12-21 23:20:32
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
MLE  
実行時間 -
コード長 1,267 bytes
コンパイル時間 2,228 ms
コンパイル使用メモリ 191,156 KB
実行使用メモリ 727,404 KB
最終ジャッジ日時 2024-12-17 22:53:49
合計ジャッジ時間 21,214 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 14 TLE * 5 MLE * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using state = tuple<long long, int, int>;   // (cost, tou, kai)

const int MAX = 2e5 + 10;
int N, M, S, T;
long K;
vector<pair<int, long long>> G[MAX];
map<int, long long> mp[MAX];   //[tou][kai] = minCost

int main() {
    scanf("%d%d%lld%d%d", &N, &M, &K, &S, &T);
    S--;T--;K--;
    for (int i = 0; i < M; i++) {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        a--;b--;c--;
        G[a].emplace_back(b, c);
    }
    priority_queue<state, vector<state>, greater<state>> pq;
    pq.emplace(0, 0, S);
    const long long inf = 1e18;
    long long ans = inf;
    while (!pq.empty()) {
        long long cost;
        int tou, kai;
        tie(cost, tou, kai) = pq.top();
        pq.pop();
        if (tou == N - 1) {
            ans = min(ans, cost + llabs(kai - T));
        }
        if (mp[tou].count(kai)) {
            continue;
        }
        mp[tou][kai] = cost;
        for (auto&& e : G[tou]) {
            if (mp[tou + 1].count(e.second) && mp[tou + 1][e.second] <= cost + llabs(e.first - kai)) {
                continue;
            }
            pq.emplace(cost + llabs(e.first - kai), tou + 1, e.second);
        }
    }
    cout << (ans == inf ? -1 : ans) << endl;
    return 0;
}
0