์‹ค๋ฒ„1 : ์ตœ๋‹จ๊ฑฐ๋ฆฌ ๋ฌธ์ œ์ด๋‹ค.

์ƒ๊ฐ

  1. ๋ชจ๋“  ๊ฒฝ๋กœ์— ๋Œ€ํ•œ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•ด์•ผ ํ•œ๋‹ค.
  2. ๊ฐ€์ค‘์น˜๋Š” ์–‘์ˆ˜์ด์ž ๋™์ผ
  3. ์ •์  ๊ฐœ์ˆ˜ 100๊ฐœ

๋ชจ๋“  ๊ฒฝ๋กœ์— ๋Œ€ํ•ด ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•ด์•ผ ํ•œ๋‹ค๋Š” ์ ์—์„œ ํ”Œ๋ฃจ์ด๋“œ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๊ณ , ์•Œ๊ณ ๋ฆฌ์ฆ˜์—๋„ ํ†ต๊ณผํ•œ ๋…ธ๋“œ ๊ฐœ์ˆ˜์ด๋ฏ€๋กœ ์ง„ํ–‰ํ•œ๋‹ค. (์ œํ•œ์‹œ๊ฐ„ 1์ดˆ)

Code

import sys
 
 
def read_input():
    n = int(sys.stdin.readline().rstrip())
    w = [
        [int(x) if int(x) != 0 else 100000 for x in sys.stdin.readline().split()]
        for y in range(n)
    ]
    d = w
    p = [[int(0) for x in range(n)] for y in range(n)]
    return n, w, d, p
 
 
def allShortestPath(n, w, d, p):
    for k in range(n):
        for i in range(n):
            for j in range(n):
                if d[i][j] > d[i][k - 1] + d[k - 1][j]:
                    p[i][j] = k
                    d[i][j] = d[i][k - 1] + d[k - 1][j]
    for i in range(n):
        for j in range(n):
            if d[i][j] == 100000:
                d[i][j] = 0
            else:
                d[i][j] = 1
    return d, p
 
 
def path(start, end, p):
    return
 
 
def printOutput(mat):
    n = len(mat)
    m = len(mat[0])
 
    for i in range(n):
        for j in range(m):
            print(mat[i][j], end=" ")
        print()
 
n, w, d, p = read_input()
d, p = allShortestPath(n, w, d, p)
printOutput(d)
 
 

Reference