結果

問題 No.807 umg tours
ユーザー vwxyzvwxyz
提出日時 2021-12-25 02:18:58
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,787 ms / 4,000 ms
コード長 2,462 bytes
コンパイル時間 1,054 ms
コンパイル使用メモリ 81,080 KB
実行使用メモリ 361,372 KB
最終ジャッジ日時 2023-10-20 06:04:30
合計ジャッジ時間 29,725 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
65,304 KB
testcase_01 AC 62 ms
65,220 KB
testcase_02 AC 64 ms
66,624 KB
testcase_03 AC 60 ms
64,416 KB
testcase_04 AC 55 ms
62,052 KB
testcase_05 AC 59 ms
62,360 KB
testcase_06 AC 61 ms
64,424 KB
testcase_07 AC 58 ms
63,852 KB
testcase_08 AC 43 ms
52,984 KB
testcase_09 AC 44 ms
53,416 KB
testcase_10 AC 50 ms
59,668 KB
testcase_11 AC 1,790 ms
307,160 KB
testcase_12 AC 1,429 ms
253,732 KB
testcase_13 AC 2,005 ms
308,660 KB
testcase_14 AC 1,000 ms
182,604 KB
testcase_15 AC 729 ms
160,212 KB
testcase_16 AC 2,097 ms
327,764 KB
testcase_17 AC 2,787 ms
358,656 KB
testcase_18 AC 2,774 ms
361,372 KB
testcase_19 AC 2,552 ms
358,052 KB
testcase_20 AC 1,157 ms
238,108 KB
testcase_21 AC 1,252 ms
245,664 KB
testcase_22 AC 562 ms
144,628 KB
testcase_23 AC 483 ms
132,228 KB
testcase_24 AC 1,211 ms
312,472 KB
testcase_25 AC 2,669 ms
361,168 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=map(int,readline().split())
edges0=[]
edges1=[]
for _ in range(M):
    a,b,c=map(int,readline().split())
    a-=1;b-=1
    edges0.append((a,b,c))
    edges1.append((a,b,c))
    edges1.append((b,a,c))
    edges1.append((a+N,b+N,c))
    edges1.append((b+N,a+N,c))
    edges1.append((a,b+N,0))
    edges1.append((b,a+N,0))
for i in range(N):
    edges1.append((i,i+N,0))
G0=Graph(N,edges=edges0,weighted=True)
G1=Graph(2*N,edges=edges1,weighted=True,directed=True)
dist0=G0.Dijkstra(0)
dist1=G1.Dijkstra(0)
for i in range(N):
    ans=dist0[i]+dist1[i+N]
    print(ans)
0