結果

問題 No.788 トラックの移動
ユーザー brthyyjpbrthyyjp
提出日時 2022-01-13 08:50:04
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,636 ms / 2,000 ms
コード長 1,275 bytes
コンパイル時間 176 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 112,444 KB
最終ジャッジ日時 2024-04-28 02:56:57
合計ジャッジ時間 10,125 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,613 ms
111,688 KB
testcase_01 AC 33 ms
53,544 KB
testcase_02 AC 32 ms
54,120 KB
testcase_03 AC 37 ms
54,336 KB
testcase_04 AC 435 ms
85,312 KB
testcase_05 AC 1,554 ms
112,168 KB
testcase_06 AC 1,561 ms
111,780 KB
testcase_07 AC 34 ms
54,168 KB
testcase_08 AC 32 ms
53,348 KB
testcase_09 AC 32 ms
54,000 KB
testcase_10 AC 33 ms
53,484 KB
testcase_11 AC 32 ms
53,216 KB
testcase_12 AC 32 ms
54,040 KB
testcase_13 AC 32 ms
54,040 KB
testcase_14 AC 34 ms
53,668 KB
testcase_15 AC 464 ms
109,280 KB
testcase_16 AC 1,636 ms
112,444 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
INF = 1<<60
def dijkstra(s, edge):
    n = len(edge)
    dist = [INF]*n
    dist[s] = 0
    edgelist = []
    heapq.heappush(edgelist,(dist[s], s))
    while edgelist:
        minedge = heapq.heappop(edgelist)
        if dist[minedge[1]] < minedge[0]:
            continue
        v = minedge[1]
        for e in edge[v]:
            if dist[e[1]] > dist[v]+e[0]:
                dist[e[1]] = dist[v]+e[0]
                heapq.heappush(edgelist,(dist[e[1]], e[1]))
    return dist

import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

def main():

    n, m, l = map(int, input().split())
    l -= 1
    T = list(map(int, input().split()))
    g = [[] for i in range(n)]
    for i in range(m):
        a, b, c = map(int, input().split())
        a, b = a-1, b-1
        g[a].append((c, b))
        g[b].append((c, a))

    cnt = 0
    for i in range(n):
        if T[i] != 0:
            cnt += 1
    if cnt == 1:
        print(0)
        exit()

    D = [dijkstra(i, g) for i in range(n)]

    ans = INF
    for i in range(n):
        s=sum(T[j]*D[i][j] for j in range(n))*2
        if s:
            s+=min(D[l][j]-D[i][j] for j in range(n) if T[j])
        ans=min(ans,s)
    print(ans)

if __name__ == '__main__':
    main()
0