結果

問題 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,549 ms / 5,000 ms
コード長 3,251 bytes
コンパイル時間 219 ms
コンパイル使用メモリ 13,056 KB
実行使用メモリ 21,760 KB
最終ジャッジ日時 2024-07-23 21:59:57
合計ジャッジ時間 16,964 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
11,136 KB
testcase_01 AC 32 ms
11,136 KB
testcase_02 AC 33 ms
11,136 KB
testcase_03 AC 594 ms
13,696 KB
testcase_04 AC 32 ms
11,264 KB
testcase_05 AC 32 ms
11,264 KB
testcase_06 AC 32 ms
11,136 KB
testcase_07 AC 70 ms
11,392 KB
testcase_08 AC 58 ms
11,264 KB
testcase_09 AC 68 ms
11,392 KB
testcase_10 AC 55 ms
11,264 KB
testcase_11 AC 46 ms
11,264 KB
testcase_12 AC 991 ms
18,176 KB
testcase_13 AC 817 ms
18,304 KB
testcase_14 AC 816 ms
18,304 KB
testcase_15 AC 373 ms
17,024 KB
testcase_16 AC 426 ms
15,488 KB
testcase_17 AC 1,053 ms
19,200 KB
testcase_18 AC 772 ms
18,176 KB
testcase_19 AC 1,549 ms
20,992 KB
testcase_20 AC 1,521 ms
21,248 KB
testcase_21 AC 1,410 ms
21,760 KB
testcase_22 AC 1,314 ms
21,632 KB
testcase_23 AC 145 ms
11,776 KB
testcase_24 AC 34 ms
11,264 KB
testcase_25 AC 140 ms
16,000 KB
testcase_26 AC 45 ms
11,392 KB
testcase_27 AC 389 ms
16,000 KB
testcase_28 AC 257 ms
17,664 KB
testcase_29 AC 33 ms
11,136 KB
testcase_30 AC 71 ms
12,032 KB
testcase_31 AC 995 ms
17,920 KB
testcase_32 AC 74 ms
11,264 KB
testcase_33 AC 912 ms
16,384 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