結果

問題 No.2764 Warp Drive Spacecraft
ユーザー maguroflymagurofly
提出日時 2024-04-29 11:36:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,635 ms / 3,000 ms
コード長 2,408 bytes
コンパイル時間 131 ms
コンパイル使用メモリ 82,512 KB
実行使用メモリ 215,892 KB
最終ジャッジ日時 2024-05-18 00:09:53
合計ジャッジ時間 23,033 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,492 KB
testcase_01 AC 37 ms
53,932 KB
testcase_02 AC 35 ms
54,220 KB
testcase_03 AC 35 ms
54,092 KB
testcase_04 AC 35 ms
53,136 KB
testcase_05 AC 35 ms
53,668 KB
testcase_06 AC 35 ms
53,492 KB
testcase_07 AC 35 ms
53,348 KB
testcase_08 AC 35 ms
55,232 KB
testcase_09 AC 36 ms
53,552 KB
testcase_10 AC 69 ms
74,484 KB
testcase_11 AC 39 ms
56,232 KB
testcase_12 AC 85 ms
76,856 KB
testcase_13 AC 82 ms
76,904 KB
testcase_14 AC 37 ms
53,704 KB
testcase_15 AC 38 ms
53,616 KB
testcase_16 AC 352 ms
152,328 KB
testcase_17 AC 366 ms
152,580 KB
testcase_18 AC 374 ms
162,592 KB
testcase_19 AC 948 ms
163,208 KB
testcase_20 AC 962 ms
163,088 KB
testcase_21 AC 981 ms
163,212 KB
testcase_22 AC 1,017 ms
162,176 KB
testcase_23 AC 972 ms
156,412 KB
testcase_24 AC 1,047 ms
158,844 KB
testcase_25 AC 965 ms
163,232 KB
testcase_26 AC 1,315 ms
196,676 KB
testcase_27 AC 1,304 ms
179,096 KB
testcase_28 AC 1,333 ms
201,044 KB
testcase_29 AC 1,326 ms
188,968 KB
testcase_30 AC 1,635 ms
202,008 KB
testcase_31 AC 1,338 ms
204,024 KB
testcase_32 AC 1,455 ms
215,892 KB
testcase_33 AC 510 ms
123,680 KB
testcase_34 AC 499 ms
121,352 KB
testcase_35 AC 493 ms
122,976 KB
testcase_36 AC 364 ms
152,484 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq

INF = 2 * 10**18

class StaticCHT:
    # funcs: 一次関数 f(x)=ax+b: (a, b) のリスト
    def __init__(self, funcs):
        by_a = {}
        for a, b in funcs:
            if a in by_a:
                by_a[a] = min(by_a[a], b)
            else:
                by_a[a] = b
        funcs = sorted(by_a.items(), key = lambda f: -f[0])

        # 交点を求める
        self.points = []
        self.funcs = [funcs[0]]
        for i in range(len(funcs) - 1):
            a1, b1 = funcs[i]
            a2, b2 = funcs[i + 1]
            self.points.append((b1 - b2, a2 - a1))
            self.funcs.append((a2, b2))
            while len(self.points) >= 2:
                p1, q1 = self.points[-2]
                p2, q2 = self.points[-1]
                if p1 * q2 <= p2 * q1:
                    break
                self.funcs.pop(-2)
                self.points.pop()
                self.points.pop()
                a1, b1 = self.funcs[-2]
                a2, b2 = self.funcs[-1]
                self.points.append((b1 - b2, a2 - a1))
    
    # 点 x における最小値を求める
    def min(self, x):
        l, r = 0, len(self.points)
        while l < r:
            mid = (l + r) // 2
            p, q = self.points[mid]
            if x * q <= p:
                l = mid + 1
            else:
                r = mid
        a, b = self.funcs[l]
        return a * x + b

def dijkstra(graph, start, inf = 10**18):
    dist = [inf] * len(graph)
    dist[start] = 0
    pq = []
    heapq.heappush(pq, (0, start))
    while pq:
        d, u = heapq.heappop(pq)
        if d > dist[u]:
            continue
        for v, w in graph[u]:
            if dist[v] > d + w:
                dist[v] = d + w
                heapq.heappush(pq, (d + w, v))
    return dist

N, M = map(int, input().split())
W = list(map(int, input().split()))
edges = [list(map(int, input().split())) for _ in range(M)]

graph = [[] for _ in range(N)]
for u, v, t in edges:
    graph[u - 1].append((v - 1, t))
    graph[v - 1].append((u - 1, t))

dist_s = dijkstra(graph, 0, INF)
dist_t = dijkstra(graph, N - 1, INF)

ans = dist_s[N - 1]

# dist_s[i] + dist_t[j] + W[i] * W[j]
funcs = []
for j in range(N):
    if dist_t[j] < INF:
        funcs.append((W[j], dist_t[j]))
cht = StaticCHT(funcs)
for i in range(N):
    if dist_s[i] < INF:
        ans = min(ans, dist_s[i] + cht.min(W[i]))

print(ans)
0