結果
問題 | No.1812 Uribo Road |
ユーザー | terasa |
提出日時 | 2022-11-05 00:37:49 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 827 ms / 5,000 ms |
コード長 | 3,227 bytes |
コンパイル時間 | 662 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 101,888 KB |
最終ジャッジ日時 | 2024-07-18 22:05:17 |
合計ジャッジ時間 | 10,847 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 61 ms
68,352 KB |
testcase_01 | AC | 59 ms
68,864 KB |
testcase_02 | AC | 67 ms
71,296 KB |
testcase_03 | AC | 113 ms
79,564 KB |
testcase_04 | AC | 60 ms
68,480 KB |
testcase_05 | AC | 62 ms
68,352 KB |
testcase_06 | AC | 67 ms
68,992 KB |
testcase_07 | AC | 93 ms
78,476 KB |
testcase_08 | AC | 151 ms
80,000 KB |
testcase_09 | AC | 196 ms
80,384 KB |
testcase_10 | AC | 147 ms
79,616 KB |
testcase_11 | AC | 156 ms
79,616 KB |
testcase_12 | AC | 465 ms
91,520 KB |
testcase_13 | AC | 234 ms
84,992 KB |
testcase_14 | AC | 221 ms
83,436 KB |
testcase_15 | AC | 441 ms
86,016 KB |
testcase_16 | AC | 400 ms
88,736 KB |
testcase_17 | AC | 775 ms
97,664 KB |
testcase_18 | AC | 240 ms
83,456 KB |
testcase_19 | AC | 827 ms
100,352 KB |
testcase_20 | AC | 789 ms
101,888 KB |
testcase_21 | AC | 691 ms
98,228 KB |
testcase_22 | AC | 499 ms
93,556 KB |
testcase_23 | AC | 186 ms
80,128 KB |
testcase_24 | AC | 77 ms
73,088 KB |
testcase_25 | AC | 305 ms
83,968 KB |
testcase_26 | AC | 137 ms
79,272 KB |
testcase_27 | AC | 244 ms
82,176 KB |
testcase_28 | AC | 502 ms
87,424 KB |
testcase_29 | AC | 62 ms
68,608 KB |
testcase_30 | AC | 177 ms
80,204 KB |
testcase_31 | AC | 533 ms
88,576 KB |
testcase_32 | AC | 144 ms
79,360 KB |
testcase_33 | AC | 303 ms
85,368 KB |
ソースコード
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)