結果

問題 No.807 umg tours
ユーザー anagohirameanagohirame
提出日時 2019-03-22 22:14:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,910 ms / 4,000 ms
コード長 1,431 bytes
コンパイル時間 500 ms
コンパイル使用メモリ 87,204 KB
実行使用メモリ 238,736 KB
最終ジャッジ日時 2023-08-15 12:00:43
合計ジャッジ時間 21,514 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 86 ms
75,940 KB
testcase_01 AC 86 ms
75,780 KB
testcase_02 AC 89 ms
75,772 KB
testcase_03 AC 89 ms
75,664 KB
testcase_04 AC 86 ms
75,668 KB
testcase_05 AC 85 ms
75,572 KB
testcase_06 AC 87 ms
75,560 KB
testcase_07 AC 89 ms
75,784 KB
testcase_08 AC 77 ms
71,236 KB
testcase_09 AC 77 ms
71,088 KB
testcase_10 AC 80 ms
71,008 KB
testcase_11 AC 1,187 ms
196,352 KB
testcase_12 AC 1,247 ms
166,484 KB
testcase_13 AC 1,465 ms
204,248 KB
testcase_14 AC 694 ms
130,940 KB
testcase_15 AC 549 ms
120,304 KB
testcase_16 AC 1,625 ms
212,416 KB
testcase_17 AC 1,821 ms
233,984 KB
testcase_18 AC 1,910 ms
233,404 KB
testcase_19 AC 1,829 ms
229,456 KB
testcase_20 AC 986 ms
155,908 KB
testcase_21 AC 960 ms
160,036 KB
testcase_22 AC 477 ms
113,416 KB
testcase_23 AC 409 ms
106,588 KB
testcase_24 AC 752 ms
209,048 KB
testcase_25 AC 1,828 ms
238,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys, heapq
def input():
	return sys.stdin.readline()[:-1]

class DijkstraList():
	#隣接リスト版
	#同一頂点の複数回探索を防ぐため訪問した頂点数を変数cntで持つ
	def __init__(self, adj, start):
		self.list = adj
		self.start = start
		self.size = len(adj)

	def solve(self):
		self.dist = [float("inf") for _ in range(self.size)]
		self.dist[self.start] = 0
		self.prev = [-1 for _ in range(self.size)]
		self.q = []
		self.cnt = 0

		heapq.heappush(self.q, (0, self.start))

		while self.q and self.cnt < self.size:
			u_dist, u = heapq.heappop(self.q)
			if self.dist[u] < u_dist:
				continue
			for v, w in self.list[u]:
				if self.dist[v] > u_dist + w:
					self.dist[v] = u_dist + w
					self.prev[v] = u
					heapq.heappush(self.q, (self.dist[v], v))
			self.cnt += 1
		return

	def distance(self, goal):
		return self.dist[goal]

	def path(self, goal):
		self.path = [goal]
		while self.path[-1] != self.start:
			self.path.append(self.prev[self.path[-1]])
		return self.path[::-1]

n, m = map(int, input().split())
adj = [[] for _ in range(2*n)]
for _ in range(m):
	a, b, c = map(int, input().split())
	adj[a-1].append([b-1, c])
	adj[b-1].append([a-1, c])
	adj[a-1].append([b-1+n, 0])
	adj[b-1].append([a-1+n, 0])
	adj[a-1+n].append([b-1+n, c])
	adj[b-1+n].append([a-1+n, c])

d = DijkstraList(adj, 0)
d.solve()

print(0)
for i in range(1, n):
	print(d.distance(i) + d.distance(i+n))
0