https://www.acmicpc.net/problem/21940
์น๊ตฌ๋ค์ด ์ด๊ณ ์๋ ๋์๋ฒํธ ๋๋ฌธ์ ํท๊ฐ๋ ธ๋ ๋ฌธ์
import sys
INF = sys.maxsize
n, m = map(int, sys.stdin.readline().split())
time = [[INF] * n for _ in range(n)]
for _ in range(m):
a, b, t = map(int, sys.stdin.readline().split())
time[a-1][b-1] = t
k = int(input())
city = list(map(int, sys.stdin.readline().split()))
def floyd():
dist = [[INF] * n for _ in range(n)]
for i in range(n):
for j in range(n):
dist[i][j] = time[i][j]
for k in range(n):
for i in range(n):
for j in range(n):
if dist[i][j] > dist[i][k] + dist[k][j]:
dist[i][j] = dist[i][k] + dist[k][j]
return dist
dist = floyd()
travel = []
# ์๋ณต ๊ฑฐ๋ฆฌ ๊ตฌํ๊ธฐ
for i in range(len(city)):
temp = []
for j in range(n):
if (city[i] - 1) == j:
temp.append(0)
elif dist[city[i]-1][j] == INF or dist[j][city[i]-1] == INF:
temp.append(INF)
else:
temp.append(dist[city[i]-1][j] + dist[j][city[i]-1])
travel.append(temp)
answer = []
for j in range(n):
temp = 0
for i in range(len(city)):
if temp < travel[i][j]:
temp = travel[i][j]
answer.append(temp)
ans = min(answer)
for i in range(n):
if answer[i] == ans:
print(i+1, end=' ')
ํ๋ก์ด๋ ์๊ณ ๋ฆฌ์ฆ์ผ๋ก ๋์ N๊ฐ ์ฌ์ด์ ์ต๋จ ์๊ฐ์ ๊ตฌํด์ค ๋ค(NxN ๋ฆฌ์คํธ)
์น๊ตฌ๋ค์ด ์ด๊ณ ์๋ ๋์๋ฅผ ์ถ๋ฐ์ , N๊ฐ์ ๋์๋ฅผ ๋์ฐฉ์ ์ผ๋ก ํ๋ ์๋ณต ์๊ฐ์ ๊ตฌํด์ฃผ์๋ค(KxN ๋ฆฌ์คํธ)
๊ทธ๋ฆฌ๊ณ N๊ฐ์ ๋์๋ณ๋ก ์๋ณต์๊ฐ์ ์ต๋๊ฐ์ ๊ตฌํด์ฃผ๊ณ ๋ต์ ๊ตฌํ๋ค
โ
์ฌ๊ธฐ์ ๋ด๊ฐ ํท๊ฐ๋ ธ๋ ๊ฒ์ ์๋ณต์๊ฐ์ ์ต๋๊ฐ์ ๊ตฌํด์ฃผ๋ ๋ถ๋ถ์ด์๋๋ฐ
์น๊ตฌ๋ค์ด ์ด๊ณ ์๋ ๋์๊ฐ 3, 2, 1์ด๋ผ๊ณ ํ๋ฉด
3๋ฒ ๋์์ ์ด๊ณ ์๋ ์น๊ตฌ์ ์๋ณต์๊ฐ์ travel ๋ฆฌ์คํธ์ 0๋ฒ ์ธ๋ฑ์ค์ ์์!
๋์ ๋ฒํธ๋ฅผ ์ธ๋ฑ์ค ๋ฒํธ๋ก ์ฐฉ๊ฐํด์ ์ธ๋ฑ์ค ์๋ฌ๊ฐ ๋ด๋ค
โ