結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー tktk_snsn
提出日時 2020-12-30 23:28:55
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 856 ms / 2,000 ms
コード長 952 bytes
コンパイル時間 274 ms
コンパイル使用メモリ 82,576 KB
実行使用メモリ 82,740 KB
最終ジャッジ日時 2024-10-08 07:38:07
合計ジャッジ時間 14,203 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 57
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import heapq
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)
inf = 10**18

T = int(input())
N, M = map(int, input().split())
edge = []
G = [[] for _ in range(N)]
for i in range(M):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    edge.append((a, b, c))
    G[a].append((b, c))
    if T == 0:
        G[b].append((a, c))


def dijkstra(i):
    s, t, c = edge[i]
    dist = [inf] * N
    dist[t] = 0
    que = [(0, t)]
    while que:
        dv, v = heapq.heappop(que)
        if dist[v] < dv:
            continue
        if v == s:
            break
        for u, du in G[v]:
            if (v, u) in ((s, t), (t, s)):
                continue
            if dist[u] > dv + du:
                dist[u] = dv + du
                heapq.heappush(que, (dv + du, u))
    return c + dist[s]


ans = inf
for i in range(M):
    d = dijkstra(i)
    if d < ans:
        ans = d
if ans >= inf:
    ans = -1
print(ans)
0