結果

問題 No.1812 Uribo Road
ユーザー terasaterasa
提出日時 2022-11-05 00:33:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 4,056 ms / 5,000 ms
コード長 3,231 bytes
コンパイル時間 243 ms
コンパイル使用メモリ 82,308 KB
実行使用メモリ 233,932 KB
最終ジャッジ日時 2024-07-18 21:59:00
合計ジャッジ時間 28,880 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
70,140 KB
testcase_01 AC 61 ms
70,024 KB
testcase_02 AC 69 ms
72,212 KB
testcase_03 AC 128 ms
79,376 KB
testcase_04 AC 66 ms
69,232 KB
testcase_05 AC 62 ms
70,260 KB
testcase_06 AC 62 ms
69,464 KB
testcase_07 AC 95 ms
78,892 KB
testcase_08 AC 175 ms
80,476 KB
testcase_09 AC 246 ms
80,828 KB
testcase_10 AC 174 ms
79,640 KB
testcase_11 AC 182 ms
79,536 KB
testcase_12 AC 1,917 ms
170,812 KB
testcase_13 AC 497 ms
94,956 KB
testcase_14 AC 419 ms
91,180 KB
testcase_15 AC 873 ms
99,616 KB
testcase_16 AC 1,233 ms
133,608 KB
testcase_17 AC 3,013 ms
194,852 KB
testcase_18 AC 435 ms
83,424 KB
testcase_19 AC 4,056 ms
233,932 KB
testcase_20 AC 3,865 ms
230,368 KB
testcase_21 AC 2,898 ms
204,132 KB
testcase_22 AC 2,165 ms
175,288 KB
testcase_23 AC 215 ms
80,424 KB
testcase_24 AC 92 ms
78,576 KB
testcase_25 AC 244 ms
82,608 KB
testcase_26 AC 169 ms
79,812 KB
testcase_27 AC 372 ms
83,056 KB
testcase_28 AC 527 ms
88,236 KB
testcase_29 AC 57 ms
69,320 KB
testcase_30 AC 207 ms
81,228 KB
testcase_31 AC 1,768 ms
131,956 KB
testcase_32 AC 143 ms
80,368 KB
testcase_33 AC 521 ms
98,696 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