結果
| 問題 | No.614 壊れたキャンパス | 
| コンテスト | |
| ユーザー |  treeone | 
| 提出日時 | 2017-12-13 13:28:20 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                TLE
                                 
                            (最新) 
                                AC
                                 
                            (最初) | 
| 実行時間 | - | 
| コード長 | 2,421 bytes | 
| コンパイル時間 | 1,721 ms | 
| コンパイル使用メモリ | 169,848 KB | 
| 実行使用メモリ | 29,912 KB | 
| 最終ジャッジ日時 | 2024-12-14 10:16:59 | 
| 合計ジャッジ時間 | 39,063 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 8 TLE * 12 | 
ソースコード
#include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define all(a) a.begin(), a.end()
#define int long long
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef pair<int, int> P;
const int INF = 1e18;
int n, m, k, s, t;
vector<P> d[200010];
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m >> k >> s >> t;
    rep(i, 0, m){
        int a, b, c;
        cin >> a >> b >> c;
        assert(1 <= a && a <= n - 1 && 1 <= b && b <= k && 1 <= c && c <= k);
        a--;
        d[a].push_back(P(b, c));
    }
    vector<P> pre;
    pre.push_back(P(s, 0));
    d[n - 1].push_back(P(t, -1));
    rep(i, 0, n){
        cerr << "now " << i << " ,next " << i + 1 << "====================================" << endl;
        vector<P> next;
        rep(j, 0, pre.size()){
            if(next.size()){
                P p = next.back();
                if(p.second + abs(p.first - pre[j].first) <= pre[j].second){
                    continue;
                }
            }
            while(next.size()){
                P p = next.back();
                if(p.second > pre[j].second + abs(p.first - pre[j].first)){
                    next.pop_back();
                }else{
                    next.push_back(pre[j]);
                    break;
                }
            }
            if(next.size() == 0){
                next.push_back(pre[j]);
            }
        }
        cerr << "next : " << endl;
        rep(j, 0, next.size()){
            cerr <<next[j].first << " " << next[j].second << endl;
        }
        pre.clear();
        cerr << "make pre : " << endl;
        rep(j, 0, d[i].size()){
            int cost = INF;
            int idx = upper_bound(all(next), P(d[i][j].first, 0)) - next.begin();
            if(idx != next.size()) chmin(cost, next[idx].second + abs(next[idx].first - d[i][j].first));
            idx--;
            if(idx != -1) chmin(cost, next[idx].second + abs(next[idx].first - d[i][j].first));
            pre.push_back(P(d[i][j].second, cost));
            cerr << d[i][j].first << " " << d[i][j].second << " " << cost << endl;
        }
        sort(all(pre));
        cerr << "pre : " << endl;        
        rep(j, 0, pre.size()){
            cerr << pre[j].first << " " << pre[j].second << endl;
        }
    }
    cout << (pre[0].second == INF ? -1 : pre[0].second) << endl;
}
            
            
            
        