結果
問題 | No.1069 電柱 / Pole (Hard) |
ユーザー | vwxyz |
提出日時 | 2021-12-27 05:01:00 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 4,959 bytes |
コンパイル時間 | 310 ms |
コンパイル使用メモリ | 82,324 KB |
実行使用メモリ | 137,380 KB |
最終ジャッジ日時 | 2024-09-24 20:17:50 |
合計ジャッジ時間 | 4,260 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 43 ms
62,692 KB |
testcase_01 | AC | 39 ms
53,888 KB |
testcase_02 | AC | 40 ms
55,276 KB |
testcase_03 | AC | 40 ms
54,448 KB |
testcase_04 | AC | 291 ms
137,380 KB |
testcase_05 | TLE | - |
testcase_06 | -- | - |
testcase_07 | -- | - |
testcase_08 | -- | - |
testcase_09 | -- | - |
testcase_10 | -- | - |
testcase_11 | -- | - |
testcase_12 | -- | - |
testcase_13 | -- | - |
testcase_14 | -- | - |
testcase_15 | -- | - |
testcase_16 | -- | - |
testcase_17 | -- | - |
testcase_18 | -- | - |
testcase_19 | -- | - |
testcase_20 | -- | - |
testcase_21 | -- | - |
testcase_22 | -- | - |
testcase_23 | -- | - |
testcase_24 | -- | - |
testcase_25 | -- | - |
testcase_26 | -- | - |
testcase_27 | -- | - |
testcase_28 | -- | - |
testcase_29 | -- | - |
testcase_30 | -- | - |
testcase_31 | -- | - |
testcase_32 | -- | - |
testcase_33 | -- | - |
testcase_34 | -- | - |
testcase_35 | -- | - |
testcase_36 | -- | - |
testcase_37 | -- | - |
testcase_38 | -- | - |
testcase_39 | -- | - |
testcase_40 | -- | - |
testcase_41 | -- | - |
testcase_42 | -- | - |
testcase_43 | -- | - |
testcase_44 | -- | - |
testcase_45 | -- | - |
testcase_46 | -- | - |
testcase_47 | -- | - |
testcase_48 | -- | - |
testcase_49 | -- | - |
testcase_50 | -- | - |
testcase_51 | -- | - |
testcase_52 | -- | - |
testcase_53 | -- | - |
testcase_54 | -- | - |
testcase_55 | -- | - |
testcase_56 | -- | - |
testcase_57 | -- | - |
testcase_58 | -- | - |
testcase_59 | -- | - |
testcase_60 | -- | - |
testcase_61 | -- | - |
testcase_62 | -- | - |
testcase_63 | -- | - |
testcase_64 | -- | - |
testcase_65 | -- | - |
testcase_66 | -- | - |
testcase_67 | -- | - |
testcase_68 | -- | - |
testcase_69 | -- | - |
testcase_70 | -- | - |
testcase_71 | -- | - |
testcase_72 | -- | - |
testcase_73 | -- | - |
testcase_74 | -- | - |
testcase_75 | -- | - |
testcase_76 | -- | - |
testcase_77 | -- | - |
testcase_78 | -- | - |
testcase_79 | -- | - |
testcase_80 | -- | - |
testcase_81 | -- | - |
testcase_82 | -- | - |
ソースコード
import heapq import math import sys readline=sys.stdin.readline class Graph: def __init__(self,V,edges=False,graph=False,directed=False,weighted=False,inf=float("inf")): self.V=V self.directed=directed self.weighted=weighted self.inf=inf if not graph: self.edges=edges self.graph=[[] for i in range(self.V)] if weighted: for i,j,d in self.edges: self.graph[i].append((j,d)) if not self.directed: self.graph[j].append((i,d)) else: for i,j in self.edges: self.graph[i].append(j) if not self.directed: self.graph[j].append(i) else: self.graph=graph self.edges=[] for i in range(self.V): if self.weighted: for j,d in self.graph[i]: if self.directed or not self.directed and i<=j: self.edges.append((i,j,d)) else: for j in self.graph[i]: if self.directed or not self.directed and i<=j: self.edges.append((i,j)) def Dijkstra(self,s,route_restoration=False): dist=[self.inf]*self.V dist[s]=0 hq=[(0,s)] if route_restoration: parents=[None]*self.V while hq: dx,x=heapq.heappop(hq) if dist[x]<dx: continue for y,dy in self.graph[x]: if dist[y]>dx+dy: dist[y]=dx+dy if route_restoration: parents[y]=x heapq.heappush(hq,(dist[y],y)) if route_restoration: return dist,parents else: return dist def Route_Restoration(self,s,g,parents): route=[g] while s!=g: if parents[g]==None: route=[] break g=parents[g] route.append(g) route=route[::-1] return route def K_Shortest_Path_Routing(self,s,t,K,edge_unicursal=False,point_unicursal=False): dist,parents=self.Dijkstra(s,route_restoration=True) if dist[t]==self.inf: return False route_lst=[tuple(self.Route_Restoration(s,x,parents)) for x in range(self.V)] edges_rev=[(j,i,d) for i,j,d in self.edges] G_rev=Graph(self.V,edges=edges_rev,weighted=self.weighted,directed=self.directed,inf=self.inf) dist_rev,parents_rev=G_rev.Dijkstra(t,route_restoration=True) route_rev_lst=[] for x in range(self.V): route=[x] while t!=x: if parents_rev[x]==None: route=[] break x=parents_rev[x] route.append(x) route_rev_lst.append(tuple(route)) route=route_lst[t] queue=[(dist[t],route,[dist[x] for x in route])] set_queue=set() set_queue.add(route) retu=[] while queue and K: d,route,route_dist=heapq.heappop(queue) bl=True if edge_unicursal: l=len(route) se=set() for j in range(len(route)-1): if self.directed: se.add((route[j],route[j+1])) else: if route[j]<route[j+1]: se.add((route[j],route[j+1])) else: se.add((route[j+1],route[j])) if len(se)!=l-1: bl=False if point_unicursal: if len(route)!=len(set(route)): bl=False if bl: retu.append((d,route,route_dist)) K-=1 for i in range(len(route)-1): x=route[i] for y,d in self.graph[x]: if y==route[i+1]: continue tpl=route[:i+1]+route_rev_lst[y] if not tpl in set_queue: heapq.heappush(queue,(route_dist[i]+d+dist_rev[y],tpl,route_dist[:i+1]+[route_dist[i]+d+dist_rev[y]-dist_rev[z] for z in route_rev_lst[y]])) set_queue.add(tpl) return retu N,M,K=map(int,readline().split()) S,T=map(int,readline().split()) S-=1;T-=1 X,Y=[],[] for _ in range(N): x,y=map(int,readline().split()) X.append(x) Y.append(y) edges=[] for _ in range(M): P,Q=map(int,readline().split()) P-=1;Q-=1 edges.append((P,Q,math.sqrt((X[P]-X[Q])**2+(Y[P]-Y[Q])**2))) G=Graph(N,edges=edges,weighted=True) ans_lst=[-1]*K for i,(d,route,dist) in enumerate(G.K_Shortest_Path_Routing(S,T,K,point_unicursal=True)): ans_lst[i]=d print(*ans_lst,sep="\n")