結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー rlangevinrlangevin
提出日時 2024-02-10 13:32:17
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,018 bytes
コンパイル時間 359 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 223,668 KB
最終ジャッジ日時 2024-02-10 13:32:37
合計ジャッジ時間 18,818 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
60,264 KB
testcase_01 AC 36 ms
53,460 KB
testcase_02 AC 35 ms
53,460 KB
testcase_03 AC 37 ms
53,460 KB
testcase_04 AC 35 ms
53,460 KB
testcase_05 AC 43 ms
63,788 KB
testcase_06 AC 44 ms
65,836 KB
testcase_07 AC 179 ms
76,432 KB
testcase_08 AC 240 ms
77,360 KB
testcase_09 AC 1,806 ms
140,152 KB
testcase_10 AC 243 ms
77,164 KB
testcase_11 AC 1,556 ms
112,924 KB
testcase_12 AC 501 ms
80,864 KB
testcase_13 AC 949 ms
91,092 KB
testcase_14 AC 130 ms
76,572 KB
testcase_15 AC 251 ms
77,292 KB
testcase_16 AC 109 ms
76,484 KB
testcase_17 AC 131 ms
76,176 KB
testcase_18 AC 126 ms
76,176 KB
testcase_19 AC 664 ms
83,760 KB
testcase_20 AC 93 ms
76,184 KB
testcase_21 AC 1,011 ms
96,592 KB
testcase_22 AC 36 ms
53,460 KB
testcase_23 AC 35 ms
53,460 KB
testcase_24 AC 35 ms
53,460 KB
testcase_25 AC 35 ms
53,460 KB
testcase_26 AC 34 ms
53,460 KB
testcase_27 AC 35 ms
53,460 KB
testcase_28 AC 163 ms
76,460 KB
testcase_29 AC 1,051 ms
92,916 KB
testcase_30 AC 58 ms
68,636 KB
testcase_31 AC 243 ms
77,440 KB
testcase_32 AC 36 ms
53,460 KB
testcase_33 AC 1,671 ms
132,448 KB
testcase_34 AC 896 ms
98,780 KB
testcase_35 AC 113 ms
76,908 KB
testcase_36 AC 83 ms
76,484 KB
testcase_37 AC 58 ms
68,752 KB
testcase_38 AC 110 ms
76,480 KB
testcase_39 AC 87 ms
76,240 KB
testcase_40 AC 35 ms
53,460 KB
testcase_41 AC 35 ms
53,460 KB
testcase_42 AC 34 ms
53,460 KB
testcase_43 AC 386 ms
78,588 KB
testcase_44 AC 235 ms
77,168 KB
testcase_45 TLE -
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 #

import sys
input = sys.stdin.readline

from heapq import heappush, heappop
inf = float('inf')
def dijkstra(s, g, G, t):
    # ゴールがない場合はg=-1とする。

    N = len(G)

    def cost(v, m):
        return v * N + m
    def cost2(v, m):
        return v * (N+1) + m

    dist = [inf] * N
    mindist = [inf] * N
    seen = [False] * N
    Edge = set()
    Q = [cost2(cost(0, s), 0)]
    while Q:
        c, p = divmod(heappop(Q), N+1)
        p -= 1
        c, m = divmod(c, N)
        if seen[m]:
            continue
        seen[m] = True
        dist[m] = c
        if t == 0:
            Edge.add((p, m))
            Edge.add((m, p))
        else:
            Edge.add((p, m))

        #------heapをアップデートする。--------
        for u, C in G[m]:
            if seen[u]:
                continue
            newdist = dist[m] + C

            #------------------------------------
            if newdist >= mindist[u]:
                continue
            mindist[u] = newdist
            heappush(Q, cost2(cost(newdist, u), m+1))
    return dist, Edge

T = int(input())
N, M = map(int, input().split())
G = [[] for i in range(N)]
E = []
if T == 0:
    for i in range(M):
        u, v, c = map(int, input().split())
        u, v = u - 1, v - 1
        G[u].append((v, c))
        G[v].append((u, c))
        E.append((u, v, c))

    ans = inf
    for s in range(N):
        D, Edge = dijkstra(s, -1, G, T)
        for u, v, c in E:
            if (u, v) in Edge:
                continue
            ans = min(ans, D[u] + D[v] + c)
else:
    for i in range(M):
        u, v, c = map(int, input().split())
        u, v = u - 1, v - 1
        G[u].append((v, c))
        E.append((u, v, c))

    ans = inf
    for s in range(N):
        D, Edge = dijkstra(s, -1, G, T)
        for u, v, c in E:
            if (u, v) in Edge:
                continue
            if v != s:
                continue
            ans = min(ans, D[u] + c)
        
print(ans) if ans != inf else print(-1)
0