結果

問題 No.614 壊れたキャンパス
ユーザー 双六双六
提出日時 2020-08-10 05:01:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,871 ms / 2,000 ms
コード長 1,565 bytes
コンパイル時間 149 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 272,892 KB
最終ジャッジ日時 2024-04-15 20:30:36
合計ジャッジ時間 17,672 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 50 ms
54,144 KB
testcase_01 AC 49 ms
54,400 KB
testcase_02 AC 47 ms
54,272 KB
testcase_03 AC 47 ms
54,656 KB
testcase_04 AC 46 ms
54,144 KB
testcase_05 AC 47 ms
54,400 KB
testcase_06 AC 47 ms
54,656 KB
testcase_07 AC 47 ms
54,272 KB
testcase_08 AC 1,683 ms
271,692 KB
testcase_09 AC 1,533 ms
272,892 KB
testcase_10 AC 621 ms
184,924 KB
testcase_11 AC 1,764 ms
269,856 KB
testcase_12 AC 1,871 ms
272,428 KB
testcase_13 AC 1,840 ms
271,408 KB
testcase_14 AC 1,646 ms
267,884 KB
testcase_15 AC 619 ms
189,300 KB
testcase_16 AC 1,534 ms
267,128 KB
testcase_17 AC 869 ms
269,148 KB
testcase_18 AC 807 ms
257,012 KB
testcase_19 AC 554 ms
236,380 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.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
					heappush(self.Q, (alt, v))


#処理内容
def main():
	N, M, K, S, T = getlist()
	node = []
	node.append(S - 1)
	node.append((N - 1) * K + T - 1)
	G = Graph()
	for i in range(M):
		a, b, c = getlist()
		a -= 1; b -= 1; c -= 1
		G.add_edge(a * K + b, (a + 1) * K + c, 0)
		node.append(a * K + b)
		node.append((a + 1) * K + c)

	node.sort()
	# print(node)
	for i in range(len(node) - 1):
		s, t = node[i], node[i + 1]
		at = int(s // K)
		af = s % K
		bt = int(t // K)
		bf = t % K
		if at == bt:
			dis = abs(s - t)
			G.add_edge(s, t, dis)
			G.add_edge(t, s, dis)

	path = Dijkstra(G, S - 1)
	ans = path.dist[(N - 1) * K + T - 1]
	if ans == INF:
		print(-1)
	else:
		print(ans)


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