結果

問題 No.1812 Uribo Road
ユーザー vwxyzvwxyz
提出日時 2023-02-24 07:13:02
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 996 ms / 5,000 ms
コード長 3,251 bytes
コンパイル時間 495 ms
コンパイル使用メモリ 82,312 KB
実行使用メモリ 95,564 KB
最終ジャッジ日時 2024-07-23 21:59:40
合計ジャッジ時間 12,193 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
53,248 KB
testcase_01 AC 43 ms
53,120 KB
testcase_02 AC 53 ms
63,872 KB
testcase_03 AC 100 ms
77,440 KB
testcase_04 AC 42 ms
53,632 KB
testcase_05 AC 43 ms
53,632 KB
testcase_06 AC 44 ms
53,888 KB
testcase_07 AC 80 ms
76,400 KB
testcase_08 AC 154 ms
77,664 KB
testcase_09 AC 200 ms
78,492 KB
testcase_10 AC 135 ms
77,920 KB
testcase_11 AC 163 ms
78,376 KB
testcase_12 AC 483 ms
84,748 KB
testcase_13 AC 208 ms
81,668 KB
testcase_14 AC 217 ms
82,164 KB
testcase_15 AC 526 ms
86,616 KB
testcase_16 AC 403 ms
83,088 KB
testcase_17 AC 875 ms
92,456 KB
testcase_18 AC 244 ms
83,272 KB
testcase_19 AC 996 ms
95,440 KB
testcase_20 AC 989 ms
95,564 KB
testcase_21 AC 780 ms
91,584 KB
testcase_22 AC 549 ms
88,336 KB
testcase_23 AC 146 ms
77,384 KB
testcase_24 AC 51 ms
61,696 KB
testcase_25 AC 363 ms
84,076 KB
testcase_26 AC 137 ms
78,380 KB
testcase_27 AC 239 ms
82,284 KB
testcase_28 AC 564 ms
86,432 KB
testcase_29 AC 43 ms
53,632 KB
testcase_30 AC 168 ms
79,660 KB
testcase_31 AC 623 ms
86,988 KB
testcase_32 AC 115 ms
76,752 KB
testcase_33 AC 419 ms
86,432 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