結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー 👑 tamatotamato
提出日時 2020-12-17 10:31:13
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,011 ms / 2,000 ms
コード長 1,424 bytes
コンパイル時間 172 ms
コンパイル使用メモリ 81,848 KB
実行使用メモリ 82,372 KB
最終ジャッジ日時 2023-10-20 11:22:43
合計ジャッジ時間 16,243 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
53,636 KB
testcase_01 AC 41 ms
53,636 KB
testcase_02 AC 41 ms
53,636 KB
testcase_03 AC 40 ms
53,636 KB
testcase_04 AC 41 ms
53,636 KB
testcase_05 AC 42 ms
53,636 KB
testcase_06 AC 42 ms
53,636 KB
testcase_07 AC 73 ms
75,832 KB
testcase_08 AC 99 ms
76,224 KB
testcase_09 AC 939 ms
81,236 KB
testcase_10 AC 83 ms
76,328 KB
testcase_11 AC 739 ms
79,980 KB
testcase_12 AC 205 ms
77,348 KB
testcase_13 AC 403 ms
78,212 KB
testcase_14 AC 48 ms
63,204 KB
testcase_15 AC 89 ms
76,240 KB
testcase_16 AC 214 ms
76,916 KB
testcase_17 AC 57 ms
69,652 KB
testcase_18 AC 53 ms
67,488 KB
testcase_19 AC 494 ms
78,576 KB
testcase_20 AC 46 ms
57,732 KB
testcase_21 AC 1,011 ms
80,760 KB
testcase_22 AC 41 ms
53,636 KB
testcase_23 AC 42 ms
53,636 KB
testcase_24 AC 42 ms
53,636 KB
testcase_25 AC 41 ms
53,636 KB
testcase_26 AC 41 ms
53,636 KB
testcase_27 AC 42 ms
53,636 KB
testcase_28 AC 52 ms
67,552 KB
testcase_29 AC 141 ms
76,488 KB
testcase_30 AC 52 ms
62,168 KB
testcase_31 AC 81 ms
75,696 KB
testcase_32 AC 42 ms
53,636 KB
testcase_33 AC 307 ms
77,108 KB
testcase_34 AC 180 ms
76,436 KB
testcase_35 AC 168 ms
76,376 KB
testcase_36 AC 136 ms
76,428 KB
testcase_37 AC 106 ms
76,200 KB
testcase_38 AC 181 ms
76,580 KB
testcase_39 AC 156 ms
76,880 KB
testcase_40 AC 42 ms
53,636 KB
testcase_41 AC 43 ms
53,636 KB
testcase_42 AC 42 ms
53,636 KB
testcase_43 AC 124 ms
76,700 KB
testcase_44 AC 113 ms
76,880 KB
testcase_45 AC 906 ms
81,488 KB
testcase_46 AC 78 ms
75,812 KB
testcase_47 AC 157 ms
76,556 KB
testcase_48 AC 607 ms
79,836 KB
testcase_49 AC 91 ms
75,912 KB
testcase_50 AC 42 ms
53,636 KB
testcase_51 AC 41 ms
53,636 KB
testcase_52 AC 219 ms
76,600 KB
testcase_53 AC 160 ms
76,744 KB
testcase_54 AC 768 ms
81,980 KB
testcase_55 AC 772 ms
81,668 KB
testcase_56 AC 769 ms
81,532 KB
testcase_57 AC 927 ms
82,372 KB
testcase_58 AC 932 ms
82,340 KB
testcase_59 AC 927 ms
82,256 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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