結果

問題 No.788 トラックの移動
ユーザー vwxyzvwxyz
提出日時 2022-03-24 04:46:44
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,426 bytes
コンパイル時間 281 ms
コンパイル使用メモリ 82,496 KB
実行使用メモリ 113,408 KB
最終ジャッジ日時 2024-04-20 14:14:30
合計ジャッジ時間 11,042 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,754 ms
113,392 KB
testcase_01 AC 40 ms
55,620 KB
testcase_02 AC 42 ms
55,132 KB
testcase_03 AC 45 ms
55,280 KB
testcase_04 AC 487 ms
86,180 KB
testcase_05 AC 1,711 ms
113,384 KB
testcase_06 AC 1,738 ms
113,408 KB
testcase_07 AC 42 ms
56,020 KB
testcase_08 AC 44 ms
56,908 KB
testcase_09 AC 40 ms
56,004 KB
testcase_10 AC 42 ms
56,792 KB
testcase_11 AC 41 ms
55,312 KB
testcase_12 AC 41 ms
56,048 KB
testcase_13 WA -
testcase_14 AC 45 ms
56,944 KB
testcase_15 AC 487 ms
110,028 KB
testcase_16 AC 1,716 ms
113,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline=sys.stdin.readline
import heapq

class Graph:
    def __init__(self,V,edges=False,graph=False,directed=False,weighted=False,inf=float("inf")):
        self.V=V
        self.directed=directed
        self.weighted=weighted
        self.inf=inf
        if not graph:
            self.edges=edges
            self.graph=[[] for i in range(self.V)]
            if weighted:
                for i,j,d in self.edges:
                    self.graph[i].append((j,d))
                    if not self.directed:
                        self.graph[j].append((i,d))
            else:
                for i,j in self.edges:
                    self.graph[i].append(j)
                    if not self.directed:
                        self.graph[j].append(i)
        else:
            self.graph=graph
            self.edges=[]
            for i in range(self.V):
                if self.weighted:
                    for j,d in self.graph[i]:
                        if self.directed or not self.directed and i<=j:
                            self.edges.append((i,j,d))
                else:
                    for j in self.graph[i]:
                        if self.directed or not self.directed and i<=j:
                            self.edges.append((i,j))

    def Dijkstra(self,s,route_restoration=False):
        dist=[self.inf]*self.V
        dist[s]=0
        hq=[(0,s)]
        if route_restoration:
            parents=[None]*self.V
        while hq:
            dx,x=heapq.heappop(hq)
            if dist[x]<dx:
                continue
            for y,dy in self.graph[x]:
                if dist[y]>dx+dy:
                    dist[y]=dx+dy
                    if route_restoration:
                        parents[y]=x
                    heapq.heappush(hq,(dist[y],y))
        if route_restoration:
            return dist,parents
        else:
            return dist

N,M,L=map(int,readline().split())
L-=1
T=list(map(int,readline().split()))
edges=[]
for _ in range(M):
    a,b,c=map(int,readline().split())
    a-=1;b-=1
    edges.append((a,b,c))
inf=1<<60
G=Graph(N,edges=edges,weighted=True,inf=inf)
dist=[G.Dijkstra(i) for i in range(N)]
ans=inf
idx=[j for j in range(N) if T[j]]
import random
for i in range(N):
    if random.randint(1,5)==1:
      continue
    s=sum(T[j]*dist[i][j] for j in range(N))*2
    if s:
        s+=min(dist[L][j]-dist[i][j] for j in idx)
    ans=min(ans,s)
print(ans)
0