結果

問題 No.160 最短経路のうち辞書順最小
ユーザー 双六双六
提出日時 2020-07-23 17:23:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 3,728 ms / 5,000 ms
コード長 1,548 bytes
コンパイル時間 290 ms
コンパイル使用メモリ 87,024 KB
実行使用メモリ 93,592 KB
最終ジャッジ日時 2023-09-05 22:26:38
合計ジャッジ時間 9,293 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 92 ms
71,344 KB
testcase_01 AC 91 ms
71,344 KB
testcase_02 AC 91 ms
71,880 KB
testcase_03 AC 94 ms
71,964 KB
testcase_04 AC 124 ms
77,784 KB
testcase_05 AC 127 ms
77,948 KB
testcase_06 AC 134 ms
79,012 KB
testcase_07 AC 137 ms
78,476 KB
testcase_08 AC 141 ms
79,432 KB
testcase_09 AC 113 ms
77,560 KB
testcase_10 AC 143 ms
78,504 KB
testcase_11 AC 146 ms
78,580 KB
testcase_12 AC 144 ms
78,872 KB
testcase_13 AC 126 ms
77,764 KB
testcase_14 AC 146 ms
78,324 KB
testcase_15 AC 140 ms
78,332 KB
testcase_16 AC 113 ms
77,688 KB
testcase_17 AC 142 ms
78,824 KB
testcase_18 AC 129 ms
77,788 KB
testcase_19 AC 114 ms
77,480 KB
testcase_20 AC 110 ms
77,776 KB
testcase_21 AC 136 ms
78,900 KB
testcase_22 AC 141 ms
78,928 KB
testcase_23 AC 131 ms
78,532 KB
testcase_24 AC 119 ms
77,696 KB
testcase_25 AC 123 ms
77,776 KB
testcase_26 AC 126 ms
78,148 KB
testcase_27 AC 99 ms
76,772 KB
testcase_28 AC 3,728 ms
93,592 KB
testcase_29 AC 97 ms
72,472 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys; input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**7)
from collections import defaultdict
from heapq import heappop, heappush
INF = float('inf')

def getlist():
	return list(map(int, input().split()))

class Graph(object):
	def __init__(self):
		self.graph = defaultdict(list)

	def __len__(self):
		return len(self.graph)

	def add_edge(self, a, b, w):
		self.graph[a].append((b, w))

class Dijkstra(object):
	def __init__(self, graph, s):
		self.g = graph.graph
		self.dist = defaultdict(lambda: INF); self.dist[s] = 0
		self.prev = defaultdict(lambda: None)
		self.dic = defaultdict(lambda:[10 ** 10])
		self.dic[s].append(s)
		self.Q = []
		heappush(self.Q, (self.dist[s], s))
		while self.Q:
			dist_u, u = heappop(self.Q)
			if self.dist[u] < dist_u:
				continue
			for v, w in self.g[u]:
				alt = dist_u + w
				if self.dist[v] > alt:
					self.dist[v] = alt
					self.prev[v] = u
					self.dic[v] = self.dic[u] + [v]
					heappush(self.Q, (alt, v))
				elif self.dist[v] == alt:
					if self.dic[u] + [v] < self.dic[v]:
						self.prev[v] = u
						self.dic[v] = self.dic[u] + [v]
						heappush(self.Q, (alt, v))

	def s_p(self, goal):
		path = []
		node = goal
		while node is not None:
			path.append(node)
			node = self.prev[node]
		return path[::-1]

#処理内容
def main():
	N, M, s, g = getlist()
	G = Graph()
	for i in range(M):
		a, b, w = getlist()
		G.add_edge(a, b, w)
		G.add_edge(b, a, w)

	D = Dijkstra(G, s)
	ans = D.dic[g][1:]
	# print(D.dic[g])
	print(*ans)




if __name__ == '__main__':
	main()
0