結果

問題 No.788 トラックの移動
ユーザー kohei2019
提出日時 2022-07-29 00:18:47
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,803 ms / 2,000 ms
コード長 1,360 bytes
コンパイル時間 186 ms
コンパイル使用メモリ 82,240 KB
実行使用メモリ 87,964 KB
最終ジャッジ日時 2024-07-18 14:54:28
合計ジャッジ時間 10,304 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
import sys
input = sys.stdin.readline
hpop = heapq.heappop
hpush = heapq.heappush
N,M,L = map(int,input().split())
L -= 1
lst = list(map(int,input().split()))
if sum(lst) == 1:
    print(0)
    exit()
lsg = [[] for i in range(N)]
for i in range(M):
    a,b,c = map(int,input().split())
    a -= 1
    b -= 1
    lsg[a].append((b,c))
    lsg[b].append((a,c))
INF = 10**10
distL = [INF]*(N)
used = [False]*(N)
distL[L] = 0
h = [(0,L)]
while h:
    c,x = hpop(h)
    if used[x]:
        continue
    used[x] = True
    for j,cos in lsg[x]:
        if used[j]:
            continue
        if distL[j] > distL[x]+cos:
            distL[j] = distL[x]+cos
            hpush(h, (distL[j],j))
ans = 10**18
for i in range(N):
    distC = [INF]*(N)
    used = [False]*(N)
    distC[i] = 0
    h = [(0,i)]
    while h:
        c,x = hpop(h)
        if used[x]:
            continue
        used[x] = True
        for j,cos in lsg[x]:
            if used[j]:
                continue
            if distC[j] > distC[x]+cos:
                distC[j] = distC[x]+cos
                hpush(h, (distC[j],j))
    divmax = -INF
    sumcost = 0
    for k in range(N):
        sumcost += distC[k]*lst[k]*2
        if lst[k] >= 1 and divmax<(distC[k]-distL[k]):
            divmax = distC[k]-distL[k]
    if ans>(sumcost-divmax):
        ans = sumcost-divmax
print(ans)
0