結果

問題 No.1320 Two Type Min Cost Cycle
ユーザー chineristACchineristAC
提出日時 2020-12-17 00:32:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,554 ms / 2,000 ms
コード長 1,432 bytes
コンパイル時間 246 ms
コンパイル使用メモリ 81,852 KB
実行使用メモリ 199,044 KB
最終ジャッジ日時 2023-10-20 10:05:29
合計ジャッジ時間 23,867 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,484 KB
testcase_01 AC 41 ms
53,484 KB
testcase_02 AC 41 ms
53,484 KB
testcase_03 AC 37 ms
53,480 KB
testcase_04 AC 37 ms
53,480 KB
testcase_05 AC 37 ms
53,480 KB
testcase_06 AC 36 ms
53,480 KB
testcase_07 AC 130 ms
78,092 KB
testcase_08 AC 182 ms
80,432 KB
testcase_09 AC 1,554 ms
170,124 KB
testcase_10 AC 186 ms
80,124 KB
testcase_11 AC 1,219 ms
140,180 KB
testcase_12 AC 403 ms
92,096 KB
testcase_13 AC 672 ms
107,640 KB
testcase_14 AC 89 ms
76,084 KB
testcase_15 AC 199 ms
81,052 KB
testcase_16 AC 295 ms
80,392 KB
testcase_17 AC 108 ms
77,244 KB
testcase_18 AC 104 ms
76,812 KB
testcase_19 AC 749 ms
99,920 KB
testcase_20 AC 87 ms
76,212 KB
testcase_21 AC 1,406 ms
129,264 KB
testcase_22 AC 40 ms
53,480 KB
testcase_23 AC 38 ms
53,484 KB
testcase_24 AC 37 ms
53,480 KB
testcase_25 AC 39 ms
53,484 KB
testcase_26 AC 37 ms
53,480 KB
testcase_27 AC 38 ms
53,484 KB
testcase_28 AC 104 ms
76,852 KB
testcase_29 AC 351 ms
92,076 KB
testcase_30 AC 60 ms
66,480 KB
testcase_31 AC 127 ms
77,056 KB
testcase_32 AC 37 ms
53,484 KB
testcase_33 AC 655 ms
119,552 KB
testcase_34 AC 369 ms
89,744 KB
testcase_35 AC 195 ms
77,456 KB
testcase_36 AC 160 ms
77,096 KB
testcase_37 AC 131 ms
76,856 KB
testcase_38 AC 234 ms
79,420 KB
testcase_39 AC 185 ms
77,508 KB
testcase_40 AC 39 ms
53,484 KB
testcase_41 AC 39 ms
53,484 KB
testcase_42 AC 36 ms
53,480 KB
testcase_43 AC 344 ms
91,288 KB
testcase_44 AC 239 ms
82,124 KB
testcase_45 AC 1,482 ms
199,044 KB
testcase_46 AC 126 ms
77,912 KB
testcase_47 AC 386 ms
97,832 KB
testcase_48 AC 779 ms
120,308 KB
testcase_49 AC 153 ms
77,332 KB
testcase_50 AC 39 ms
53,484 KB
testcase_51 AC 37 ms
53,484 KB
testcase_52 AC 298 ms
82,164 KB
testcase_53 AC 198 ms
78,560 KB
testcase_54 AC 982 ms
120,444 KB
testcase_55 AC 1,001 ms
119,424 KB
testcase_56 AC 995 ms
118,940 KB
testcase_57 AC 1,453 ms
180,732 KB
testcase_58 AC 1,478 ms
179,728 KB
testcase_59 AC 1,472 ms
181,700 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Dijkstra():
    class Edge():
        def __init__(self, _to, _cost):
            self.to = _to
            self.cost = _cost

    def __init__(self, V):
        self.G = [[] for i in range(V)]
        self._E = 0
        self._V = V

    @property
    def E(self):
        return self._E

    @property
    def V(self):
        return self._V

    def add(self, _from, _to, _cost):
        self.G[_from].append(self.Edge(_to, _cost))
        self._E += 1

    def shortest_path(self, s):
        import heapq
        que = []
        d = [10**15] * self.V
        d[s] = 0
        heapq.heappush(que, (0, s))

        while len(que) != 0:
            cost, v = heapq.heappop(que)
            if d[v] < cost: continue

            for i in range(len(self.G[v])):
                e = self.G[v][i]
                if d[e.to] > d[v] + e.cost:
                    d[e.to] = d[v] + e.cost
                    heapq.heappush(que, (d[e.to], e.to))
        return d

T = int(input())
N,M = map(int,input().split())
edge = [tuple(map(int,input().split())) for i in range(M)]
res = 10**15

for i in range(M):
    tmp = Dijkstra(N)
    for j in range(M):
        u,v,w = edge[j]
        if i!=j:
            tmp.add(u-1,v-1,w)
            if not T:
                tmp.add(v-1,u-1,w)
    u,v,w = edge[i]
    d = tmp.shortest_path(v-1)[u-1]
    if d!=10**15:
        res = min(res,d+w)

if res!=10**15:
    print(res)
else:
    print(-1)
0