結果

問題 No.1320 Two Type Min Cost Cycle
コンテスト
ユーザー tamato
提出日時 2020-12-17 10:31:13
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 918 ms / 2,000 ms
コード長 1,424 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 340 ms
コンパイル使用メモリ 85,376 KB
実行使用メモリ 87,512 KB
最終ジャッジ日時 2026-04-08 18:37:34
合計ジャッジ時間 13,305 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 57
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.buffer.readline

    import heapq

    def dijkstra(adj, start, u0, v0):
        # adj: [[to, cost] * vertices], 0th index must be empty
        inf = 1 << 60
        dist = [inf] * len(adj)
        dist[start] = 0
        q = []
        heapq.heappush(q, (0, start))
        while q:
            min_dist, v_from = heapq.heappop(q)
            if min_dist > dist[v_from]:
                continue
            v_tos = adj[v_from]
            for v_to, cost in v_tos:
                if v_from == u0 and v_to == v0:
                    continue
                if v_from == v0 and v_to == u0:
                    continue
                if min_dist + cost < dist[v_to]:
                    dist[v_to] = min_dist + cost
                    heapq.heappush(q, (dist[v_to], v_to))
        return dist

    T = int(input())
    N, M = map(int, input().split())
    adj = [[] for _ in range(N+1)]
    E = []
    for _ in range(M):
        a, b, c = map(int, input().split())
        adj[a].append((b, c))
        if T == 0:
            adj[b].append((a, c))
        E.append((a, b, c))

    ans = 10**18
    for u0, v0, c in E:
        dist = dijkstra(adj, v0, u0, v0)
        if dist[u0] < 1 << 60:
            ans = min(ans, dist[u0] + c)
    if ans < 10**18:
        print(ans)
    else:
        print(-1)


if __name__ == '__main__':
    main()
0