結果

問題 No.614 壊れたキャンパス
ユーザー treeonetreeone
提出日時 2017-12-13 20:53:22
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 109 ms / 2,000 ms
コード長 2,101 bytes
コンパイル時間 1,396 ms
コンパイル使用メモリ 154,792 KB
実行使用メモリ 18,116 KB
最終ジャッジ日時 2023-08-21 02:27:07
合計ジャッジ時間 3,776 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,096 KB
testcase_01 AC 5 ms
8,312 KB
testcase_02 AC 5 ms
8,100 KB
testcase_03 AC 4 ms
8,040 KB
testcase_04 AC 4 ms
7,980 KB
testcase_05 AC 5 ms
8,080 KB
testcase_06 AC 5 ms
8,144 KB
testcase_07 AC 4 ms
8,104 KB
testcase_08 AC 78 ms
13,076 KB
testcase_09 AC 93 ms
18,116 KB
testcase_10 AC 109 ms
13,156 KB
testcase_11 AC 78 ms
12,588 KB
testcase_12 AC 92 ms
12,676 KB
testcase_13 AC 81 ms
12,456 KB
testcase_14 AC 81 ms
13,128 KB
testcase_15 AC 84 ms
13,048 KB
testcase_16 AC 83 ms
13,856 KB
testcase_17 AC 91 ms
14,288 KB
testcase_18 AC 70 ms
16,188 KB
testcase_19 AC 69 ms
15,660 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
    assert(1 <= n && n <= 2*1e5);
    assert(0 <= m && m <= 2*1e5);
    assert(1 <= k && k <= 1e9);
    assert(1 <= s && s <= k);
    assert(1 <= t && t <= k);
    
    rep(i, 0, m){
        int a, b, c;
        cin >> a >> b >> c;
        assert(1 <= a and a <= n - 1);
        assert(1 <= b and b <= k);
        assert(1 <= c and 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){
        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]);
            }
        }
        pre.clear();
        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));
        }
        sort(all(pre));
    }
    cout << (pre[0].second == INF ? -1 : pre[0].second) << endl;
}
0