結果

問題 No.614 壊れたキャンパス
ユーザー face4face4
提出日時 2019-10-12 17:24:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 863 ms / 2,000 ms
コード長 1,765 bytes
コンパイル時間 941 ms
コンパイル使用メモリ 101,600 KB
実行使用メモリ 80,116 KB
最終ジャッジ日時 2023-08-19 00:45:12
合計ジャッジ時間 11,239 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 781 ms
78,528 KB
testcase_09 AC 786 ms
77,240 KB
testcase_10 AC 663 ms
77,616 KB
testcase_11 AC 824 ms
79,584 KB
testcase_12 AC 863 ms
79,284 KB
testcase_13 AC 852 ms
80,116 KB
testcase_14 AC 769 ms
79,912 KB
testcase_15 AC 679 ms
77,744 KB
testcase_16 AC 685 ms
78,492 KB
testcase_17 AC 385 ms
74,092 KB
testcase_18 AC 349 ms
64,576 KB
testcase_19 AC 366 ms
63,036 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// 計算量がちょっと怖い
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<set>
using namespace std;

typedef long long ll;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int n, m, k, s, t;
    cin >> n >> m >> k >> s >> t;
    set<pair<int,int>> sp;
    sp.insert({1, s});
    sp.insert({n, t});
    vector<int> a(m), b(m), c(m);
    for(int i = 0; i < m; i++){
        cin >> a[i] >> b[i] >> c[i];
        sp.insert({a[i], b[i]});
        sp.insert({a[i]+1, c[i]});
    }
    map<pair<int,int>, int> p2id;
    int siz = 0;
    for(auto p : sp)    p2id[p] = siz++;
    vector<pair<int,int>> vp[siz];
    auto bef = sp.begin(); int id = 1;
    for(auto it = ++sp.begin(); it != sp.end(); it++){
        if(it->first == bef->first){
            int diff = it->second-bef->second;
            vp[id].push_back({id-1, diff});
            vp[id-1].push_back({id, diff});
        }
        bef = it;
        id++;
    }
    for(int i = 0; i < m; i++){
        int ida = p2id[{a[i], b[i]}];
        int idb = p2id[{a[i]+1, c[i]}];
        vp[ida].push_back({idb, 0});
    }

    int from = p2id[{1, s}];
    int to = p2id[{n, t}];
    vector<ll> dp(siz, 1ll<<60);
    priority_queue<pair<ll,int>> pq;
    dp[from] = 0;
    pq.push({-0, from});
    while(!pq.empty()){
        auto p = pq.top();  pq.pop();
        ll cost = -p.first;
        int now = p.second;
        if(dp[now] != cost) continue;
        for(pair<int,int> edge : vp[now]){
            ll ncost = cost + edge.second;
            if(dp[edge.first] > ncost){
                dp[edge.first] = ncost;
                pq.push({-ncost, edge.first});
            }
        }
    }
    cout << (dp[to] == 1ll<<60 ? -1 : dp[to]) << endl;
    return 0;
}
0