結果

問題 No.2100 [Cherry Alpha C] Two-way Steps
ユーザー Manuel1024Manuel1024
提出日時 2022-12-11 08:35:27
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 177 ms / 2,000 ms
コード長 1,284 bytes
コンパイル時間 755 ms
コンパイル使用メモリ 81,176 KB
実行使用メモリ 13,440 KB
最終ジャッジ日時 2024-10-15 06:33:39
合計ジャッジ時間 8,717 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 48
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;

int main(){
    int n, m; cin >> n >> m;
    vector<int> h(n);
    for(auto &it: h) cin >> it;
    vector<vector<int>> G(n);
    for(int i = 0; i < m; i++){
        int x, y; cin >> x >> y;
        x--; y--;
        G[x].emplace_back(y);
        G[y].emplace_back(x);
    }

    vector<vector<ll>> dp1(2, vector<ll>(n, -(1LL << 60)));
    dp1[0][0] = 0;
    for(int i = 0; i < n; i++){
        for(auto &nex: G[i]){
            if(nex < i) continue;
            if(h[i] < h[nex]) dp1[1][nex] = max(dp1[1][nex], dp1[0][i]+h[nex]-h[i]);
            else dp1[0][nex] = max(dp1[0][nex], max(dp1[0][i], dp1[1][i]));
        }
    }
    cout << (max(dp1[0][n-1], dp1[1][n-1]) >= 0 ? max(dp1[0][n-1], dp1[1][n-1]) : -1) << endl;
    vector<vector<ll>> dp2(2, vector<ll>(n, -(1LL << 60)));
    dp2[0][n-1] = 0;
    for(int i = n-1; i >= 0; i--){
        for(auto &nex: G[i]){
            if(nex > i) continue;
            if(h[i] < h[nex]) dp2[1][nex] = max(dp2[1][nex], dp2[0][i]+h[nex]-h[i]);
            else dp2[0][nex] = max(dp2[0][nex], max(dp2[0][i], dp2[1][i]));
        }
    }
    cout << (max(dp2[0][0], dp2[1][0]) >= 0 ? max(dp2[0][0], dp2[1][0]) : -1) << endl;
    return 0;
}
0