結果

問題 No.2100 [Cherry Alpha C] Two-way Steps
ユーザー ikomaikoma
提出日時 2022-10-14 22:32:18
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 540 ms / 2,000 ms
コード長 1,116 bytes
コンパイル時間 1,208 ms
コンパイル使用メモリ 86,556 KB
実行使用メモリ 135,812 KB
最終ジャッジ日時 2023-09-08 22:40:57
合計ジャッジ時間 20,007 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
71,156 KB
testcase_01 AC 90 ms
71,384 KB
testcase_02 AC 91 ms
71,204 KB
testcase_03 AC 90 ms
71,644 KB
testcase_04 AC 455 ms
122,380 KB
testcase_05 AC 390 ms
110,928 KB
testcase_06 AC 189 ms
84,672 KB
testcase_07 AC 204 ms
87,032 KB
testcase_08 AC 448 ms
120,240 KB
testcase_09 AC 396 ms
112,272 KB
testcase_10 AC 270 ms
97,240 KB
testcase_11 AC 370 ms
111,772 KB
testcase_12 AC 316 ms
101,468 KB
testcase_13 AC 284 ms
103,064 KB
testcase_14 AC 325 ms
99,424 KB
testcase_15 AC 309 ms
106,388 KB
testcase_16 AC 331 ms
100,956 KB
testcase_17 AC 235 ms
95,956 KB
testcase_18 AC 315 ms
107,236 KB
testcase_19 AC 474 ms
125,596 KB
testcase_20 AC 223 ms
89,044 KB
testcase_21 AC 300 ms
99,780 KB
testcase_22 AC 338 ms
100,696 KB
testcase_23 AC 432 ms
118,508 KB
testcase_24 AC 536 ms
131,268 KB
testcase_25 AC 540 ms
131,224 KB
testcase_26 AC 521 ms
131,076 KB
testcase_27 AC 517 ms
132,304 KB
testcase_28 AC 517 ms
131,376 KB
testcase_29 AC 523 ms
131,612 KB
testcase_30 AC 531 ms
131,016 KB
testcase_31 AC 528 ms
131,716 KB
testcase_32 AC 529 ms
131,120 KB
testcase_33 AC 540 ms
135,812 KB
testcase_34 AC 386 ms
108,552 KB
testcase_35 AC 340 ms
106,196 KB
testcase_36 AC 338 ms
105,880 KB
testcase_37 AC 323 ms
106,008 KB
testcase_38 AC 353 ms
106,960 KB
testcase_39 AC 263 ms
105,116 KB
testcase_40 AC 270 ms
105,020 KB
testcase_41 AC 271 ms
105,080 KB
testcase_42 AC 269 ms
105,176 KB
testcase_43 AC 280 ms
104,884 KB
testcase_44 AC 256 ms
121,040 KB
testcase_45 AC 249 ms
121,160 KB
testcase_46 AC 255 ms
121,256 KB
testcase_47 AC 247 ms
121,144 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from collections import deque
N,M=map(int,input().split())
H=list(map(int,input().split()))
XY=[list(map(lambda x:int(x)-1,input().split())) for _ in range(M)]

edge1 = [[] for _ in range(N)]
edge2 = [[] for _ in range(N)]

ins1 = [0]*N
ins2 = [0]*N

for x,y in XY:
    edge1[x].append((y,max(0, H[y]-H[x])))
    ins1[y]+=1
    edge2[y].append((x,max(0, H[x]-H[y])))
    ins2[x]+=1
INF=10**60
def solve(edge, ins, start, goal):
    dp = [-INF]*N
    dp2 = [-INF]*N
    dp[start]=0

    que = deque()
    for i,j in enumerate(ins):
        if j==0:
            que.append(i)
    while que:
        cur = que.popleft()
        u = dp[cur]
        u2 = dp2[cur]
        for to,h in edge[cur]:
            if h>0:
                dp2[to] = max(dp2[to], u + h)
            else:
                dp[to] = max(dp[to], u2, u)
            ins[to]-=1
            if ins[to]==0:
                que.append(to)
    ans = max(dp[goal], dp2[goal])
    if ans<0:
        print(-1)
    else:
        print(ans)
    # print(dp)
    # print(dp2)

solve(edge1,ins1, 0, -1)
solve(edge2,ins2, -1, 0)
0