結果

問題 No.909 たぴの配置
ユーザー vwxyzvwxyz
提出日時 2023-07-15 05:43:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,042 ms / 3,000 ms
コード長 2,261 bytes
コンパイル時間 248 ms
コンパイル使用メモリ 86,812 KB
実行使用メモリ 257,880 KB
最終ジャッジ日時 2023-10-14 20:18:36
合計ジャッジ時間 14,954 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,036 KB
testcase_01 AC 70 ms
71,104 KB
testcase_02 AC 67 ms
71,212 KB
testcase_03 AC 69 ms
71,136 KB
testcase_04 AC 70 ms
71,232 KB
testcase_05 AC 1,042 ms
253,496 KB
testcase_06 AC 1,020 ms
257,880 KB
testcase_07 AC 909 ms
246,972 KB
testcase_08 AC 848 ms
247,208 KB
testcase_09 AC 855 ms
250,972 KB
testcase_10 AC 781 ms
238,380 KB
testcase_11 AC 736 ms
234,992 KB
testcase_12 AC 909 ms
253,732 KB
testcase_13 AC 976 ms
255,508 KB
testcase_14 AC 682 ms
236,460 KB
testcase_15 AC 687 ms
234,184 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline=sys.stdin.readline
import heapq

class Graph:
    def __init__(self,V,edges=None,graph=None,directed=False,weighted=False,inf=float("inf")):
        self.V=V
        self.directed=directed
        self.weighted=weighted
        self.inf=inf
        if graph!=None:
            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))
        else:
            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)

    def Dijkstra(self,s,route_restoration=False):
        dist=[self.inf]*self.V
        dist[s]=0
        queue=[(0,s)]
        if route_restoration:
            parents=[None]*self.V
        while queue:
            dx,x=heapq.heappop(queue)
            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(queue,(dist[y],y))
        if route_restoration:
            return dist,parents
        else:
            return dist

N=int(readline())
X=list(map(int,readline().split()))
Y=list(map(int,readline().split()))
edges=[]
for i in range(N):
    edges.append((0,i+1,X[i]))
    edges.append((i+1,0,X[i]))
    edges.append((N+1,i+1,Y[i]))
    edges.append((i+1,N+1,Y[i]))
G=Graph(N+2,edges=edges,weighted=True,directed=True)
dist=G.Dijkstra(0)
print(dist[N+1])
print(*dist,sep="\n")
0