結果

問題 No.2100 [Cherry Alpha C] Two-way Steps
ユーザー wanuiwanui
提出日時 2022-10-15 00:36:11
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 85 ms / 2,000 ms
コード長 2,662 bytes
コンパイル時間 2,712 ms
コンパイル使用メモリ 205,588 KB
実行使用メモリ 20,864 KB
最終ジャッジ日時 2024-06-26 18:33:07
合計ジャッジ時間 6,679 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 48
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
// clang-format off
using namespace std; using ll=long long; using ull=unsigned long long; using pll=pair<ll,ll>; const ll INF=4e18;
void print0(){}; template<typename H,typename... T> void print0(H h,T... t){cout<<h;print0(t...);}
void print(){print0("\n");}; template<typename H,typename... T>void print(H h,T... t){print0(h);if(sizeof...(T)>0)print0(" ");print(t...);}
void perr0(){}; template<typename H,typename... T> void perr0(H h,T... t){cerr<<h;perr0(t...);}
void perr(){perr0("\n");}; template<typename H,typename... T>void perr(H h,T... t){perr0(h);if(sizeof...(T)>0)perr0(" ");perr(t...);}
void ioinit() { cout<<fixed<<setprecision(15); cerr<<fixed<<setprecision(6); ios_base::sync_with_stdio(0); cin.tie(0); }
#define debug1(a) { cerr<<#a<<":"<<a<<endl; }
#define debug2(a,b) { cerr<<#a<<":"<<a<<" "<<#b<<":"<<b<<endl; }
#define debug3(a,b,c) { cerr<<#a<<":"<<a<<" "<<#b<<":"<<b<<" "<<#c<<":"<<c<<endl; }
#define debug4(a,b,c,d) { cerr<<#a<<":"<<a<<" "<<#b<<":"<<b<<" "<<#c<<":"<<c<<" "<<#d<<":"<<d<<endl; }
// clang-format on

const ll V = 100005;
vector<vector<ll>> edgmap(V);
vector<ll> height(V);
ll N;

ll memo1[V][2];
bool done1[V][2];
ll dp1(ll u, ll up) {
    if (done1[u][up]) return memo1[u][up];
    done1[u][up] = true;

    if (u == N) {
        return memo1[u][up] = 0;
    }

    ll ans = -INF;
    for (auto v : edgmap[u]) {
        if (u > v) continue;
        if (up && height[v] > height[u]) continue;
        if (height[v] > height[u]) {
            ll diff = height[v] - height[u];
            ans = max(ans, diff + dp1(v, 1));
        } else {
            ans = max(ans, dp1(v, 0));
        }
    }
    return memo1[u][up] = ans;
}
ll memo2[V][2];
bool done2[V][2];
ll dp2(ll u, ll up) {
    if (done2[u][up]) return memo2[u][up];
    done2[u][up] = true;

    if (u == 1) {
        return memo2[u][up] = 0;
    }

    ll ans = -INF;
    for (auto v : edgmap[u]) {
        if (u < v) continue;
        if (up && height[v] > height[u]) continue;
        if (height[v] > height[u]) {
            ll diff = height[v] - height[u];
            ans = max(ans, diff + dp2(v, 1));
        } else {
            ans = max(ans, dp2(v, 0));
        }
    }
    return memo2[u][up] = ans;
}

int main() {
    ioinit();
    ll M;
    cin >> N >> M;
    for (ll i = 1; i <= N; i++) {
        cin >> height[i];
    }
    for (ll i = 0; i < M; i++) {
        ll x, y;
        cin >> x >> y;
        edgmap[x].push_back(y);
        edgmap[y].push_back(x);
    }
    ll a1 = dp1(1, 0);
    if (a1 < -INF / 2) a1 = -1;
    print(a1);

    ll a2 = dp2(N, 0);
    if (a2 < -INF / 2) a2 = -1;
    print(a2);

    return 0;
}
0