結果
問題 | No.2764 Warp Drive Spacecraft |
ユーザー |
![]() |
提出日時 | 2025-03-26 15:57:56 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 2,092 ms / 3,000 ms |
コード長 | 3,262 bytes |
コンパイル時間 | 227 ms |
コンパイル使用メモリ | 82,048 KB |
実行使用メモリ | 206,564 KB |
最終ジャッジ日時 | 2025-03-26 15:59:43 |
合計ジャッジ時間 | 33,666 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 35 |
ソースコード
import sysimport heapqdef dijkstra(adj, start, n):INF = float('inf')dist = [INF] * ndist[start] = 0heap = []heapq.heappush(heap, (0, start))while heap:current_dist, u = heapq.heappop(heap)if current_dist > dist[u]:continuefor v, w in adj[u]:if dist[v] > current_dist + w:dist[v] = current_dist + wheapq.heappush(heap, (dist[v], v))return distdef main():input = sys.stdin.read().split()ptr = 0N = int(input[ptr])ptr +=1M = int(input[ptr])ptr +=1W = list(map(int, input[ptr:ptr+N]))ptr +=Nadj = [[] for _ in range(N)]for _ in range(M):u = int(input[ptr])-1ptr +=1v = int(input[ptr])-1ptr +=1t = int(input[ptr])ptr +=1adj[u].append( (v, t) )adj[v].append( (u, t) )d1 = dijkstra(adj, 0, N)d2 = dijkstra(adj, N-1, N)points = []for j in range(N):if d2[j] != float('inf'):points.append( (W[j], d2[j]) )if not points:print(d1[N-1] if d1[N-1] != float('inf') else -1)returnpoints.sort()filtered = []prev_w = Nonefor w, d in points:if w == prev_w:continuefiltered.append( (w, d) )prev_w = wdef build_convex_hull(points):stack = []for w, d in points:while len(stack) >= 2:a = stack[-2]b = stack[-1]val1 = (b[1] - a[1]) * (w - b[0])val2 = (d - b[1]) * (b[0] - a[0])if val1 >= val2:stack.pop()else:breakstack.append( (w, d) )return stackconvex = build_convex_hull(filtered)def query_convex_hull(convex, x):if not convex:return float('inf')left = 0right = len(convex) -1res = 0while left <= right:mid = (left + right) //2if mid == len(convex)-1:res = midbreakw_mid, d_mid = convex[mid]w_next, d_next = convex[mid+1]val_mid = w_mid * x + d_midval_next = w_next * x + d_nextif val_mid <= val_next:res = midright = mid -1else:left = mid +1min_val = float('inf')for i in range(max(0, res-1), min(len(convex), res+2)):w, d = convex[i]current_val = w * x + dif current_val < min_val:min_val = current_valreturn min_valno_warp = d1[N-1] if d1[N-1] != float('inf') else float('inf')warp_min = float('inf')for i in range(N):if d1[i] == float('inf'):continuex = W[i]min_j_val = query_convex_hull(convex, x)if min_j_val == float('inf'):continuetotal = d1[i] + min_j_valif total < warp_min:warp_min = totalans = min(no_warp, warp_min)print(ans if ans != float('inf') else -1)if __name__ == '__main__':main()