結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー ningenMeningenMe
提出日時 2020-12-02 20:59:04
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 2,441 bytes
コンパイル時間 205 ms
コンパイル使用メモリ 12,100 KB
実行使用メモリ 15,300 KB
最終ジャッジ日時 2023-10-20 05:40:28
合計ジャッジ時間 5,058 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
10,300 KB
testcase_01 AC 31 ms
10,300 KB
testcase_02 AC 30 ms
10,300 KB
testcase_03 AC 31 ms
10,300 KB
testcase_04 AC 30 ms
10,300 KB
testcase_05 AC 64 ms
10,332 KB
testcase_06 AC 43 ms
10,336 KB
testcase_07 AC 143 ms
10,564 KB
testcase_08 AC 762 ms
10,648 KB
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3

import sys
import heapq
input = sys.stdin.readline
inf = 1e15

def solve_undirected_graph():
    N,M = map(int,input().split())
    edges = [[] for i in range(N)]
    for i in range(M):
        u,v,w =  map(int,input().split())
        u-=1
        v-=1
        edges[u].append((v,w))
        edges[v].append((u,w))
    ans = inf
    for root in range(N):
        dist  = [inf for i in range(N)]
        label = [-1 for i in range(N)]
        q = [(0,root)]
        dist[root]  = 0
        label[root] = root

        while len(q) > 0:
            cost,from_node = heapq.heappop(q)
            if cost > dist[from_node]:
                continue
            for to_node,w in edges[from_node]:
                if dist[to_node] > cost + w:
                    dist[to_node] = cost + w
                    if from_node == root:
                        label[to_node] = to_node
                    else :
                        label[to_node] = label[from_node]
                    heapq.heappush(q,(cost+w,to_node))
        for from_node in range(N):
            for to_node,w in edges[from_node]:
                if from_node == root or to_node == root:
                    continue
                if label[from_node] == label[to_node]:
                    continue
                if ans > dist[from_node] + dist[to_node] + w:
                    ans = dist[from_node] + dist[to_node] + w
    return ans

def solve_directed_graph():
    N,M = map(int,input().split())
    edges = [[] for i in range(N)]
    for i in range(M):
        u,v,w =  map(int,input().split())
        u-=1
        v-=1
        edges[u].append((v,w))
    ans = inf
    for root in range(N):
        dist  = [inf for i in range(N)]
        q = [(0,root)]
        dist[root]  = 0

        while len(q) > 0:
            cost,from_node = heapq.heappop(q)
            if cost > dist[from_node]:
                continue
            for to_node,w in edges[from_node]:
                if dist[to_node] > cost + w:
                    dist[to_node] = cost + w
                    heapq.heappush(q,(cost+w,to_node))
                if to_node == root and ans > dist[from_node] + w:
                    ans = dist[from_node] + w
    return ans

def main():
    T = int(input())
    ans = inf
    if T == 0:
        ans = solve_undirected_graph()
    else:
        ans = solve_directed_graph()
    if ans == inf:
        ans = -1
    print(ans)

main()
0