BOJ - 6185 - Clear And Present Danger

Updated:

import sys
_INF = 1e9
def solution():
    N, M = map(int, sys.stdin.readline().split())
    dist = []
    maps = []
    for _ in range(M):
        dist.append(int(sys.stdin.readline()) - 1)
    for _ in range(N):
        maps.append(list(map(int, sys.stdin.readline().split())))

    for k in range(N):
        for i in range(N):
            for j in range(N):
                if i == j: continue
                maps[i][j] = min(maps[i][j], maps[i][k] + maps[k][j])

    ret = 0
    cur = dist[0]
    for i in range(1, M):
        ret += maps[cur][dist[i]]
        cur = dist[i]

    print(ret)

solution()

https://www.acmicpc.net/problem/6185

Categories:

Updated:

Leave a comment