結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー rlangevinrlangevin
提出日時 2024-02-10 13:49:19
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,904 bytes
コンパイル時間 176 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 110,916 KB
最終ジャッジ日時 2024-02-10 13:49:44
合計ジャッジ時間 22,845 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,460 KB
testcase_01 AC 35 ms
53,460 KB
testcase_02 AC 35 ms
53,460 KB
testcase_03 AC 35 ms
53,460 KB
testcase_04 AC 34 ms
53,460 KB
testcase_05 AC 40 ms
61,740 KB
testcase_06 AC 41 ms
63,788 KB
testcase_07 AC 106 ms
76,300 KB
testcase_08 AC 192 ms
77,348 KB
testcase_09 AC 1,530 ms
98,176 KB
testcase_10 AC 131 ms
76,888 KB
testcase_11 AC 1,265 ms
94,076 KB
testcase_12 AC 368 ms
79,404 KB
testcase_13 AC 716 ms
84,664 KB
testcase_14 AC 108 ms
76,452 KB
testcase_15 AC 134 ms
76,888 KB
testcase_16 AC 106 ms
76,724 KB
testcase_17 AC 85 ms
75,976 KB
testcase_18 AC 86 ms
75,972 KB
testcase_19 AC 435 ms
79,820 KB
testcase_20 AC 90 ms
76,312 KB
testcase_21 AC 932 ms
85,744 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 34 ms
53,460 KB
testcase_26 AC 35 ms
53,460 KB
testcase_27 AC 35 ms
53,460 KB
testcase_28 AC 122 ms
76,576 KB
testcase_29 AC 316 ms
77,172 KB
testcase_30 AC 49 ms
64,460 KB
testcase_31 AC 105 ms
76,304 KB
testcase_32 AC 36 ms
53,460 KB
testcase_33 AC 1,023 ms
87,180 KB
testcase_34 AC 605 ms
82,948 KB
testcase_35 AC 100 ms
76,356 KB
testcase_36 AC 73 ms
73,920 KB
testcase_37 AC 56 ms
66,536 KB
testcase_38 AC 101 ms
76,472 KB
testcase_39 AC 84 ms
76,380 KB
testcase_40 AC 36 ms
53,460 KB
testcase_41 AC 34 ms
53,460 KB
testcase_42 AC 34 ms
53,460 KB
testcase_43 AC 170 ms
77,408 KB
testcase_44 AC 137 ms
76,888 KB
testcase_45 TLE -
testcase_46 AC 98 ms
76,304 KB
testcase_47 AC 849 ms
85,100 KB
testcase_48 AC 651 ms
82,792 KB
testcase_49 AC 120 ms
76,432 KB
testcase_50 AC 37 ms
53,460 KB
testcase_51 AC 34 ms
53,460 KB
testcase_52 AC 275 ms
78,212 KB
testcase_53 AC 190 ms
77,056 KB
testcase_54 AC 657 ms
83,748 KB
testcase_55 AC 650 ms
83,492 KB
testcase_56 AC 633 ms
83,764 KB
testcase_57 AC 1,525 ms
101,024 KB
testcase_58 AC 1,543 ms
101,924 KB
testcase_59 AC 1,557 ms
101,540 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

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

    N = len(G)

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

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


        #------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, (cost(newdist, u), m))
    return dist, Edge

T = int(input())
N, M = map(int, input().split())
G = [[] for i in range(N)]
E = []
if T == 0:
    DD = dict()
    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))
        DD[(u, v)] = i
        DD[(v, u)] = i

    ans = inf
    for s in range(N):
        D, Edge = dijkstra(s, T)
        for i in range(M):
            u, v, c = E[i]
            if i 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, T)
        for i in range(M):
            u, v, c = E[i]
            if v != s:
                continue
            ans = min(ans, D[u] + c)
        
print(ans) if ans != inf else print(-1)
0