結果

問題 No.1069 電柱 / Pole (Hard)
ユーザー vwxyzvwxyz
提出日時 2021-12-27 04:48:26
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 4,995 bytes
コンパイル時間 153 ms
コンパイル使用メモリ 13,312 KB
実行使用メモリ 97,664 KB
最終ジャッジ日時 2024-09-24 20:01:53
合計ジャッジ時間 6,817 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
11,392 KB
testcase_01 AC 33 ms
11,392 KB
testcase_02 AC 32 ms
11,392 KB
testcase_03 AC 32 ms
11,392 KB
testcase_04 AC 388 ms
97,664 KB
testcase_05 WA -
testcase_06 AC 68 ms
15,104 KB
testcase_07 AC 69 ms
15,360 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 56 ms
14,592 KB
testcase_11 AC 35 ms
11,648 KB
testcase_12 AC 40 ms
12,416 KB
testcase_13 AC 52 ms
14,208 KB
testcase_14 AC 48 ms
13,184 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 42 ms
12,416 KB
testcase_21 AC 48 ms
13,440 KB
testcase_22 AC 42 ms
12,544 KB
testcase_23 AC 86 ms
20,224 KB
testcase_24 WA -
testcase_25 AC 81 ms
19,072 KB
testcase_26 AC 80 ms
18,944 KB
testcase_27 AC 79 ms
18,944 KB
testcase_28 AC 80 ms
18,944 KB
testcase_29 AC 45 ms
12,544 KB
testcase_30 AC 37 ms
11,776 KB
testcase_31 AC 31 ms
11,520 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 32 ms
11,520 KB
testcase_37 AC 37 ms
11,776 KB
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 AC 38 ms
11,904 KB
testcase_42 AC 37 ms
11,776 KB
testcase_43 AC 39 ms
11,904 KB
testcase_44 WA -
testcase_45 AC 48 ms
13,312 KB
testcase_46 WA -
testcase_47 AC 57 ms
14,976 KB
testcase_48 WA -
testcase_49 WA -
testcase_50 AC 37 ms
11,904 KB
testcase_51 WA -
testcase_52 AC 40 ms
12,160 KB
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 WA -
testcase_61 WA -
testcase_62 AC 32 ms
11,392 KB
testcase_63 AC 32 ms
11,520 KB
testcase_64 WA -
testcase_65 WA -
testcase_66 WA -
testcase_67 WA -
testcase_68 WA -
testcase_69 WA -
testcase_70 AC 60 ms
13,696 KB
testcase_71 WA -
testcase_72 WA -
testcase_73 WA -
testcase_74 WA -
testcase_75 WA -
testcase_76 WA -
testcase_77 WA -
testcase_78 WA -
testcase_79 AC 50 ms
13,568 KB
testcase_80 WA -
testcase_81 AC 47 ms
13,184 KB
testcase_82 WA -
権限があれば一括ダウンロードができます

ソースコード

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):
        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)
            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 edge_unicursal:
                        l=len(tpl)
                        se=set()
                        for i in range(len(tpl)-1):
                            if self.directed:
                                se.add((tpl[i],tpl[i+1]))
                            else:
                                if tpl[i]<tpl[i+1]:
                                    se.add((tpl[i],tpl[i+1]))
                                else:
                                    se.add((tpl[i+1],tpl[i]))
                        if len(se)!=l-1:
                            continue
                    if point_unicursal:
                        if len(tpl)!=len(set(tpl)):
                            continue
                    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)):
    ans_lst[i]=d
print(*ans_lst,sep="\n")
0