結果
問題 | No.3072 Speedrun Query |
ユーザー |
👑 ![]() |
提出日時 | 2025-03-23 15:31:01 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,766 ms / 2,500 ms |
コード長 | 1,243 bytes |
コンパイル時間 | 301 ms |
コンパイル使用メモリ | 82,576 KB |
実行使用メモリ | 221,476 KB |
最終ジャッジ日時 | 2025-03-23 15:31:37 |
合計ジャッジ時間 | 35,336 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 21 |
ソースコード
"""https://yukicoder.me/problems/no/3072"""import heapqdef Dijkstra(lis,start):ret = [float("inf")] * len(lis)ret[start] = 0end_flag = [False] * len(lis)end_num = 0q = [(0,start)]while len(q) > 0:ncost,now = heapq.heappop(q)if end_flag[now]:continueend_flag[now] = Trueend_num += 1if end_num == len(lis):breakfor nex,ecost in lis[now]:if ret[nex] > ncost + ecost:ret[nex] = ncost + ecostheapq.heappush(q , (ret[nex] , nex))return retN,Ka,Kb = map(int,input().split())av = list(map(int,input().split()))bv = list(map(int,input().split()))lis = [ [] for i in range(N+2) ]for i in range(N-1):lis[i].append( (i+1,1) )lis[i+1].append( (i,1) )for v in av:lis[v-1].append( (N,0) )lis[N].append( (v-1,0) )for v in bv:lis[v-1].append( (N+1,0) )lis[N+1].append( (v-1,0) )da = Dijkstra(lis,N)db = Dijkstra(lis,N+1)Q = int(input())for _ in range(Q):s,t = map(int,input().split())s -= 1t -= 1ans = min( abs(s-t) , da[s]+da[t] , db[s]+db[t] , da[s]+da[N+1]+db[t] , da[t]+da[N+1]+db[s] )print (ans)