結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー anagohirame
提出日時 2020-12-17 00:39:47
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,027 bytes
コンパイル時間 159 ms
コンパイル使用メモリ 82,352 KB
実行使用メモリ 86,412 KB
最終ジャッジ日時 2024-09-20 05:36:32
合計ジャッジ時間 13,480 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37 WA * 14 TLE * 1 -- * 5
権限があれば一括ダウンロードができます

ソースコード

diff #

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

def enc(pos, cost):
	return cost * 10000 + pos

def dec(x):
	return x % 10000, x // 10000

INF = 10**18
t = int(input())
n, m = map(int, input().split())
adj = [[] for _ in range(n)]
for _ in range(m):
	u, v, w = map(int, input().split())
	adj[u-1].append(enc(v-1, w))
	if t == 0:
		adj[v-1].append(enc(u-1, w))

ans = INF
for x in range(n):
	dist = [INF for _ in range(n)]
	prev = [-1 for _ in range(n)]
	visited = [False for _ in range(n)]
	dist[x] = 0
	q = [x]
	flg = False
	while q:
		i, c0 = dec(heappop(q))
		visited[i] = True
		#print(i, c0)
		for y in adj[i]:
			j, c1 = dec(y)
			if t == 0 and prev[i] == j:
				continue
			if visited[j] and j == x:
					#print("! " + str(j) + " " + str(c1))
					ans = min(ans, c0 + c1)
					flg = True
			elif dist[j] > c0 + c1:
				prev[j] = i
				dist[j] = c0 + c1
				heappush(q, enc(j, c0 + c1))
		if flg:
			break
	#print("-----")

if ans == INF:
	print(-1)
else:
	print(ans)
0