結果

問題 No.788 トラックの移動
ユーザー htkbhtkb
提出日時 2019-02-08 23:07:55
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,743 bytes
コンパイル時間 292 ms
コンパイル使用メモリ 86,724 KB
実行使用メモリ 90,332 KB
最終ジャッジ日時 2023-09-14 03:56:02
合計ジャッジ時間 12,075 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,806 ms
90,332 KB
testcase_01 AC 74 ms
71,584 KB
testcase_02 AC 76 ms
71,252 KB
testcase_03 AC 76 ms
71,604 KB
testcase_04 AC 536 ms
79,464 KB
testcase_05 AC 1,796 ms
89,176 KB
testcase_06 AC 1,813 ms
89,752 KB
testcase_07 AC 76 ms
71,316 KB
testcase_08 AC 81 ms
71,316 KB
testcase_09 AC 81 ms
71,548 KB
testcase_10 AC 82 ms
71,556 KB
testcase_11 AC 81 ms
71,504 KB
testcase_12 AC 81 ms
71,540 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 417 ms
79,148 KB
testcase_16 AC 1,765 ms
87,552 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from itertools import starmap
from operator import mul

N, M, L = map(int, input().split())
L -= 1
t = list(map(int, input().split()))
target = set([i for i, n in enumerate(t) if n])
edges = [[] for _ in [0]*N]

for a, b, c in (map(int, l.split()) for l in sys.stdin):
    edges[a-1].append((b-1, c))
    edges[b-1].append((a-1, c))


def dijkstra(v_count: int, edges: list, start: int,
             *, adj_matrix: bool = False, unreachable=10**18) -> list:
    """ ダイクストラ
    :param v_count: 頂点数
    :param edges: 辺のリスト(隣接リストor隣接行列)
    :param start: スタートする頂点
    :param adj_matrix: edgesに渡したリストが隣接行列ならTrue
    :param unreachable: 到達不能を表すコスト値
                        隣接行列の辺の有無の判定および返すリストの初期値に使用
    """
    from heapq import heappush, heappop

    vertices = [unreachable] * v_count
    vertices[start] = 0
    q, rem = [(0, start)], v_count - 1

    while q and rem:
        cost, v = heappop(q)
        if vertices[v] < cost:
            continue
        rem -= 1

        for dest, _cost in edges[v]:
            newcost = cost + _cost
            if vertices[dest] > newcost:
                vertices[dest] = newcost
                heappush(q, (newcost, dest))

    return vertices


if t[L]:
    flag = True
else:
    flag = False
    L_to_i = dijkstra(N, edges, L)

ans = 10**18
for i in range(N):
    costs = dijkstra(N, edges, i)
    cost = sum(starmap(mul, zip(costs, t)))*2
    if flag:
        cost -= costs[L]
    else:
        cost += min(L_to_i[i], min(L_to_i[j]-costs[j] for j in range(N) if t[j]))

    if ans > cost:
        ans = cost

print(ans)
0