結果

問題 No.2100 [Cherry Alpha C] Two-way Steps
ユーザー kwm_tkwm_t
提出日時 2022-10-15 12:54:25
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 118 ms / 2,000 ms
コード長 2,196 bytes
コンパイル時間 2,244 ms
コンパイル使用メモリ 210,364 KB
実行使用メモリ 21,316 KB
最終ジャッジ日時 2023-09-09 02:28:58
合計ジャッジ時間 7,185 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 79 ms
16,156 KB
testcase_05 AC 61 ms
13,340 KB
testcase_06 AC 11 ms
5,604 KB
testcase_07 AC 14 ms
6,088 KB
testcase_08 AC 77 ms
18,312 KB
testcase_09 AC 59 ms
12,344 KB
testcase_10 AC 28 ms
8,836 KB
testcase_11 AC 55 ms
13,664 KB
testcase_12 AC 39 ms
10,048 KB
testcase_13 AC 45 ms
13,924 KB
testcase_14 AC 33 ms
8,780 KB
testcase_15 AC 48 ms
13,800 KB
testcase_16 AC 36 ms
9,296 KB
testcase_17 AC 32 ms
11,900 KB
testcase_18 AC 47 ms
14,120 KB
testcase_19 AC 89 ms
19,064 KB
testcase_20 AC 20 ms
6,684 KB
testcase_21 AC 39 ms
10,004 KB
testcase_22 AC 36 ms
9,060 KB
testcase_23 AC 80 ms
16,244 KB
testcase_24 AC 106 ms
21,260 KB
testcase_25 AC 106 ms
21,316 KB
testcase_26 AC 109 ms
21,136 KB
testcase_27 AC 110 ms
21,140 KB
testcase_28 AC 113 ms
21,248 KB
testcase_29 AC 110 ms
21,244 KB
testcase_30 AC 111 ms
21,144 KB
testcase_31 AC 114 ms
21,228 KB
testcase_32 AC 118 ms
21,000 KB
testcase_33 AC 117 ms
21,072 KB
testcase_34 AC 50 ms
11,524 KB
testcase_35 AC 45 ms
11,732 KB
testcase_36 AC 44 ms
11,820 KB
testcase_37 AC 42 ms
11,552 KB
testcase_38 AC 48 ms
11,816 KB
testcase_39 AC 33 ms
10,308 KB
testcase_40 AC 33 ms
10,380 KB
testcase_41 AC 34 ms
10,628 KB
testcase_42 AC 34 ms
10,372 KB
testcase_43 AC 34 ms
10,548 KB
testcase_44 AC 61 ms
19,992 KB
testcase_45 AC 60 ms
19,792 KB
testcase_46 AC 61 ms
19,936 KB
testcase_47 AC 60 ms
19,928 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
//using mint = modint1000000007;
//const int mod = 1000000007;
//using mint = modint998244353;
//const int mod = 998244353;
//const int INF = 1e9;
const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n-1); i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define endl "\n"
#define P pair<long long,int>
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
struct Edge {
	int to;
	long long cost;
	Edge(int _to, long long _cost) :to(_to), cost(_cost) {}
};

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n, m; cin >> n >> m;
	vector<int>h(n);
	vector<vector<Edge>>g0(n), g1(n);
	rep(i, n)cin >> h[i];
	rep(i, m) {
		int x, y; cin >> x >> y; x--, y--;
		if (x > y)swap(x, y);
		g0[x].emplace_back(y, max(0, h[y] - h[x]));
		g1[y].emplace_back(x, max(0, h[x] - h[y]));
	}
	{
		vector<vector<long long>> dp(n, vector<long long>(2, -1));
		dp[0][1] = 0;
		rep(i, n)rep(j, 2) {
			if (-1 == dp[i][j])continue;
			for (Edge e : g0[i]) {
				long long nval = dp[i][j] + e.cost;
				int npos = e.to;
				if (0 == j) {
					if (e.cost <= 0) chmax(dp[npos][1], nval);
				}
				else {
					if (e.cost > 0) chmax(dp[npos][0], nval);
					else chmax(dp[npos][1], nval);
				}
			}
		}
		cout << max(dp[n - 1][0], dp[n - 1][1]) << endl;
	}
	{
		vector<vector<long long>> dp(n, vector<long long>(2, -1));
		dp[n - 1][1] = 0;
		rrep(i, n)rep(j, 2) {
			if (-1 == dp[i][j])continue;
			for (Edge e : g1[i]) {
				long long nval = dp[i][j] + e.cost;
				int npos = e.to;
				if (0 == j) {
					if (e.cost <= 0) chmax(dp[npos][1], nval);
				}
				else {
					if (e.cost > 0) chmax(dp[npos][0], nval);
					else chmax(dp[npos][1], nval);
				}
			}
		}
		cout << max(dp[0][0], dp[0][1]) << endl;
	}
	return 0;
}
0