結果

問題 No.2100 [Cherry Alpha C] Two-way Steps
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-06-11 04:05:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,970 bytes
コンパイル時間 1,460 ms
コンパイル使用メモリ 123,740 KB
実行使用メモリ 24,180 KB
最終ジャッジ日時 2023-08-31 01:41:56
合計ジャッジ時間 20,428 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
11,436 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 140 ms
17,356 KB
testcase_05 AC 109 ms
13,544 KB
testcase_06 AC 21 ms
6,260 KB
testcase_07 AC 26 ms
6,868 KB
testcase_08 AC 141 ms
21,752 KB
testcase_09 AC 107 ms
11,484 KB
testcase_10 AC 1,668 ms
6,248 KB
testcase_11 AC 97 ms
15,468 KB
testcase_12 AC 74 ms
9,324 KB
testcase_13 AC 86 ms
17,612 KB
testcase_14 AC 79 ms
7,220 KB
testcase_15 AC 90 ms
16,724 KB
testcase_16 AC 85 ms
7,468 KB
testcase_17 AC 60 ms
15,920 KB
testcase_18 AC 88 ms
17,940 KB
testcase_19 AC 164 ms
20,944 KB
testcase_20 AC 35 ms
7,480 KB
testcase_21 AC 73 ms
10,224 KB
testcase_22 AC 87 ms
7,272 KB
testcase_23 AC 127 ms
18,680 KB
testcase_24 AC 184 ms
23,956 KB
testcase_25 AC 187 ms
24,088 KB
testcase_26 AC 186 ms
23,848 KB
testcase_27 AC 187 ms
24,036 KB
testcase_28 AC 188 ms
24,036 KB
testcase_29 AC 183 ms
24,180 KB
testcase_30 AC 184 ms
23,948 KB
testcase_31 AC 183 ms
23,892 KB
testcase_32 AC 183 ms
24,092 KB
testcase_33 AC 182 ms
23,944 KB
testcase_34 AC 293 ms
8,280 KB
testcase_35 AC 1,530 ms
8,000 KB
testcase_36 AC 1,898 ms
7,280 KB
testcase_37 TLE -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <numeric>
#include <deque>
#include <complex>
#include <cassert>

using namespace std;

template<typename T> using pq = priority_queue<T, vector<T>, greater<T>>;
using ll = long long; 

vector<ll> H;

vector<vector<ll>> dijkstra(vector<vector<ll>> &E, ll start){
 
    ll N = E.size(), t;
    ll from, alt, d;

    pq<tuple<ll, ll, ll>> que;
    vector<vector<ll>> dist(N, vector<ll>(2, 1e18));
    que.push({0, start, 0});
    dist[start][0] = 0;
 
    while(!que.empty()){
        tie(d, from, t) = que.top();
        que.pop();
        if (dist[from][t] < d) continue;
        for (auto to : E[from]){
            if (start == 0){
                if (to < from) continue;
            } 
            else{
                if (to > from) continue;
            }
            if (H[to]>H[from]){
                if (t == 1) continue;
                alt = d - (H[to]-H[from]);
                if (alt < dist[to][1]){
                    dist[to][1] = alt;
                    que.push({dist[to][1], to, 1});
                }
            }
            else{
                alt = d;
                if (alt < dist[to][0]){
                    dist[to][0] = alt;
                    que.push({dist[to][0], to, 0});
                }
            }
        }
    }
 
    return dist;
}

int main(){

    ll N, M, A, B, C, ans;
    cin >> N >> M;
    H.resize(N);
    for (int i=0; i<N; i++) cin >> H[i];
    vector<vector<ll>> E(N), E2(N);
    for (int i=0; i<M; i++){
        cin >> A >> B;
        A--; B--;
        E[A].push_back(B);
        E[B].push_back(A);
    }
    vector<vector<ll>> dist = dijkstra(E, 0);
    ans = min(dist[N-1][0], dist[N-1][1]);
    cout << (ans == 1e18 ? -1 : -ans) << endl;
    dist = dijkstra(E, N-1);
    ans = min(dist[0][0], dist[0][1]);
    cout << (ans == 1e18 ? -1 : -ans) << endl;
}
0