結果

問題 No.1320 Two Type Min Cost Cycle
コンテスト
ユーザー norioc
提出日時 2026-08-23 22:41:44
言語 PyPy3
(7.3.23)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 1,218 ms / 2,000 ms
+ 886µs
コード長 1,031 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 240 ms
コンパイル使用メモリ 96,236 KB
実行使用メモリ 93,060 KB
最終ジャッジ日時 2026-08-23 22:42:07
合計ジャッジ時間 19,588 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 57
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from collections import defaultdict
from heapq import heappush, heappop

INF = 1 << 60
T = int(input())
N, M = map(int, input().split())

adj = defaultdict(list)
for _ in range(M):
    U, V, W = map(int, input().split())
    U -= 1
    V -= 1
    adj[U].append((V, W))
    if T == 0:
        adj[V].append((U, W))


def solve():
    res = INF

    for s in range(N):
        for t, tw in adj[s]:
            # 辺 (s, t) を除外し、t から s への最短距離を求める
            dists = [INF] * N
            dists[t] = 0
            q = [(0, t)]
            while q:
                d, v = heappop(q)
                if dists[v] != d: continue

                for to, w in adj[v]:
                    if (v, to) == (t, s): continue

                    nd = dists[v] + w
                    if dists[to] > nd:
                        dists[to] = nd
                        heappush(q, (nd, to))

            res = min(res, dists[s] + tw)

    if res == INF:
        return -1
    return res


ans = solve()
print(ans)
0