結果

問題 No.1393 Median of Walk
ユーザー chineristACchineristAC
提出日時 2020-11-13 00:30:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 438 ms / 2,000 ms
コード長 1,505 bytes
コンパイル時間 440 ms
コンパイル使用メモリ 87,040 KB
実行使用メモリ 80,624 KB
最終ジャッジ日時 2023-09-27 04:53:30
合計ジャッジ時間 12,125 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
71,396 KB
testcase_01 AC 60 ms
71,348 KB
testcase_02 AC 60 ms
71,436 KB
testcase_03 AC 67 ms
75,200 KB
testcase_04 AC 81 ms
76,268 KB
testcase_05 AC 78 ms
76,140 KB
testcase_06 AC 71 ms
75,972 KB
testcase_07 AC 66 ms
71,364 KB
testcase_08 AC 199 ms
79,208 KB
testcase_09 AC 189 ms
78,656 KB
testcase_10 AC 189 ms
79,048 KB
testcase_11 AC 420 ms
79,156 KB
testcase_12 AC 391 ms
79,304 KB
testcase_13 AC 303 ms
78,836 KB
testcase_14 AC 401 ms
80,624 KB
testcase_15 AC 362 ms
79,072 KB
testcase_16 AC 423 ms
78,916 KB
testcase_17 AC 341 ms
78,888 KB
testcase_18 AC 427 ms
78,520 KB
testcase_19 AC 434 ms
78,656 KB
testcase_20 AC 423 ms
79,912 KB
testcase_21 AC 320 ms
79,540 KB
testcase_22 AC 438 ms
79,512 KB
testcase_23 AC 415 ms
78,844 KB
testcase_24 AC 390 ms
78,436 KB
testcase_25 AC 296 ms
79,124 KB
testcase_26 AC 227 ms
79,012 KB
testcase_27 AC 238 ms
79,084 KB
testcase_28 AC 80 ms
75,500 KB
testcase_29 AC 85 ms
75,528 KB
testcase_30 AC 189 ms
77,564 KB
testcase_31 AC 200 ms
77,224 KB
testcase_32 AC 281 ms
78,708 KB
testcase_33 AC 237 ms
78,392 KB
testcase_34 AC 412 ms
79,048 KB
testcase_35 AC 366 ms
78,704 KB
testcase_36 AC 318 ms
78,672 KB
testcase_37 AC 60 ms
71,288 KB
testcase_38 AC 226 ms
77,804 KB
testcase_39 AC 235 ms
77,408 KB
testcase_40 AC 173 ms
77,468 KB
testcase_41 AC 160 ms
78,740 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n,m = map(int,input().split())

assert 2<=n<=1000
assert 1<=m<=min(n*(n-1)//2,2500)

edge = [[] for i in range(n)]
edge_cost = {}
edge_weight = []

check = set([])

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))

    assert 1<=u<=n
    assert 1<=v<=n
    assert 1<=w<=10**9
    assert (u,v) not in check
    assert u!=v
    check.add((u,v))

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