結果
問題 | No.2764 Warp Drive Spacecraft |
ユーザー | magurofly |
提出日時 | 2024-04-29 11:36:41 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,667 ms / 3,000 ms |
コード長 | 2,408 bytes |
コンパイル時間 | 201 ms |
コンパイル使用メモリ | 82,816 KB |
実行使用メモリ | 216,136 KB |
最終ジャッジ日時 | 2024-07-17 20:23:08 |
合計ジャッジ時間 | 28,361 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 40 ms
53,248 KB |
testcase_01 | AC | 40 ms
53,120 KB |
testcase_02 | AC | 40 ms
52,864 KB |
testcase_03 | AC | 39 ms
52,736 KB |
testcase_04 | AC | 40 ms
53,248 KB |
testcase_05 | AC | 39 ms
53,264 KB |
testcase_06 | AC | 40 ms
52,736 KB |
testcase_07 | AC | 40 ms
53,632 KB |
testcase_08 | AC | 40 ms
53,120 KB |
testcase_09 | AC | 40 ms
53,120 KB |
testcase_10 | AC | 76 ms
73,088 KB |
testcase_11 | AC | 43 ms
54,400 KB |
testcase_12 | AC | 92 ms
76,552 KB |
testcase_13 | AC | 95 ms
76,932 KB |
testcase_14 | AC | 39 ms
52,736 KB |
testcase_15 | AC | 38 ms
53,504 KB |
testcase_16 | AC | 404 ms
152,452 KB |
testcase_17 | AC | 426 ms
152,336 KB |
testcase_18 | AC | 440 ms
162,472 KB |
testcase_19 | AC | 1,119 ms
163,132 KB |
testcase_20 | AC | 1,125 ms
163,104 KB |
testcase_21 | AC | 1,182 ms
163,180 KB |
testcase_22 | AC | 1,208 ms
162,184 KB |
testcase_23 | AC | 1,212 ms
156,348 KB |
testcase_24 | AC | 1,272 ms
158,996 KB |
testcase_25 | AC | 1,205 ms
163,224 KB |
testcase_26 | AC | 1,585 ms
196,660 KB |
testcase_27 | AC | 1,567 ms
179,360 KB |
testcase_28 | AC | 1,569 ms
200,764 KB |
testcase_29 | AC | 1,579 ms
189,336 KB |
testcase_30 | AC | 1,614 ms
201,940 KB |
testcase_31 | AC | 1,605 ms
204,036 KB |
testcase_32 | AC | 1,667 ms
216,136 KB |
testcase_33 | AC | 635 ms
123,572 KB |
testcase_34 | AC | 600 ms
121,472 KB |
testcase_35 | AC | 599 ms
122,752 KB |
testcase_36 | AC | 429 ms
152,664 KB |
testcase_37 | AC | 653 ms
143,892 KB |
testcase_38 | AC | 602 ms
129,832 KB |
ソースコード
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)