結果

問題 No.1393 Median of Walk
ユーザー chineristACchineristAC
提出日時 2020-11-12 22:54:34
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 421 ms / 2,000 ms
コード長 1,304 bytes
コンパイル時間 358 ms
コンパイル使用メモリ 87,100 KB
実行使用メモリ 80,168 KB
最終ジャッジ日時 2023-09-27 04:48:02
合計ジャッジ時間 13,539 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
71,428 KB
testcase_01 AC 63 ms
71,584 KB
testcase_02 AC 62 ms
71,464 KB
testcase_03 AC 65 ms
75,180 KB
testcase_04 AC 83 ms
76,648 KB
testcase_05 AC 74 ms
76,344 KB
testcase_06 AC 72 ms
76,064 KB
testcase_07 AC 64 ms
71,400 KB
testcase_08 AC 193 ms
78,768 KB
testcase_09 AC 186 ms
78,824 KB
testcase_10 AC 185 ms
78,804 KB
testcase_11 AC 416 ms
78,748 KB
testcase_12 AC 351 ms
78,828 KB
testcase_13 AC 293 ms
78,704 KB
testcase_14 AC 413 ms
80,168 KB
testcase_15 AC 358 ms
78,840 KB
testcase_16 AC 414 ms
78,748 KB
testcase_17 AC 344 ms
78,628 KB
testcase_18 AC 414 ms
78,656 KB
testcase_19 AC 421 ms
78,664 KB
testcase_20 AC 418 ms
79,692 KB
testcase_21 AC 313 ms
78,732 KB
testcase_22 AC 421 ms
79,592 KB
testcase_23 AC 400 ms
79,024 KB
testcase_24 AC 386 ms
78,664 KB
testcase_25 AC 297 ms
79,796 KB
testcase_26 AC 216 ms
78,964 KB
testcase_27 AC 225 ms
79,196 KB
testcase_28 AC 78 ms
75,744 KB
testcase_29 AC 85 ms
75,716 KB
testcase_30 AC 195 ms
77,224 KB
testcase_31 AC 193 ms
77,384 KB
testcase_32 AC 276 ms
78,648 KB
testcase_33 AC 240 ms
78,452 KB
testcase_34 AC 392 ms
79,000 KB
testcase_35 AC 358 ms
78,492 KB
testcase_36 AC 294 ms
78,768 KB
testcase_37 AC 61 ms
71,568 KB
testcase_38 AC 254 ms
77,208 KB
testcase_39 AC 233 ms
77,572 KB
testcase_40 AC 177 ms
77,812 KB
testcase_41 AC 157 ms
78,320 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n,m = map(int,input().split())
edge = [[] for i in range(n)]
edge_cost = {}
edge_weight = []
for i in range(m):
    u,v,w = map(int,input().split())
    edge[u-1].append(v-1)
    edge_cost[u-1,v-1] = 1
    edge_weight.append((u-1,v-1,w))

dist = [10**18 for i in range(n)]
ans = [-1 for i in range(n)]
dist[0] = 0

def dfs(s,w):
    que = [s]
    while que:
        v = que.pop()
        for nv in edge[v]:
            if dist[v]!=-10**18:
                if dist[nv] > dist[v] + edge_cost[v,nv]:
                    dist[nv] = dist[v] + edge_cost[v,nv]
                    if dist[nv] < -n:
                        dist[nv] = -10**18
                    if dist[nv]<=0 and ans[nv]==-1:
                        ans[nv] = w
                    que.append(nv)
            else:
                if dist[nv]!=-10**18:
                    dist[nv] = -10**18
                    if ans[nv]==-1:
                        ans[nv] = w
                    que.append(nv)

dfs(0,-1)

edge_weight.sort(key = lambda x:x[2])
for u,v,w in edge_weight:
    edge_cost[u,v] = -1
    if dist[v] > dist[u] + edge_cost[u,v]:
        dist[v] = dist[u] + edge_cost[u,v]
        if dist[v] < -n:
            dist[v] = -10**18
        if dist[v]<=0 and ans[v]==-1:
            ans[v] = w
        dfs(v,w)

print(*ans[1:],sep="\n")
0