結果

問題 No.2100 [Cherry Alpha C] Two-way Steps
ユーザー t98slidert98slider
提出日時 2022-10-14 22:04:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 148 ms / 2,000 ms
コード長 2,093 bytes
コンパイル時間 2,060 ms
コンパイル使用メモリ 183,092 KB
実行使用メモリ 30,080 KB
最終ジャッジ日時 2023-09-08 21:39:56
合計ジャッジ時間 8,396 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 106 ms
22,220 KB
testcase_05 AC 86 ms
17,288 KB
testcase_06 AC 14 ms
7,040 KB
testcase_07 AC 17 ms
7,732 KB
testcase_08 AC 107 ms
26,348 KB
testcase_09 AC 83 ms
15,396 KB
testcase_10 AC 52 ms
9,568 KB
testcase_11 AC 76 ms
18,656 KB
testcase_12 AC 50 ms
12,476 KB
testcase_13 AC 60 ms
20,276 KB
testcase_14 AC 48 ms
10,196 KB
testcase_15 AC 66 ms
19,800 KB
testcase_16 AC 53 ms
10,768 KB
testcase_17 AC 41 ms
18,024 KB
testcase_18 AC 61 ms
20,524 KB
testcase_19 AC 128 ms
26,344 KB
testcase_20 AC 22 ms
8,532 KB
testcase_21 AC 50 ms
12,816 KB
testcase_22 AC 54 ms
10,484 KB
testcase_23 AC 98 ms
23,248 KB
testcase_24 AC 147 ms
30,020 KB
testcase_25 AC 147 ms
30,016 KB
testcase_26 AC 142 ms
30,036 KB
testcase_27 AC 148 ms
29,960 KB
testcase_28 AC 146 ms
30,020 KB
testcase_29 AC 147 ms
30,080 KB
testcase_30 AC 148 ms
29,980 KB
testcase_31 AC 146 ms
30,028 KB
testcase_32 AC 147 ms
30,052 KB
testcase_33 AC 147 ms
30,024 KB
testcase_34 AC 87 ms
13,272 KB
testcase_35 AC 80 ms
12,324 KB
testcase_36 AC 78 ms
12,304 KB
testcase_37 AC 73 ms
11,984 KB
testcase_38 AC 84 ms
12,800 KB
testcase_39 AC 68 ms
11,524 KB
testcase_40 AC 68 ms
11,568 KB
testcase_41 AC 69 ms
11,680 KB
testcase_42 AC 69 ms
11,444 KB
testcase_43 AC 68 ms
11,396 KB
testcase_44 AC 67 ms
27,032 KB
testcase_45 AC 68 ms
26,916 KB
testcase_46 AC 63 ms
26,876 KB
testcase_47 AC 60 ms
26,796 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int  n, M, u, v;
    ll INF = 1ll << 30;
    cin >> n >> M;
    vector<vector<pair<int,ll>>> g1(2 * n), g2(2 * n);
    vector<int> h(n);
    for(int i = 0; i < n; i++){
        cin >> h[i];
        g1[i].emplace_back(i + n, 0);
        g2[i].emplace_back(i + n, 0);
    }
    for(int i = 0; i < M; i++){
        cin >> u >> v;
        u--, v--;
        if(h[u] > h[v])swap(u, v);
        if(u < v){
            g1[u].emplace_back(n + v, abs(v - u) * INF - (h[v] - h[u]));
            g2[v + n].emplace_back(u, abs(v - u) * INF);
        }else{
            g2[u].emplace_back(n + v, abs(v - u) * INF - (h[v] - h[u]));
            g1[v + n].emplace_back(u, abs(v - u) * INF);
        }
    }
    priority_queue<pair<ll, int>, vector<pair<ll,int>>, greater<pair<ll,int>>> pq;
    ll d;
    vector<ll> dist1(2 * n, 1ll << 61), dist2(2 * n, 1ll << 61);
    dist1[0] = 0;
    pq.emplace(0, 0);
    while(!pq.empty()){
        tie(d, v) = pq.top();
        pq.pop();
        //cerr << d << " " << v << '\n';
        if(d > dist1[v])continue;
        for(auto &&p:g1[v]){
            if(d + p.second >= dist1[p.first])continue;
            dist1[p.first] = d + p.second;
            pq.emplace(dist1[p.first], p.first);
        }
    }
    dist2[n - 1] = 0;
    pq.emplace(0, n - 1);
    while(!pq.empty()){
        tie(d, v) = pq.top();
        pq.pop();
        //cerr << d << " " << v << '\n';
        if(d > dist2[v])continue;
        for(auto &&p:g2[v]){
            if(d + p.second >= dist2[p.first])continue;
            dist2[p.first] = d + p.second;
            pq.emplace(dist2[p.first], p.first);
        }
    }
    //cerr << "enter" << '\n';
    ll ans = min(dist1[n - 1], dist1[2 * n - 1]);
    if(ans >> 61 & 1){
        cout << -1 << '\n';
    }else{
        cout << INF * (n - 1) - ans << '\n';
    }
    ans = min(dist2[0], dist2[n]);
    if(ans >> 61 & 1){
        cout << -1 << '\n';
    }else{
        cout << INF * (n - 1) - ans << '\n';
    }
}
0