結果

問題 No.2100 [Cherry Alpha C] Two-way Steps
ユーザー AngrySadEightAngrySadEight
提出日時 2022-10-14 23:06:46
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 172 ms / 2,000 ms
コード長 3,560 bytes
コンパイル時間 5,391 ms
コンパイル使用メモリ 263,040 KB
実行使用メモリ 16,812 KB
最終ジャッジ日時 2023-09-08 23:31:57
合計ジャッジ時間 11,128 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 126 ms
13,148 KB
testcase_05 AC 101 ms
11,032 KB
testcase_06 AC 19 ms
4,844 KB
testcase_07 AC 24 ms
5,156 KB
testcase_08 AC 124 ms
14,244 KB
testcase_09 AC 99 ms
10,544 KB
testcase_10 AC 53 ms
7,244 KB
testcase_11 AC 92 ms
11,124 KB
testcase_12 AC 70 ms
8,356 KB
testcase_13 AC 73 ms
10,364 KB
testcase_14 AC 61 ms
8,016 KB
testcase_15 AC 81 ms
10,860 KB
testcase_16 AC 67 ms
8,292 KB
testcase_17 AC 49 ms
8,836 KB
testcase_18 AC 76 ms
10,680 KB
testcase_19 AC 149 ms
15,436 KB
testcase_20 AC 32 ms
5,976 KB
testcase_21 AC 67 ms
8,748 KB
testcase_22 AC 64 ms
8,136 KB
testcase_23 AC 113 ms
12,960 KB
testcase_24 AC 164 ms
16,532 KB
testcase_25 AC 167 ms
16,524 KB
testcase_26 AC 163 ms
16,476 KB
testcase_27 AC 169 ms
16,596 KB
testcase_28 AC 165 ms
16,536 KB
testcase_29 AC 167 ms
16,612 KB
testcase_30 AC 172 ms
16,812 KB
testcase_31 AC 166 ms
16,432 KB
testcase_32 AC 163 ms
16,532 KB
testcase_33 AC 164 ms
16,668 KB
testcase_34 AC 90 ms
10,364 KB
testcase_35 AC 81 ms
9,872 KB
testcase_36 AC 80 ms
9,428 KB
testcase_37 AC 75 ms
9,348 KB
testcase_38 AC 87 ms
9,504 KB
testcase_39 AC 68 ms
10,580 KB
testcase_40 AC 70 ms
10,576 KB
testcase_41 AC 69 ms
10,564 KB
testcase_42 AC 68 ms
10,476 KB
testcase_43 AC 69 ms
10,420 KB
testcase_44 AC 98 ms
13,848 KB
testcase_45 AC 97 ms
14,012 KB
testcase_46 AC 94 ms
14,284 KB
testcase_47 AC 96 ms
13,896 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = (int)(n) - 1; i >= 0; i--)
#define repk(i, k, n) for (int i = k; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define mod1 1000000007
#define mod2 998244353
#define vi vector<int>
#define vs vector<string>
#define vc vector<char>
#define vl vector<ll>
#define vvi vector<vector<int>>
#define vvc vector<vector<char>>
#define vvl vector<vector<ll>>
#define vvvi vector<vector<vector<int>>>
#define vvvl vector<vector<vector<ll>>>
#define pii pair<int,int>
#define pil pair<int,ll>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define vpii vector<pair<int,int>>
#define vpll vector<pair<ll,ll>>
#define vvpii vector<vector<pair<int,int>>>
#define vvpll vector<vector<pair<ll,ll>>>

template<typename T> void debug(T e){
    cerr << e << endl;
}

template<typename T> void debug(vector<T> &v){
    rep(i, v.size()){
        cerr << v[i] << " ";
    }
    cerr << endl;
}

template<typename T> void debug(vector<vector<T>> &v){
    rep(i, v.size()){
        rep(j, v[i].size()){
            cerr << v[i][j] << " ";
        }
        cerr << endl;
    }
}

template<typename T> void debug(vector<pair<T, T>> &v){
    rep(i,v.size()){
        cerr << v[i].first << " " << v[i].second << endl;
    }
}

template<typename T> void debug(set<T> &st){
    for (auto itr = st.begin(); itr != st.end(); itr++){
        cerr << *itr << " ";
    }
    cerr << endl;
}

template<typename T> void debug(multiset<T> &ms){
    for (auto itr = ms.begin(); itr != ms.end(); itr++){
        cerr << *itr << " ";
    }
    cerr << endl;
}

template<typename T> void debug(map<T, T> &mp){
    for (auto itr = mp.begin(); itr != mp.end(); itr++){
        cerr << itr->first << " " << itr->second << endl;
    }
}

void debug_out(){
    cerr << endl;
}

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T){
    cerr << H << " ";
    debug_out(T...);
}



int main(){
    ll N,M;
    cin >> N >> M;
    vl H(N);
    rep(i,N) cin >> H[i];
    vl X(M);
    vl Y(M);
    rep(i,M){
        cin >> X[i] >> Y[i];
        X[i]--;
        Y[i]--;
    }
    vvl graph(N, vl(0));
    for (ll i = 0; i < M; i++){
        graph[X[i]].push_back(Y[i]);
        graph[Y[i]].push_back(X[i]);
    }
    ll INF = 1000000000000000;

    vl dp1(N, -INF);
    vl dp2(N, -INF);
    dp1[0] = 0;
    for (ll i = 0; i < N; i++){
        for (auto x: graph[i]){
            if (x <= i){
                continue;
            }
            if (H[x] > H[i]){
                dp2[x] = max(dp2[x], dp1[i] + abs(H[x] - H[i]));
            }
            else{
                dp1[x] = max(dp1[x], dp1[i]);
                dp1[x] = max(dp1[x], dp2[i]);
            }
        }
    }
    //debug(dp1);
    //debug(dp2);
    if (max(dp1[N - 1], dp2[N - 1]) < 0) cout << -1 << endl;
    else cout << max(dp1[N - 1], dp2[N - 1]) << endl;

    vl dp3(N, -INF);
    vl dp4(N, -INF);
    dp3[N - 1] = 0;
    for (ll i = N - 1; i >= 0; i--){
        for (auto x: graph[i]){
            if (x >= i){
                continue;
            }
            if (H[x] > H[i]){
                dp4[x] = max(dp4[x], dp3[i] + abs(H[x] - H[i]));
            }
            else{
                dp3[x] = max(dp3[x], dp3[i]);
                dp3[x] = max(dp3[x], dp4[i]);
            }
        }
    }
    if (max(dp3[0], dp4[0]) < 0) cout << -1 << endl;
    else cout << max(dp3[0], dp4[0]) << endl;
}
0