結果

問題 No.1812 Uribo Road
ユーザー terasaterasa
提出日時 2022-11-05 00:37:49
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,165 ms / 5,000 ms
コード長 3,227 bytes
コンパイル時間 297 ms
コンパイル使用メモリ 86,968 KB
実行使用メモリ 106,776 KB
最終ジャッジ日時 2023-09-26 02:47:34
合計ジャッジ時間 16,964 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 174 ms
80,188 KB
testcase_01 AC 178 ms
80,132 KB
testcase_02 AC 183 ms
81,864 KB
testcase_03 AC 227 ms
83,052 KB
testcase_04 AC 176 ms
80,196 KB
testcase_05 AC 177 ms
80,312 KB
testcase_06 AC 176 ms
80,396 KB
testcase_07 AC 209 ms
82,324 KB
testcase_08 AC 269 ms
84,376 KB
testcase_09 AC 321 ms
84,912 KB
testcase_10 AC 261 ms
84,548 KB
testcase_11 AC 279 ms
83,568 KB
testcase_12 AC 682 ms
96,892 KB
testcase_13 AC 369 ms
88,576 KB
testcase_14 AC 350 ms
89,056 KB
testcase_15 AC 644 ms
90,884 KB
testcase_16 AC 594 ms
93,848 KB
testcase_17 AC 1,069 ms
102,424 KB
testcase_18 AC 379 ms
88,048 KB
testcase_19 AC 1,165 ms
105,096 KB
testcase_20 AC 1,163 ms
106,776 KB
testcase_21 AC 1,072 ms
103,212 KB
testcase_22 AC 725 ms
97,912 KB
testcase_23 AC 279 ms
84,512 KB
testcase_24 AC 179 ms
82,236 KB
testcase_25 AC 457 ms
86,968 KB
testcase_26 AC 253 ms
83,700 KB
testcase_27 AC 381 ms
87,012 KB
testcase_28 AC 706 ms
93,112 KB
testcase_29 AC 184 ms
80,308 KB
testcase_30 AC 294 ms
84,880 KB
testcase_31 AC 746 ms
92,364 KB
testcase_32 AC 228 ms
83,752 KB
testcase_33 AC 470 ms
90,200 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from typing import List, Tuple, Optional
import sys
import itertools
import heapq
import bisect
from collections import deque, defaultdict
from functools import lru_cache, cmp_to_key

input = sys.stdin.readline

# for AtCoder Easy test
if __file__ != 'prog.py':
    sys.setrecursionlimit(10 ** 6)


def readints(): return map(int, input().split())
def readlist(): return list(readints())
def readstr(): return input().rstrip()


class Dijkstra:
    def __init__(self, N: int, E: List[List[Tuple[int, int]]],
                 start: int = 0, inf: int = 1 << 50):
        self.N = N
        self.E = E
        self.start = start
        self.inf = inf

        self.C = [self.inf] * N
        self.prev = [None] * N
        self._calculate()

    def get_cost(self, i: int) -> int:
        """return cost to i-th vertex. return inf if the vertex is unreachable."""
        return self.C[i]

    def get_path(self, i) -> Optional[List[int]]:
        """return shortest path to i-th vertex if reachable otherwise None"""
        if not self.reachable(i):
            return None

        p = []
        cur = i
        while cur is not None:
            p.append(cur)
            cur = self.prev[cur]
        p.reverse()
        return p

    def reachable(self, i) -> bool:
        """return whether i-th vertex is reachable from start"""
        return self.C[i] < self.inf

    def _calculate(self) -> None:
        h = [(0, self.start)]
        self.C[self.start] = 0
        visited = [False] * self.N

        while h:
            _, v = heapq.heappop(h)
            if visited[v] is True:
                continue
            visited[v] = True

            for c, d in self.E[v]:
                if self.C[d] > self.C[v] + c:
                    self.C[d] = self.C[v] + c
                    self.prev[d] = v
                    heapq.heappush(h, (self.C[d], d))


N, M, K = readints()
R = set(map(lambda x: int(x) - 1, input().split()))
E = [[] for _ in range(N)]
v = []
path = []
for i in range(M):
    a, b, c = readints()
    a -= 1
    b -= 1
    E[a].append((c, b))
    E[b].append((c, a))
    if i in R:
        v.append(a)
        v.append(b)
        path.append((a, b, c))

V = sorted(set(v))
idx = {}
for i, a in enumerate(V):
    idx[a] = i
path = [(idx[a], idx[b], c) for a, b, c in path]
C = [[0 for _ in range(len(V))] for _ in range(len(V))]
for i in range(len(V)):
    solver = Dijkstra(N, E, start=V[i])
    for j in range(i + 1, len(V)):
        c = solver.get_cost(V[j])
        C[i][j] = c
        C[j][i] = c

C0 = []
solver = Dijkstra(N, E, start=0)
for v in V:
    C0.append(solver.get_cost(v))
CN = []
solver = Dijkstra(N, E, start=N - 1)
for v in V:
    CN.append(solver.get_cost(v))

INF = 1 << 50
dp = [[INF for _ in range(len(V))] for _ in range(1 << K)]
for i in range(len(V)):
    dp[0][i] = C0[i]
for s in range(1 << K):
    for j in range(K):
        a, b, c = path[j]
        if s & (1 << j):
            continue
        for k in range(len(V)):
            nxt = s | (1 << j)
            dp[nxt][a] = min(dp[nxt][a], dp[s][k] + C[k][b] + c)
            dp[nxt][b] = min(dp[nxt][b], dp[s][k] + C[k][a] + c)

ans = INF
for k in range(len(V)):
    ans = min(ans, dp[-1][k] + CN[k])
print(ans)
0