結果
問題 | No.2376 障害物競プロ |
ユーザー |
![]() |
提出日時 | 2025-03-26 15:50:15 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,108 ms / 4,000 ms |
コード長 | 2,705 bytes |
コンパイル時間 | 256 ms |
コンパイル使用メモリ | 82,912 KB |
実行使用メモリ | 146,196 KB |
最終ジャッジ日時 | 2025-03-26 15:52:05 |
合計ジャッジ時間 | 62,301 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 40 |
ソースコード
import heapqdef segments_intersect(a, b, c, d):ax, ay = abx, by = bcx, cy = cdx, dy = d# Calculate the direction vectorsv1x = bx - axv1y = by - ayv2x = dx - cxv2y = dy - cy# Calculate the denominatordenominator = v1x * v2y - v1y * v2xif denominator == 0:return False# Calculate parameters s and ts_numerator = (cx - ax) * v1y - (cy - ay) * v1xt_numerator = (cx - ax) * v2y - (cy - ay) * v2xs = s_numerator / denominatort = t_numerator / denominatorreturn 0 < s < 1 and 0 < t < 1def main():import sysinput = sys.stdin.read().split()ptr = 0N, M = int(input[ptr]), int(input[ptr+1])ptr +=2logs = []nodes = []for i in range(N):x1 = int(input[ptr])y1 = int(input[ptr+1])x2 = int(input[ptr+2])y2 = int(input[ptr+3])ptr +=4logs.append( ( (x1, y1), (x2, y2) ) )nodes.append( (x1, y1) )nodes.append( (x2, y2) )V = len(nodes)INF = float('inf')# Build adjacency listgraph = [[] for _ in range(V)]for u in range(V):for v in range(V):if u == v:continue# Check if u and v are endpoints of the same logif (u // 2) == (v // 2):continuea = nodes[u]b = nodes[v]valid = Truefor log in logs:c, d = logif segments_intersect(a, b, c, d):valid = Falsebreakif valid:dx = a[0] - b[0]dy = a[1] - b[1]distance = (dx**2 + dy**2)**0.5graph[u].append( (v, distance) )# Precompute shortest paths using Dijkstra's algorithmshortest = []for s in range(V):dist = [INF] * Vdist[s] = 0.0heap = []heapq.heappush(heap, (0.0, s))while heap:d, u = heapq.heappop(heap)if d > dist[u]:continuefor v, w in graph[u]:if dist[v] > d + w:dist[v] = d + wheapq.heappush(heap, (dist[v], v))shortest.append(dist)# Process queriesoutput = []for _ in range(M):a = int(input[ptr])b = int(input[ptr+1])c = int(input[ptr+2])d = int(input[ptr+3])ptr +=4start = 2 * (a - 1) + (b - 1)end = 2 * (c - 1) + (d - 1)res = shortest[start][end]output.append("{0:.12f}".format(res))print('\n'.join(output))if __name__ == "__main__":main()