結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー rlangevinrlangevin
提出日時 2024-02-10 13:34:46
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,939 bytes
コンパイル時間 3,701 ms
コンパイル使用メモリ 81,444 KB
実行使用メモリ 266,892 KB
最終ジャッジ日時 2024-02-10 13:35:27
合計ジャッジ時間 27,730 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 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 35 ms
53,460 KB
testcase_05 AC 53 ms
74,624 KB
testcase_06 AC 55 ms
74,752 KB
testcase_07 AC 154 ms
78,768 KB
testcase_08 AC 247 ms
79,936 KB
testcase_09 AC 1,677 ms
194,268 KB
testcase_10 AC 225 ms
80,384 KB
testcase_11 AC 1,348 ms
155,300 KB
testcase_12 AC 453 ms
89,392 KB
testcase_13 AC 873 ms
111,952 KB
testcase_14 AC 144 ms
79,028 KB
testcase_15 AC 203 ms
79,872 KB
testcase_16 AC 105 ms
76,348 KB
testcase_17 AC 118 ms
76,976 KB
testcase_18 AC 120 ms
77,104 KB
testcase_19 AC 650 ms
92,752 KB
testcase_20 AC 94 ms
76,448 KB
testcase_21 AC 995 ms
121,324 KB
testcase_22 AC 37 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 36 ms
53,460 KB
testcase_28 AC 152 ms
78,516 KB
testcase_29 AC 719 ms
155,988 KB
testcase_30 AC 54 ms
68,580 KB
testcase_31 AC 177 ms
81,728 KB
testcase_32 AC 36 ms
53,460 KB
testcase_33 AC 1,110 ms
178,300 KB
testcase_34 AC 660 ms
128,572 KB
testcase_35 AC 116 ms
76,932 KB
testcase_36 AC 84 ms
76,480 KB
testcase_37 AC 57 ms
68,616 KB
testcase_38 AC 109 ms
76,296 KB
testcase_39 AC 86 ms
76,120 KB
testcase_40 AC 36 ms
53,460 KB
testcase_41 AC 36 ms
53,460 KB
testcase_42 AC 35 ms
53,460 KB
testcase_43 AC 259 ms
81,288 KB
testcase_44 AC 181 ms
78,336 KB
testcase_45 TLE -
testcase_46 AC 163 ms
79,788 KB
testcase_47 AC 911 ms
154,260 KB
testcase_48 AC 1,144 ms
225,244 KB
testcase_49 AC 176 ms
79,148 KB
testcase_50 AC 37 ms
53,460 KB
testcase_51 AC 35 ms
53,460 KB
testcase_52 AC 320 ms
85,680 KB
testcase_53 AC 215 ms
79,776 KB
testcase_54 AC 857 ms
117,316 KB
testcase_55 AC 895 ms
117,188 KB
testcase_56 AC 905 ms
117,628 KB
testcase_57 AC 1,809 ms
265,792 KB
testcase_58 AC 1,846 ms
266,032 KB
testcase_59 AC 1,885 ms
266,680 KB
権限があれば一括ダウンロードができます

ソースコード

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

    dist = [inf] * N
    mindist = [inf] * N
    seen = [False] * N
    Edge = [set() for i in range(N)]
    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:
            Edge[p].add(m)
            Edge[m].add(p)
        else:
            Edge[p].add(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, (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:
    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 v in Edge[u]:
                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 v in Edge[u]:
                continue
            if v != s:
                continue
            ans = min(ans, D[u] + c)
        
print(ans) if ans != inf else print(-1)
0