結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 68 ms
71,328 KB
testcase_02 AC 73 ms
71,204 KB
testcase_03 AC 70 ms
71,208 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

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**9) -> 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**9
for i in range(N):
    costs = dijkstra(N, edges, i)
    cost = 0
    for i in range(N):
        cost += costs[i] * t[i] * 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