Skip to content

Commit 123c3d2

Browse files
committed
[BOJ] 11404 플로이드 (G4)
1 parent 4ba7ca7 commit 123c3d2

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

김지호/2주차/260107.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# https://www.acmicpc.net/problem/11404
2+
import sys
3+
from collections import defaultdict
4+
from collections import deque
5+
6+
sys.stdin = open("../input.txt",'r')
7+
8+
Infinity = float('inf')
9+
10+
N = int(input()) # 도시의 개수
11+
M = int(input()) # 버스의 개수
12+
13+
14+
def 플로이드워셜(N,M):
15+
graph = [[Infinity] * (N + 1) for _ in range(N + 1)]
16+
17+
# 초기화: 자기 자신 0
18+
for row in range(0, N + 1):
19+
for col in range(0, N + 1):
20+
if row == col:
21+
graph[row][col] = 0
22+
23+
24+
for _ in range(M):
25+
start, end, value = map(int, input().split(" "))
26+
if value < graph[start][end]:
27+
# 하나의 길에 여러 경로가 있다면 최소 선택
28+
graph[start][end] = value
29+
30+
31+
# 순회
32+
for 경유 in range(1, N + 1):
33+
for 시작 in range(1, N + 1):
34+
for in range(1, N + 1):
35+
graph[시작][] = min(graph[시작][], graph[시작][경유] + graph[경유][])
36+
37+
# 수행된 결과 출력
38+
return graph
39+
40+
answer = 플로이드워셜(N,M)
41+
42+
for row in range(1, N + 1):
43+
for col in range(1, N + 1):
44+
if answer[row][col] == Infinity:
45+
print(0, end=" ")
46+
else:
47+
print(answer[row][col], end=" ")
48+
print() # 줄바꿈

0 commit comments

Comments
 (0)