結果

問題 No.1449 新プロランド
ユーザー terasaterasa
提出日時 2022-11-03 16:20:55
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,457 ms / 2,000 ms
コード長 2,663 bytes
コンパイル時間 279 ms
コンパイル使用メモリ 87,268 KB
実行使用メモリ 252,504 KB
最終ジャッジ日時 2023-08-25 16:54:32
合計ジャッジ時間 19,079 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 195 ms
83,476 KB
testcase_01 AC 260 ms
90,100 KB
testcase_02 AC 357 ms
102,576 KB
testcase_03 AC 256 ms
89,984 KB
testcase_04 AC 284 ms
90,744 KB
testcase_05 AC 1,457 ms
252,504 KB
testcase_06 AC 899 ms
184,924 KB
testcase_07 AC 623 ms
162,616 KB
testcase_08 AC 1,039 ms
223,500 KB
testcase_09 AC 580 ms
148,940 KB
testcase_10 AC 690 ms
190,572 KB
testcase_11 AC 516 ms
181,520 KB
testcase_12 AC 1,090 ms
210,824 KB
testcase_13 AC 644 ms
142,104 KB
testcase_14 AC 469 ms
172,548 KB
testcase_15 AC 992 ms
199,252 KB
testcase_16 AC 747 ms
159,020 KB
testcase_17 AC 863 ms
173,396 KB
testcase_18 AC 193 ms
83,428 KB
testcase_19 AC 253 ms
124,936 KB
testcase_20 AC 183 ms
82,988 KB
testcase_21 AC 582 ms
168,588 KB
testcase_22 AC 347 ms
94,532 KB
testcase_23 AC 693 ms
207,540 KB
testcase_24 AC 521 ms
125,260 KB
testcase_25 AC 401 ms
106,084 KB
testcase_26 AC 227 ms
87,160 KB
testcase_27 AC 1,295 ms
246,608 KB
testcase_28 AC 785 ms
175,880 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)
    pass


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) -> Optional[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 = readints()
edges = [tuple(readints()) for _ in range(M)]
T = readlist()

L = 10000
E = [[] for _ in range(N * (L + 1))]
for a, b, c in edges:
    a -= 1
    b -= 1

    def h(i, t):
        return i * (L + 1) + t
    for j in range(L + 1):
        if j + T[a] > L:
            break
        if j + T[a] == 0:
            continue
        cost = T[a] + c // (j + T[a])
        E[h(a, j)].append((cost, h(b, j + T[a])))
    for j in range(L + 1):
        if j + T[b] > L:
            break
        if j + T[b] == 0:
            continue
        cost = T[b] + c // (j + T[b])
        E[h(b, j)].append((cost, h(a, j + T[b])))
solver = Dijkstra(N * (L + 1), E)
ans = solver.inf
for j in range(L + 1):
    ans = min(ans, solver.get_cost((N - 1) * (L + 1) + j))
print(ans)
0