結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
53,460 KB
testcase_01 AC 35 ms
53,460 KB
testcase_02 AC 34 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,880 KB
testcase_06 AC 54 ms
74,880 KB
testcase_07 AC 132 ms
77,860 KB
testcase_08 AC 198 ms
79,540 KB
testcase_09 AC 1,500 ms
187,000 KB
testcase_10 AC 166 ms
78,960 KB
testcase_11 AC 1,192 ms
146,560 KB
testcase_12 AC 352 ms
85,420 KB
testcase_13 AC 720 ms
107,252 KB
testcase_14 AC 130 ms
78,260 KB
testcase_15 AC 172 ms
78,832 KB
testcase_16 AC 104 ms
76,344 KB
testcase_17 AC 102 ms
76,580 KB
testcase_18 AC 105 ms
76,964 KB
testcase_19 AC 496 ms
90,496 KB
testcase_20 AC 86 ms
76,320 KB
testcase_21 AC 1,013 ms
124,268 KB
testcase_22 AC 35 ms
53,460 KB
testcase_23 AC 35 ms
53,460 KB
testcase_24 AC 46 ms
53,460 KB
testcase_25 AC 34 ms
53,460 KB
testcase_26 AC 34 ms
53,460 KB
testcase_27 AC 35 ms
53,460 KB
testcase_28 AC 135 ms
77,876 KB
testcase_29 AC 547 ms
133,660 KB
testcase_30 AC 51 ms
66,516 KB
testcase_31 AC 142 ms
80,088 KB
testcase_32 AC 35 ms
53,460 KB
testcase_33 AC 996 ms
165,288 KB
testcase_34 AC 595 ms
124,512 KB
testcase_35 AC 97 ms
76,220 KB
testcase_36 AC 77 ms
76,344 KB
testcase_37 AC 56 ms
66,528 KB
testcase_38 AC 101 ms
76,220 KB
testcase_39 AC 81 ms
76,128 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 208 ms
79,608 KB
testcase_44 AC 158 ms
77,680 KB
testcase_45 TLE -
testcase_46 AC 153 ms
78,548 KB
testcase_47 AC 822 ms
146,360 KB
testcase_48 AC 958 ms
209,532 KB
testcase_49 AC 148 ms
78,516 KB
testcase_50 AC 35 ms
53,460 KB
testcase_51 AC 35 ms
53,460 KB
testcase_52 AC 290 ms
84,872 KB
testcase_53 AC 188 ms
79,124 KB
testcase_54 AC 743 ms
111,584 KB
testcase_55 AC 771 ms
110,816 KB
testcase_56 AC 751 ms
111,344 KB
testcase_57 AC 1,629 ms
257,464 KB
testcase_58 AC 1,666 ms
261,068 KB
testcase_59 AC 1,645 ms
250,184 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

from heapq import heappush, heappop
inf = 10**18
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 != s:
                continue
            ans = min(ans, D[u] + c)
        
print(ans) if ans != inf else print(-1)
0