結果
問題 | No.2100 [Cherry Alpha C] Two-way Steps |
ユーザー |
|
提出日時 | 2022-10-14 23:02:00 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 196 ms / 2,000 ms |
コード長 | 1,893 bytes |
コンパイル時間 | 2,321 ms |
コンパイル使用メモリ | 174,496 KB |
実行使用メモリ | 22,016 KB |
最終ジャッジ日時 | 2024-06-26 16:50:15 |
合計ジャッジ時間 | 8,634 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 48 |
ソースコード
#include <bits/stdc++.h>using namespace std;using ll = long long;using pll = pair<ll, ll>;#define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i)#define rep(i, n) drep(i, 0, n - 1)#define all(a) (a).begin(), (a).end()#define pb push_back#define fi first#define se secondconst ll MOD = 1000000007;const ll MOD2 = 998244353;const ll INF = 1LL << 60;const ll MAX_N = 2e5;int main(){ll n, m;cin >> n >> m;vector<ll> h(n);rep(i, n) cin >> h[i];vector<vector<ll>> G(n);rep(i, m){ll u, v;cin >> u >> v;u--; v--;G[u].pb(v);G[v].pb(u);}// cherryvector<vector<ll>> cost1(n, vector<ll>(2, -INF));cost1[0][0] = 0;for(ll v=1; v<n; v++){for(ll u : G[v]){if(v < u) continue;if(h[u] < h[v]){if(cost1[u][0] != -INF) cost1[v][1] = max(cost1[v][1], cost1[u][0]+(h[v]-h[u]));}else{if(cost1[u][0] != -INF) cost1[v][0] = max(cost1[v][0], cost1[u][0]);if(cost1[u][1] != -INF) cost1[v][0] = max(cost1[v][0], cost1[u][1]);}}}vector<vector<ll>> cost2(n, vector<ll>(2, -INF));cost2[n-1][0] = 0;for(ll v=n-2; v>=0; v--){for(ll u : G[v]){if(u < v) continue;if(h[u] < h[v]){if(cost2[u][0] != -INF) cost2[v][1] = max(cost2[v][1], cost2[u][0]+(h[v]-h[u]));}else{if(cost2[u][0] != -INF) cost2[v][0] = max(cost2[v][0], cost2[u][0]);if(cost2[u][1] != -INF) cost2[v][0] = max(cost2[v][0], cost2[u][1]);}}}ll che = max(cost1[n-1][0], cost1[n-1][1]);ll zer = max(cost2[0][0], cost2[0][1]);if(che == -INF) cout << -1 << endl;else cout << che << endl;if(zer == -INF) cout << -1 << endl;else cout << zer << endl;}