結果

問題 No.1814 Uribo Road (Easy)
ユーザー vwxyzvwxyz
提出日時 2023-12-19 07:48:57
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 170 ms / 2,000 ms
コード長 3,251 bytes
コンパイル時間 292 ms
コンパイル使用メモリ 13,056 KB
実行使用メモリ 16,000 KB
最終ジャッジ日時 2024-09-27 08:39:41
合計ジャッジ時間 5,607 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
11,136 KB
testcase_01 AC 35 ms
11,136 KB
testcase_02 AC 34 ms
11,136 KB
testcase_03 AC 34 ms
11,008 KB
testcase_04 AC 34 ms
11,264 KB
testcase_05 AC 33 ms
11,136 KB
testcase_06 AC 33 ms
11,264 KB
testcase_07 AC 32 ms
11,136 KB
testcase_08 AC 34 ms
11,136 KB
testcase_09 AC 37 ms
11,136 KB
testcase_10 AC 36 ms
11,264 KB
testcase_11 AC 40 ms
11,520 KB
testcase_12 AC 48 ms
11,776 KB
testcase_13 AC 45 ms
11,520 KB
testcase_14 AC 54 ms
11,904 KB
testcase_15 AC 35 ms
11,136 KB
testcase_16 AC 37 ms
11,264 KB
testcase_17 AC 37 ms
11,136 KB
testcase_18 AC 37 ms
11,136 KB
testcase_19 AC 37 ms
11,264 KB
testcase_20 AC 73 ms
13,056 KB
testcase_21 AC 46 ms
11,520 KB
testcase_22 AC 116 ms
14,976 KB
testcase_23 AC 78 ms
13,056 KB
testcase_24 AC 114 ms
14,464 KB
testcase_25 AC 115 ms
14,592 KB
testcase_26 AC 143 ms
15,360 KB
testcase_27 AC 157 ms
15,744 KB
testcase_28 AC 157 ms
15,616 KB
testcase_29 AC 161 ms
15,872 KB
testcase_30 AC 166 ms
15,872 KB
testcase_31 AC 170 ms
16,000 KB
testcase_32 AC 49 ms
11,776 KB
testcase_33 AC 37 ms
11,008 KB
testcase_34 AC 35 ms
11,136 KB
testcase_35 AC 50 ms
11,904 KB
testcase_36 AC 41 ms
11,264 KB
testcase_37 AC 45 ms
11,648 KB
testcase_38 AC 61 ms
12,288 KB
testcase_39 AC 43 ms
11,520 KB
testcase_40 AC 48 ms
11,776 KB
testcase_41 AC 101 ms
14,976 KB
testcase_42 AC 63 ms
12,672 KB
testcase_43 AC 48 ms
11,520 KB
testcase_44 AC 66 ms
12,288 KB
testcase_45 AC 41 ms
11,392 KB
testcase_46 AC 108 ms
14,720 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