結果
| 問題 |
No.2100 [Cherry Alpha C] Two-way Steps
|
| コンテスト | |
| ユーザー |
momoyuu
|
| 提出日時 | 2022-10-14 23:55:39 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 186 ms / 2,000 ms |
| コード長 | 2,848 bytes |
| コンパイル時間 | 2,991 ms |
| コンパイル使用メモリ | 257,608 KB |
| 実行使用メモリ | 17,536 KB |
| 最終ジャッジ日時 | 2024-06-26 18:04:32 |
| 合計ジャッジ時間 | 9,618 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 48 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
template<class t> using vc = vector<t>;
template<class t> using vvc = vc<vc<t>>;
using pi = pair<int,int>;
using pl = pair<ll,ll>;
using vi = vc<int>;
using vvi = vvc<int>;
using vl = vc<ll>;
using vvl = vvc<ll>;
#define rep(i,a,b) for (int i = (int)(a); i < (int)(b); i++)
#define irep(i,a,b) for (int i = (int)(a); i > (int)(b); i--)
#define all(a) a.begin(),a.end()
#define print(n) cout << n << '\n'
#define pritn(n) print(n)
#define printv(n,a) {copy(all(n),ostream_iterator<a>(cout," ")); cout<<"\n";}
#define printvv(n,a) {for(auto itr:n) printv(itr,a);}
#define rup(a,b) (a+b-1)/b
#define input(A,N) rep(i,0,N) cin>>A[i]
#define chmax(a,b) a = max(a,b)
#define chmin(a,b) a = min(a,b)
template< typename T >
struct edge{
int from,to;
T cost;
/*
*to:繋がってる先
*cost:辺のコスト
*/
edge(int from,int to, T cost):from(from),to(to),cost(cost){};
edge &operator=(const int& x){
to = x;
return *this;
}
};
template< typename T >
struct graph{
int n;
vector<vector<edge<T>>> e;
graph(int n):n(n),e(n){}
void addedge(int from,int to,T cost){
e[from].emplace_back(from,to,cost);
}
vector<edge<T>> &operator[](int i) {
return e[i];
}
};
int main(){
cout << fixed << setprecision(15);
int n,m;
cin>>n>>m;
vl h(n);
input(h,n);
graph<int> g(n);
rep(i,0,m){
int u,v;
cin>>u>>v;
u--;v--;
g.addedge(u,v,1);
g.addedge(v,u,1);
}
vvc<ll> dp(n,vc<ll>(2,-1));
dp[0][0] = 0;
dp[0][1] = 0;
for(int i = 0;i<n-1;i++){
for(auto e:g[i]){
if(i>=e.to) continue;
int ni = e.to;
ll d = h[ni] - h[i];
if(d<0){
if(dp[i][0]!=-1) chmax(dp[ni][1],dp[i][0]);
if(dp[i][1]!=-1) chmax(dp[ni][1],dp[i][1]);
}else{
if(dp[i][1] == -1) continue;
chmax(dp[ni][0],dp[i][1]+d);
}
}
}
ll a = max(dp[n-1][0],dp[n-1][1]);
for(int i = 0;i<n;i++){
for(int j = 0;j<2;j++) dp[i][j] = -1;
}
dp[n-1][0] = 0;
dp[n-1][1] = 0;
for(int i = n-1;i>=1;i--){
for(auto e:g[i]){
if(i<=e.to) continue;
int ni = e.to;
ll d = h[ni] - h[i];
if(d<0){
if(dp[i][0]!=-1) chmax(dp[ni][1],dp[i][0]);
if(dp[i][1]!=-1) chmax(dp[ni][1],dp[i][1]);
}else{
if(dp[i][1] == -1) continue;
chmax(dp[ni][0],dp[i][1]+d);
}
}
}
ll b = max(dp[0][0],dp[0][1]);
//printvv(dp,ll);
print(a);
print(b);
//system("pause");
return 0;
}
momoyuu