結果

問題 No.1069 電柱 / Pole (Hard)
ユーザー vwxyzvwxyz
提出日時 2021-12-27 08:53:04
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,734 ms / 2,000 ms
コード長 7,410 bytes
コンパイル時間 433 ms
コンパイル使用メモリ 86,892 KB
実行使用メモリ 205,092 KB
最終ジャッジ日時 2023-09-30 08:34:30
合計ジャッジ時間 19,907 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
71,280 KB
testcase_01 AC 82 ms
71,408 KB
testcase_02 AC 83 ms
71,276 KB
testcase_03 AC 81 ms
71,104 KB
testcase_04 AC 1,734 ms
205,092 KB
testcase_05 AC 1,256 ms
135,544 KB
testcase_06 AC 215 ms
81,068 KB
testcase_07 AC 226 ms
81,352 KB
testcase_08 AC 232 ms
81,392 KB
testcase_09 AC 234 ms
81,704 KB
testcase_10 AC 210 ms
81,032 KB
testcase_11 AC 128 ms
78,216 KB
testcase_12 AC 155 ms
79,820 KB
testcase_13 AC 310 ms
83,340 KB
testcase_14 AC 248 ms
82,536 KB
testcase_15 AC 199 ms
80,556 KB
testcase_16 AC 176 ms
79,056 KB
testcase_17 AC 168 ms
78,836 KB
testcase_18 AC 219 ms
80,012 KB
testcase_19 AC 225 ms
82,096 KB
testcase_20 AC 157 ms
79,704 KB
testcase_21 AC 285 ms
82,904 KB
testcase_22 AC 185 ms
80,540 KB
testcase_23 AC 390 ms
88,348 KB
testcase_24 AC 205 ms
80,852 KB
testcase_25 AC 193 ms
80,400 KB
testcase_26 AC 189 ms
80,624 KB
testcase_27 AC 188 ms
80,444 KB
testcase_28 AC 188 ms
80,316 KB
testcase_29 AC 143 ms
79,540 KB
testcase_30 AC 121 ms
78,080 KB
testcase_31 AC 82 ms
71,320 KB
testcase_32 AC 82 ms
71,492 KB
testcase_33 AC 81 ms
71,752 KB
testcase_34 AC 81 ms
71,608 KB
testcase_35 AC 81 ms
71,820 KB
testcase_36 AC 83 ms
71,368 KB
testcase_37 AC 170 ms
78,856 KB
testcase_38 AC 161 ms
79,648 KB
testcase_39 AC 95 ms
76,540 KB
testcase_40 AC 149 ms
78,564 KB
testcase_41 AC 177 ms
79,136 KB
testcase_42 AC 187 ms
78,920 KB
testcase_43 AC 185 ms
79,152 KB
testcase_44 AC 214 ms
81,088 KB
testcase_45 AC 229 ms
81,900 KB
testcase_46 AC 258 ms
83,044 KB
testcase_47 AC 264 ms
82,532 KB
testcase_48 AC 209 ms
81,244 KB
testcase_49 AC 226 ms
81,548 KB
testcase_50 AC 150 ms
79,024 KB
testcase_51 AC 242 ms
82,612 KB
testcase_52 AC 148 ms
79,532 KB
testcase_53 AC 227 ms
82,004 KB
testcase_54 AC 80 ms
71,660 KB
testcase_55 AC 91 ms
76,588 KB
testcase_56 AC 79 ms
71,548 KB
testcase_57 AC 95 ms
76,500 KB
testcase_58 AC 80 ms
71,476 KB
testcase_59 AC 117 ms
78,060 KB
testcase_60 AC 82 ms
71,448 KB
testcase_61 AC 92 ms
76,700 KB
testcase_62 AC 81 ms
71,452 KB
testcase_63 AC 81 ms
71,536 KB
testcase_64 AC 106 ms
76,616 KB
testcase_65 AC 81 ms
71,524 KB
testcase_66 AC 82 ms
71,368 KB
testcase_67 AC 80 ms
71,600 KB
testcase_68 AC 82 ms
71,660 KB
testcase_69 AC 187 ms
80,348 KB
testcase_70 AC 195 ms
80,472 KB
testcase_71 AC 158 ms
79,512 KB
testcase_72 AC 144 ms
78,224 KB
testcase_73 AC 174 ms
78,808 KB
testcase_74 AC 219 ms
81,788 KB
testcase_75 AC 199 ms
80,792 KB
testcase_76 AC 245 ms
82,216 KB
testcase_77 AC 265 ms
82,020 KB
testcase_78 AC 139 ms
78,272 KB
testcase_79 AC 283 ms
83,212 KB
testcase_80 AC 249 ms
82,280 KB
testcase_81 AC 251 ms
82,304 KB
testcase_82 AC 221 ms
81,524 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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,reverse=False):
        route=[g]
        while s!=g:
            if parents[g]==None:
                route=[]
                break
            g=parents[g]
            route.append(g)
        if not reverse:
            route=route[::-1]
        return route

    def K_Shortest_Path_Routing(self,s,t,K,edge_unicursal=False,point_unicursal=False):
        if point_unicursal:
            dist,parents=self.Dijkstra(s,route_restoration=True)
            route=tuple(self.Route_Restoration(s,t,parents))
            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)
                retu.append((d,route,route_dist))
                K-=1
                set_route=set()
                for i in range(len(route)-1):
                    x=route[i]
                    set_route.add(x)
                    edges=[(v,u,d) for u,v,d in self.edges if not u in set_route and not v in set_route]
                    G_rev=Graph(self.V,edges=edges,directed=self.directed,weighted=self.weighted,inf=self.inf)
                    dist_rev,parents_rev=G_rev.Dijkstra(t,route_restoration=True)
                    for y,d in self.graph[x]:
                        if y==route[i+1]:
                            continue
                        if dist_rev[y]==self.inf:
                            continue
                        tpl=route[:i+1]+tuple(self.Route_Restoration(t,y,parents_rev,reverse=True))
                        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 tpl[i+1:]]))
                            set_queue.add(tpl)
        elif edge_unicursal:
            dist,parents=self.Dijkstra(s,route_restoration=True)
            route=tuple(self.Route_Restoration(s,t,parents))
            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)
                retu.append((d,route,route_dist))
                K-=1
                set_route=set()
                for i in range(len(route)-1):
                    x=route[i]
                    y=route[i+1]
                    set_route.add((x,y,route_dist[i+1]-route_dist[i]))
                    if not self.directed:
                        set_route.add((y,x,route_dist[i+1]-route_dist[i]))
                    edges=[(v,u,d) for u,v,d in self.edges if not (u,v,d) in set_route]
                    G_rev=Graph(self.V,edges=edges,directed=self.directed,weighted=self.weighted,inf=self.inf)
                    dist_rev,parents_rev=G_rev.Dijkstra(t,route_restoration=True)
                    for y,d in self.graph[x]:
                        if y==route[i+1]:
                            continue
                        if dist_rev[y]==self.inf:
                            continue
                        tpl=route[:i+1]+tuple(self.Route_Restoration(t,y,parents_rev,reverse=True))
                        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 tpl[i+1:]]))
                            set_queue.add(tpl)
        else:
            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_rev_lst.append(tuple(self.Route_Restoration(t,x,parents_rev,reverse=True)))
            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)
                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")
0