結果

問題 No.1812 Uribo Road
ユーザー vwxyzvwxyz
提出日時 2023-02-24 07:13:14
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,362 ms / 5,000 ms
コード長 3,251 bytes
コンパイル時間 258 ms
コンパイル使用メモリ 11,324 KB
実行使用メモリ 19,420 KB
最終ジャッジ日時 2023-10-01 04:37:31
合計ジャッジ時間 15,094 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,700 KB
testcase_01 AC 18 ms
8,736 KB
testcase_02 AC 20 ms
8,568 KB
testcase_03 AC 536 ms
11,064 KB
testcase_04 AC 17 ms
8,708 KB
testcase_05 AC 18 ms
8,616 KB
testcase_06 AC 19 ms
8,668 KB
testcase_07 AC 53 ms
8,896 KB
testcase_08 AC 40 ms
8,964 KB
testcase_09 AC 48 ms
8,932 KB
testcase_10 AC 38 ms
8,836 KB
testcase_11 AC 29 ms
8,536 KB
testcase_12 AC 906 ms
15,760 KB
testcase_13 AC 697 ms
15,924 KB
testcase_14 AC 719 ms
16,016 KB
testcase_15 AC 310 ms
14,576 KB
testcase_16 AC 365 ms
13,168 KB
testcase_17 AC 941 ms
16,872 KB
testcase_18 AC 713 ms
15,748 KB
testcase_19 AC 1,362 ms
18,636 KB
testcase_20 AC 1,352 ms
18,756 KB
testcase_21 AC 1,248 ms
19,420 KB
testcase_22 AC 1,163 ms
19,264 KB
testcase_23 AC 112 ms
9,232 KB
testcase_24 AC 20 ms
8,664 KB
testcase_25 AC 109 ms
13,580 KB
testcase_26 AC 29 ms
8,952 KB
testcase_27 AC 347 ms
13,492 KB
testcase_28 AC 214 ms
15,300 KB
testcase_29 AC 17 ms
8,564 KB
testcase_30 AC 51 ms
9,460 KB
testcase_31 AC 873 ms
15,488 KB
testcase_32 AC 55 ms
8,856 KB
testcase_33 AC 789 ms
13,748 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