結果

問題 No.1812 Uribo Road
ユーザー vwxyzvwxyz
提出日時 2023-02-24 07:13:02
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,070 ms / 5,000 ms
コード長 3,251 bytes
コンパイル時間 1,515 ms
コンパイル使用メモリ 86,624 KB
実行使用メモリ 97,488 KB
最終ジャッジ日時 2023-10-01 04:37:16
合計ジャッジ時間 14,835 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
71,288 KB
testcase_01 AC 75 ms
71,064 KB
testcase_02 AC 89 ms
76,288 KB
testcase_03 AC 131 ms
78,412 KB
testcase_04 AC 76 ms
71,172 KB
testcase_05 AC 76 ms
71,200 KB
testcase_06 AC 78 ms
71,140 KB
testcase_07 AC 111 ms
77,272 KB
testcase_08 AC 194 ms
79,508 KB
testcase_09 AC 235 ms
80,020 KB
testcase_10 AC 164 ms
78,800 KB
testcase_11 AC 194 ms
79,604 KB
testcase_12 AC 531 ms
86,452 KB
testcase_13 AC 248 ms
83,248 KB
testcase_14 AC 264 ms
82,720 KB
testcase_15 AC 560 ms
87,904 KB
testcase_16 AC 440 ms
84,800 KB
testcase_17 AC 936 ms
94,396 KB
testcase_18 AC 272 ms
83,936 KB
testcase_19 AC 1,026 ms
96,744 KB
testcase_20 AC 1,070 ms
97,488 KB
testcase_21 AC 817 ms
93,864 KB
testcase_22 AC 576 ms
90,292 KB
testcase_23 AC 178 ms
78,588 KB
testcase_24 AC 85 ms
75,644 KB
testcase_25 AC 396 ms
85,468 KB
testcase_26 AC 169 ms
79,052 KB
testcase_27 AC 276 ms
83,240 KB
testcase_28 AC 608 ms
88,764 KB
testcase_29 AC 76 ms
71,136 KB
testcase_30 AC 198 ms
79,936 KB
testcase_31 AC 672 ms
88,652 KB
testcase_32 AC 149 ms
78,820 KB
testcase_33 AC 448 ms
87,492 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 graph:
            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,M,K=map(int,readline().split())
R=list(map(int,readline().split()))
for k in range(K):
    R[k]-=1
edges=[]
inf=1<<60
for m in range(M):
    A,B,C=map(int,readline().split())
    A-=1;B-=1
    edges.append((A,B,C))
G=Graph(N,edges=edges,weighted=True)
dist=[[None]*2*K for x in range(2*K)]
for i in range(K):
    D=G.Dijkstra(edges[R[i]][0])
    for j in range(K):
        dist[2*i][2*j]=D[edges[R[j]][1]]+edges[R[j]][2]
        dist[2*i][2*j+1]=D[edges[R[j]][0]]+edges[R[j]][2]
    D=G.Dijkstra(edges[R[i]][1])
    for j in range(K):
        dist[2*i+1][2*j]=D[edges[R[j]][1]]+edges[R[j]][2]
        dist[2*i+1][2*j+1]=D[edges[R[j]][0]]+edges[R[j]][2]
dp=[[inf]*2*K for bit in range(1<<K)]
D=G.Dijkstra(0)
for k in range(K):
    dp[1<<k][2*k]=D[edges[R[k]][1]]+edges[R[k]][2]
    dp[1<<k][2*k+1]=D[edges[R[k]][0]]+edges[R[k]][2]
for bit in range(1<<K):
    for i in range(K):
        if not bit&1<<i:
            continue
        for j in range(K):
            if i==j:
                continue
            if not bit&1<<j:
                continue
            for ii in (2*i,2*i+1):
                for jj in (2*j,2*j+1):
                    dp[bit][ii]=min(dp[bit][ii],dp[bit^1<<i][jj]+dist[jj][ii])
D=G.Dijkstra(N-1)
ans=min(min(dp[-1][2*i]+D[edges[R[i]][0]] for i in range(K)),min(dp[-1][2*i+1]+D[edges[R[i]][1]] for i in range(K)))
print(ans)
0