結果

問題 No.788 トラックの移動
コンテスト
ユーザー kohei2019
提出日時 2022-07-29 00:03:08
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 1,292 ms / 2,000 ms
コード長 1,305 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 138 ms
コンパイル使用メモリ 85,248 KB
実行使用メモリ 182,576 KB
最終ジャッジ日時 2026-04-02 08:22:11
合計ジャッジ時間 7,984 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import heapq
N,M,L = map(int,input().split())
L -= 1
lst = list(map(int,input().split()))
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))
if sum(lst) == 1:
    print(0)
    exit()
INF = float('INF')
distL = [INF]*(N)
used = [False]*(N)
distL[L] = 0
h = [(0,L)]
while h:
    c,x = heapq.heappop(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
            heapq.heappush(h, (distL[j],j))
ans = INF
for i in range(N):
    st = i
    distC = [INF]*(N)
    used = [False]*(N)
    distC[st] = 0
    h = [(0,st)]
    while h:
        c,x = heapq.heappop(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
                heapq.heappush(h, (distC[j],j))
    divmax = -INF
    sumcost = 0
    for k in range(N):
        sumcost += distC[k]*lst[k]*2
        if lst[k] >= 1:
            divmax = max(divmax,distC[k]-distL[k])
    ans = min(ans,sumcost-divmax)
print(ans)
    
            
0