結果

問題 No.848 なかよし旅行
ユーザー NoneNone
提出日時 2021-04-05 04:06:15
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 604 ms / 2,000 ms
コード長 3,308 bytes
コンパイル時間 643 ms
コンパイル使用メモリ 86,944 KB
実行使用メモリ 104,404 KB
最終ジャッジ日時 2023-08-28 15:11:10
合計ジャッジ時間 8,453 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 604 ms
104,404 KB
testcase_01 AC 82 ms
75,676 KB
testcase_02 AC 75 ms
71,408 KB
testcase_03 AC 75 ms
71,176 KB
testcase_04 AC 75 ms
71,540 KB
testcase_05 AC 76 ms
71,408 KB
testcase_06 AC 87 ms
76,300 KB
testcase_07 AC 80 ms
75,896 KB
testcase_08 AC 118 ms
77,948 KB
testcase_09 AC 132 ms
78,692 KB
testcase_10 AC 119 ms
77,768 KB
testcase_11 AC 272 ms
82,876 KB
testcase_12 AC 286 ms
82,268 KB
testcase_13 AC 300 ms
83,964 KB
testcase_14 AC 253 ms
80,532 KB
testcase_15 AC 303 ms
84,380 KB
testcase_16 AC 367 ms
88,916 KB
testcase_17 AC 307 ms
85,548 KB
testcase_18 AC 263 ms
82,040 KB
testcase_19 AC 265 ms
81,016 KB
testcase_20 AC 215 ms
79,664 KB
testcase_21 AC 313 ms
86,428 KB
testcase_22 AC 238 ms
87,412 KB
testcase_23 AC 200 ms
78,768 KB
testcase_24 AC 76 ms
71,308 KB
testcase_25 AC 361 ms
88,848 KB
testcase_26 AC 75 ms
71,272 KB
testcase_27 AC 74 ms
71,156 KB
testcase_28 AC 75 ms
71,180 KB
testcase_29 AC 75 ms
71,232 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Graph:

    def __init__(self, n, directed=False, decrement=True, edges=[]):
        self.n = n
        self.directed = directed
        self.decrement = decrement
        self.edges = [[] for _ in range(self.n)]
        for x, y, cost in edges:
            self.add_edge(x, y, cost)

    def add_edge(self, x, y, cost):
        if self.decrement:
            x -= 1
            y -= 1
        self.edges[x].append((y, cost))
        if self.directed == False:
            self.edges[y].append((x, cost))

    def dijkstra(self, start=None, INF=10**18):
        """
        返り値は 0-indexed !!!
        :param start: スタート地点
        :return: スタート地点から各点への距離のリスト
        備考: heqpq の比較のための key は第一引数である点に注意( = heappush(heapq, (key,value)) )
        """
        res = [INF] * self.n
        if start is None: start=self.decrement
        start=start-self.decrement
        res[start] = 0
        next_set = [(0, start)]
        while next_set:
            dist, p = heappop(next_set)
            if res[p] < dist:
                continue
            """ここで頂点pまでの最短距離が確定。よって、ここを通るのはN回のみ"""
            for q, cost in self.edges[p]:
                temp_d = dist + cost
                if temp_d < res[q]:
                    res[q] = temp_d
                    heappush(next_set, (temp_d, q))

        return res

    def draw(self):
        """
        :return: グラフを可視化
        """
        import matplotlib.pyplot as plt
        import networkx as nx

        if self.directed:
            G = nx.DiGraph()
        else:
            G = nx.Graph()
        for x in range(self.n):
            for y, cost in self.edges[x]:
                G.add_edge(x + self.decrement, y + self.decrement, weight=cost)


        edge_labels = {(i, j): w['weight'] for i, j, w in G.edges(data=True)}
        pos = nx.spring_layout(G)
        nx.draw_networkx(G, pos, with_labels=True, connectionstyle='arc3, rad = 0.1')
        nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels)
        plt.axis("off")
        plt.show()

#########################################################
def example():
    global input
    example = iter(
        """
3 3
1 2 1
1 3 1
2 3 3
        """
            .strip().split("\n"))
    input = lambda: next(example)

def example2():
    global input
    example = iter(
        """
6 9
1 2 7
2 3 10
1 3 9
1 6 14
3 6 2
5 6 9
4 5 6
2 4 15
3 4 11
        """
            .strip().split("\n"))
    input = lambda: next(example)

#########################################################
import sys
from heapq import *
input = sys.stdin.readline


INF = 10**18  # 大きい数字

N, M, P, Q, T = map(int, input().split())

graph = Graph(N, directed=False, decrement=True)
for _ in range(M):
    x, y, cost = map(int, input().split())
    graph.add_edge(x, y, cost)

dist = graph.dijkstra(start=1,INF=INF)
distP = graph.dijkstra(start=P,INF=INF)
distQ = graph.dijkstra(start=Q,INF=INF)




res=-1
for i in range(N):
    for j in range(N):
        off=max(distP[i]+distP[j],distQ[i]+distQ[j])
        if dist[i]+dist[j]+off<=T:
            res=max(T-off,res)

if distP[0]+distQ[0]+distP[Q-1]<=T:
    res=T

print(res)
0