結果

問題 No.848 なかよし旅行
ユーザー vwxyzvwxyz
提出日時 2024-04-07 21:29:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 580 ms / 2,000 ms
コード長 2,470 bytes
コンパイル時間 216 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 113,404 KB
最終ジャッジ日時 2024-04-07 21:29:35
合計ジャッジ時間 7,417 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 580 ms
113,404 KB
testcase_01 AC 40 ms
55,608 KB
testcase_02 AC 36 ms
53,460 KB
testcase_03 AC 36 ms
53,460 KB
testcase_04 AC 36 ms
53,460 KB
testcase_05 AC 37 ms
53,460 KB
testcase_06 AC 50 ms
64,148 KB
testcase_07 AC 37 ms
53,460 KB
testcase_08 AC 92 ms
76,200 KB
testcase_09 AC 115 ms
77,340 KB
testcase_10 AC 93 ms
76,548 KB
testcase_11 AC 271 ms
84,700 KB
testcase_12 AC 306 ms
86,396 KB
testcase_13 AC 308 ms
89,356 KB
testcase_14 AC 264 ms
80,872 KB
testcase_15 AC 301 ms
89,784 KB
testcase_16 AC 401 ms
98,768 KB
testcase_17 AC 301 ms
90,524 KB
testcase_18 AC 266 ms
83,440 KB
testcase_19 AC 257 ms
83,056 KB
testcase_20 AC 201 ms
77,976 KB
testcase_21 AC 343 ms
95,140 KB
testcase_22 AC 256 ms
97,292 KB
testcase_23 AC 206 ms
77,720 KB
testcase_24 AC 39 ms
53,460 KB
testcase_25 AC 451 ms
98,200 KB
testcase_26 AC 37 ms
53,460 KB
testcase_27 AC 37 ms
53,460 KB
testcase_28 AC 36 ms
53,460 KB
testcase_29 AC 36 ms
53,460 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq

class Graph:
    def __init__(self,V,edges=None,graph=None,directed=False,weighted=False,inf=float("inf")):
        self.V=V
        self.directed=directed
        self.weighted=weighted
        self.inf=inf
        if graph!=None:
            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))
            """
        else:
            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)

    def Dijkstra(self,s,route_restoration=False):
        dist=[self.inf]*self.V
        dist[s]=0
        queue=[(0,s)]
        if route_restoration:
            parents=[None]*self.V
        while queue:
            dx,x=heapq.heappop(queue)
            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(queue,(dist[y],y))
        if route_restoration:
            return dist,parents
        else:
            return dist

N,M,P,Q,T=map(int,input().split())
edges=[]
for m in range(M):
    a,b,c=map(int,input().split())
    a-=1;b-=1
    edges.append((a,b,c))
G=Graph(N,edges=edges,weighted=True)
P-=1;Q-=1
dist0=G.Dijkstra(0)
distP=G.Dijkstra(P)
distQ=G.Dijkstra(Q)
if dist0[P]+distP[Q]+distQ[0]<=T:
    ans=T
else:
    ans=-1
    for x in range(N):
        for y in range(N):
            dP=dist0[x]+distP[x]+distP[y]+dist0[y]
            dQ=dist0[x]+distQ[x]+distQ[y]+dist0[y]
            if dP<=T and dQ<=T:
                ans=max(ans,T-max(distP[x]+distP[y],distQ[x]+distQ[y]))
print(ans)
0