結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー mkawa2mkawa2
提出日時 2020-12-19 10:40:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 828 ms / 2,000 ms
コード長 1,410 bytes
コンパイル時間 175 ms
コンパイル使用メモリ 81,592 KB
実行使用メモリ 81,924 KB
最終ジャッジ日時 2023-10-21 09:06:06
合計ジャッジ時間 11,892 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
53,504 KB
testcase_01 AC 38 ms
53,504 KB
testcase_02 AC 38 ms
53,504 KB
testcase_03 AC 38 ms
53,504 KB
testcase_04 AC 38 ms
53,504 KB
testcase_05 AC 38 ms
53,504 KB
testcase_06 AC 36 ms
53,504 KB
testcase_07 AC 55 ms
74,840 KB
testcase_08 AC 61 ms
75,744 KB
testcase_09 AC 251 ms
77,812 KB
testcase_10 AC 64 ms
76,124 KB
testcase_11 AC 238 ms
78,228 KB
testcase_12 AC 149 ms
78,180 KB
testcase_13 AC 171 ms
78,152 KB
testcase_14 AC 47 ms
63,016 KB
testcase_15 AC 70 ms
76,168 KB
testcase_16 AC 151 ms
76,744 KB
testcase_17 AC 47 ms
69,160 KB
testcase_18 AC 46 ms
67,112 KB
testcase_19 AC 190 ms
78,200 KB
testcase_20 AC 44 ms
57,600 KB
testcase_21 AC 415 ms
78,408 KB
testcase_22 AC 36 ms
53,504 KB
testcase_23 AC 38 ms
53,504 KB
testcase_24 AC 36 ms
53,504 KB
testcase_25 AC 37 ms
53,504 KB
testcase_26 AC 36 ms
53,504 KB
testcase_27 AC 36 ms
53,504 KB
testcase_28 AC 43 ms
67,112 KB
testcase_29 AC 114 ms
77,040 KB
testcase_30 AC 50 ms
61,896 KB
testcase_31 AC 75 ms
75,580 KB
testcase_32 AC 37 ms
53,504 KB
testcase_33 AC 176 ms
77,260 KB
testcase_34 AC 125 ms
76,936 KB
testcase_35 AC 120 ms
76,496 KB
testcase_36 AC 116 ms
76,412 KB
testcase_37 AC 85 ms
76,048 KB
testcase_38 AC 114 ms
76,440 KB
testcase_39 AC 104 ms
76,192 KB
testcase_40 AC 37 ms
53,504 KB
testcase_41 AC 36 ms
53,504 KB
testcase_42 AC 36 ms
53,504 KB
testcase_43 AC 84 ms
76,832 KB
testcase_44 AC 74 ms
76,384 KB
testcase_45 AC 180 ms
78,340 KB
testcase_46 AC 71 ms
75,524 KB
testcase_47 AC 153 ms
77,116 KB
testcase_48 AC 120 ms
77,220 KB
testcase_49 AC 61 ms
72,228 KB
testcase_50 AC 36 ms
53,504 KB
testcase_51 AC 35 ms
53,504 KB
testcase_52 AC 79 ms
75,812 KB
testcase_53 AC 59 ms
72,296 KB
testcase_54 AC 656 ms
80,820 KB
testcase_55 AC 681 ms
80,452 KB
testcase_56 AC 698 ms
81,248 KB
testcase_57 AC 788 ms
81,604 KB
testcase_58 AC 828 ms
81,924 KB
testcase_59 AC 802 ms
81,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(10**6)
int1 = lambda x: int(x)-1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.buffer.readline())
def MI(): return map(int, sys.stdin.buffer.readline().split())
def MI1(): return map(int1, sys.stdin.buffer.readline().split())
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def LI1(): return list(map(int1, sys.stdin.buffer.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def BI(): return sys.stdin.buffer.readline().rstrip()
def SI(): return sys.stdin.buffer.readline().rstrip().decode()
dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
inf = 10**16
# md = 998244353
md = 10**9+7

from heapq import *

t=II()
n,m=MI()
to=[[] for _ in range(n)]
uvw=[]
for i in range(m):
    u,v,w=MI()
    u,v=u-1,v-1
    to[u].append((v,w,i))
    if t==0:
        to[v].append((u,w,i))
    uvw.append((u,v,w))

def solve(s,g):
    dd=[inf]*n
    hp=[]
    heappush(hp,(0,s))
    dd[s]=0
    while hp:
        d,u=heappop(hp)
        if u==g:return d
        if d>dd[u]:continue
        for v,w,i in to[u]:
            if fin[i]:continue
            if dd[v]<=d+w:continue
            dd[v]=d+w
            heappush(hp,(d+w,v))
    return inf

fin=[False]*m
ans=inf
for i,(u,v,w) in enumerate(uvw):
    if fin[i]:continue
    fin[i]=True
    ret=solve(v,u)+w
    if ret<ans:ans=ret

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