結果
| 問題 | No.614 壊れたキャンパス | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2017-12-14 04:23:00 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 423 ms / 2,000 ms | 
| コード長 | 2,124 bytes | 
| コンパイル時間 | 2,308 ms | 
| コンパイル使用メモリ | 189,580 KB | 
| 実行使用メモリ | 29,964 KB | 
| 最終ジャッジ日時 | 2024-12-14 11:15:42 | 
| 合計ジャッジ時間 | 5,910 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 20 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll INF = 1000000000000000LL;
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, M, K, S, T;
    cin >> N >> M >> K >> S >> T;
    S--;
    T--;
    vector< vector<P> > g(N);
    for (int i = 0; i < M; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        a--;
        b--;
        c--;
        g[a].emplace_back(b, c);
    }
    for (int i = 0; i < N - 1; i++) {
        sort(g[i].begin(), g[i].end());
    }
    map<int, ll> memo;
    memo[S] = 0;
    for (int i = 0; i < N - 1; i++) {
        vector<int> hoge;
        for (auto& p : memo) hoge.push_back(p.first);
        vector<int> foo;
        for (P& p : g[i]) {
            foo.push_back(p.first);
        }
        foo.erase(unique(foo.begin(), foo.end()), foo.end());
        int sz = foo.size();
        ll val = INF;
        map<int, ll> tmp;
        for (int i = 0, j = 0; i < sz; i++) {
            while (j < hoge.size()) {
                if (hoge[j] > foo[i]) break;
                val = min(val, memo[hoge[j]] - hoge[j]);
                j++;
            }
            if (val < INF) tmp[foo[i]] = val + foo[i];
        }
        val = INF;
        for (int i = sz - 1, j = (int)hoge.size() - 1; i >= 0; i--) {
            while (j >= 0) {
                if (hoge[j] < foo[i]) break;
                val = min(val, memo[hoge[j]] + hoge[j]);
                j--;
            }
            if (val < INF) {
                if (tmp.find(foo[i]) == tmp.end()) tmp[foo[i]] = val - foo[i];
                else tmp[foo[i]] = min(tmp[foo[i]], val - foo[i]);
            }
        }
        memo.clear();
        for (P& p : g[i]) {
            if (tmp.find(p.first) == tmp.end()) continue;
            if (memo.find(p.second) == memo.end()) memo[p.second] = tmp[p.first];
            else memo[p.second] = min(memo[p.second], tmp[p.first]);
        }
    }
    ll ans = INF;
    for (auto& p : memo) {
        ans = min(ans, p.second + abs(p.first - T));
    }
    cout << (ans == INF ? -1 : ans) << endl;
    return 0;
}
            
            
            
        