結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー ningenMeningenMe
提出日時 2020-12-18 02:35:47
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,355 ms / 2,000 ms
コード長 849 bytes
コンパイル時間 233 ms
コンパイル使用メモリ 81,732 KB
実行使用メモリ 82,096 KB
最終ジャッジ日時 2023-10-21 07:47:36
合計ジャッジ時間 17,147 ms
ジャッジサーバーID
(参考情報)
judge9 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
53,548 KB
testcase_01 AC 45 ms
53,548 KB
testcase_02 AC 40 ms
53,548 KB
testcase_03 AC 40 ms
53,548 KB
testcase_04 AC 41 ms
53,548 KB
testcase_05 AC 55 ms
65,288 KB
testcase_06 AC 56 ms
67,336 KB
testcase_07 AC 108 ms
76,116 KB
testcase_08 AC 151 ms
76,696 KB
testcase_09 AC 889 ms
80,212 KB
testcase_10 AC 126 ms
76,452 KB
testcase_11 AC 745 ms
79,312 KB
testcase_12 AC 258 ms
77,852 KB
testcase_13 AC 451 ms
78,484 KB
testcase_14 AC 103 ms
75,944 KB
testcase_15 AC 127 ms
76,512 KB
testcase_16 AC 100 ms
75,944 KB
testcase_17 AC 93 ms
75,996 KB
testcase_18 AC 88 ms
75,892 KB
testcase_19 AC 396 ms
77,808 KB
testcase_20 AC 84 ms
75,948 KB
testcase_21 AC 599 ms
78,084 KB
testcase_22 AC 41 ms
53,548 KB
testcase_23 AC 41 ms
53,548 KB
testcase_24 AC 42 ms
53,548 KB
testcase_25 AC 44 ms
53,548 KB
testcase_26 AC 39 ms
53,548 KB
testcase_27 AC 42 ms
53,548 KB
testcase_28 AC 108 ms
76,264 KB
testcase_29 AC 184 ms
76,280 KB
testcase_30 AC 56 ms
64,360 KB
testcase_31 AC 95 ms
76,032 KB
testcase_32 AC 42 ms
53,548 KB
testcase_33 AC 350 ms
76,936 KB
testcase_34 AC 301 ms
76,948 KB
testcase_35 AC 106 ms
75,992 KB
testcase_36 AC 74 ms
70,612 KB
testcase_37 AC 58 ms
64,368 KB
testcase_38 AC 113 ms
76,164 KB
testcase_39 AC 80 ms
73,360 KB
testcase_40 AC 41 ms
53,548 KB
testcase_41 AC 41 ms
53,548 KB
testcase_42 AC 40 ms
53,548 KB
testcase_43 AC 162 ms
76,940 KB
testcase_44 AC 142 ms
76,708 KB
testcase_45 AC 1,355 ms
82,096 KB
testcase_46 AC 90 ms
75,712 KB
testcase_47 AC 359 ms
76,980 KB
testcase_48 AC 619 ms
79,648 KB
testcase_49 AC 109 ms
76,132 KB
testcase_50 AC 41 ms
53,548 KB
testcase_51 AC 41 ms
53,548 KB
testcase_52 AC 221 ms
76,580 KB
testcase_53 AC 151 ms
76,500 KB
testcase_54 AC 711 ms
79,336 KB
testcase_55 AC 692 ms
79,320 KB
testcase_56 AC 699 ms
79,232 KB
testcase_57 AC 950 ms
81,172 KB
testcase_58 AC 957 ms
80,960 KB
testcase_59 AC 970 ms
80,936 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

INF = 10**16
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(w * 3000 + v-1)
	if t == 0:
		adj[v-1].append(w * 3000 + u-1)

ans = INF
for x in range(n):
	dist = [INF for _ in range(n)]
	prev = [-1 for _ in range(n)]
	dist[x] = 0
	q = [x]
	while q:
		z = heappop(q)
		i, c0 = z % 3000, z // 3000
		if dist[i] != c0:
		    continue;
		for y in adj[i]:
			j, c1 = y % 3000, y // 3000
			if t == 0 and prev[i] == j:
				continue
			if dist[j] > c0 + c1:
				prev[j] = i
				dist[j] = c0 + c1
				heappush(q, j + (c0 + c1) * 3000)
			else:
				if t == 0 or j == x:
					ans = min(ans, c0 + dist[j] + c1)

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