結果

問題 No.1812 Uribo Road
ユーザー terasaterasa
提出日時 2022-11-05 00:33:30
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,231 bytes
コンパイル時間 305 ms
コンパイル使用メモリ 87,232 KB
実行使用メモリ 237,088 KB
最終ジャッジ日時 2023-09-26 02:40:56
合計ジャッジ時間 25,690 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 172 ms
80,372 KB
testcase_01 AC 177 ms
80,228 KB
testcase_02 AC 187 ms
81,680 KB
testcase_03 AC 259 ms
84,104 KB
testcase_04 AC 182 ms
80,216 KB
testcase_05 AC 181 ms
80,252 KB
testcase_06 AC 181 ms
80,444 KB
testcase_07 AC 205 ms
82,388 KB
testcase_08 AC 308 ms
84,296 KB
testcase_09 AC 382 ms
84,884 KB
testcase_10 AC 300 ms
84,376 KB
testcase_11 AC 309 ms
83,524 KB
testcase_12 AC 3,027 ms
175,528 KB
testcase_13 AC 729 ms
98,032 KB
testcase_14 AC 665 ms
97,120 KB
testcase_15 AC 1,282 ms
103,904 KB
testcase_16 AC 1,917 ms
138,036 KB
testcase_17 AC 4,567 ms
200,204 KB
testcase_18 AC 630 ms
88,200 KB
testcase_19 TLE -
testcase_20 TLE -
testcase_21 AC 4,816 ms
209,336 KB
testcase_22 AC 3,488 ms
181,268 KB
testcase_23 AC 383 ms
85,240 KB
testcase_24 AC 216 ms
83,096 KB
testcase_25 AC 432 ms
87,668 KB
testcase_26 AC 305 ms
83,796 KB
testcase_27 AC 544 ms
87,240 KB
testcase_28 AC 757 ms
93,300 KB
testcase_29 AC 180 ms
80,340 KB
testcase_30 AC 372 ms
85,460 KB
testcase_31 AC 2,654 ms
138,340 KB
testcase_32 AC 281 ms
83,680 KB
testcase_33 AC 809 ms
101,964 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)):
    for j in range(i + 1, len(V)):
        solver = Dijkstra(N, E, start=V[i])
        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