DevYoon

[๋ฐฑ์ค€] 11404. ํ”Œ๋กœ์ด๋“œ (Python) ๋ณธ๋ฌธ

PS/Baekjoon

[๋ฐฑ์ค€] 11404. ํ”Œ๋กœ์ด๋“œ (Python)

gimewn 2023. 6. 10. 23:07

link ๐Ÿ”— https://www.acmicpc.net/problem/11404

๋ฌธ์ œ

n(2 ≤ n ≤ 100)๊ฐœ์˜ ๋„์‹œ๊ฐ€ ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ•œ ๋„์‹œ์—์„œ ์ถœ๋ฐœํ•˜์—ฌ ๋‹ค๋ฅธ ๋„์‹œ์— ๋„์ฐฉํ•˜๋Š” m(1 ≤ m ≤ 100,000)๊ฐœ์˜ ๋ฒ„์Šค๊ฐ€ ์žˆ๋‹ค. ๊ฐ ๋ฒ„์Šค๋Š” ํ•œ ๋ฒˆ ์‚ฌ์šฉํ•  ๋•Œ ํ•„์š”ํ•œ ๋น„์šฉ์ด ์žˆ๋‹ค.

๋ชจ๋“  ๋„์‹œ์˜ ์Œ (A, B)์— ๋Œ€ํ•ด์„œ ๋„์‹œ A์—์„œ B๋กœ ๊ฐ€๋Š”๋ฐ ํ•„์š”ํ•œ ๋น„์šฉ์˜ ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ๋„์‹œ์˜ ๊ฐœ์ˆ˜ n์ด ์ฃผ์–ด์ง€๊ณ  ๋‘˜์งธ ์ค„์—๋Š” ๋ฒ„์Šค์˜ ๊ฐœ์ˆ˜ m์ด ์ฃผ์–ด์ง„๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์…‹์งธ ์ค„๋ถ€ํ„ฐ m+2์ค„๊นŒ์ง€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฒ„์Šค์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋จผ์ € ์ฒ˜์Œ์—๋Š” ๊ทธ ๋ฒ„์Šค์˜ ์ถœ๋ฐœ ๋„์‹œ์˜ ๋ฒˆํ˜ธ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋ฒ„์Šค์˜ ์ •๋ณด๋Š” ๋ฒ„์Šค์˜ ์‹œ์ž‘ ๋„์‹œ a, ๋„์ฐฉ ๋„์‹œ b, ํ•œ ๋ฒˆ ํƒ€๋Š”๋ฐ ํ•„์š”ํ•œ ๋น„์šฉ c๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. ์‹œ์ž‘ ๋„์‹œ์™€ ๋„์ฐฉ ๋„์‹œ๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ์—†๋‹ค. ๋น„์šฉ์€ 100,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค.

์‹œ์ž‘ ๋„์‹œ์™€ ๋„์ฐฉ ๋„์‹œ๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๋…ธ์„ ์€ ํ•˜๋‚˜๊ฐ€ ์•„๋‹ ์ˆ˜ ์žˆ๋‹ค.

์ถœ๋ ฅ

n๊ฐœ์˜ ์ค„์„ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค. i๋ฒˆ์งธ ์ค„์— ์ถœ๋ ฅํ•˜๋Š” j๋ฒˆ์งธ ์ˆซ์ž๋Š” ๋„์‹œ i์—์„œ j๋กœ ๊ฐ€๋Š”๋ฐ ํ•„์š”ํ•œ ์ตœ์†Œ ๋น„์šฉ์ด๋‹ค. ๋งŒ์•ฝ, i์—์„œ j๋กœ ๊ฐˆ ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” ๊ทธ ์ž๋ฆฌ์— 0์„ ์ถœ๋ ฅํ•œ๋‹ค.


์ฝ”๋“œ

1๏ธโƒฃ ๋‹ค์ต์ŠคํŠธ๋ผ(Dijkstra) ํ’€์ด

๋ฉ”๋ชจ๋ฆฌ : 34348 KB / ์‹œ๊ฐ„ : 1116 ms

from sys import stdin
  import heapq

  city_cnt = int(stdin.readline())
  bus_cnt = int(stdin.readline())

  city = [[2e9]*city_cnt for _ in range(city_cnt)]

  bus = {}

  for _ in range(bus_cnt):
      start, destination, cost = map(int, stdin.readline().split())
      start, destination = start-1, destination-1
      if start not in bus:
          bus[start] = {}
      if destination not in bus[start]:
          bus[start][destination] = 2e9
          # ์ตœ์†Œ๊ฐ’ ๊ฐฑ์‹ 
      bus[start][destination] = min(bus[start][destination], cost)

  def dijkstra(bus_num):
      heap = []
      heapq.heappush(heap, (bus_num, 0))
      city[bus_num][bus_num] = 0

      while heap:
          now, now_cost = heapq.heappop(heap)
          if now in bus:
              for next_value in bus[now]:
                  next_cost = bus[now][next_value]
                  if city[bus_num][next_value] > now_cost + next_cost:
                      city[bus_num][next_value] = now_cost + next_cost
                      heapq.heappush(heap, (next_value, city[bus_num][next_value]))

  for idx in range(city_cnt):
      if idx in bus:
          dijkstra(idx)

  for i in range(city_cnt):
      for j in range(city_cnt):
          if city[i][j] == 2e9:
              print(0, end=" ")
          else:
              print(city[i][j], end=" ")
      print()
  • ์ œ๋ชฉ์—์„œ๋ถ€ํ„ฐ ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ ๋ฌธ์ œ๋ผ๊ณ  ์•Œ๋ ค์ฃผ๊ณ  ์žˆ์—ˆ์ง€๋งŒ, ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ์— ๋Œ€ํ•ด ์ž˜ ๋ชจ๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ์•Œ๊ณ  ์žˆ๋Š” ๋‹ค์ต์ŠคํŠธ๋ผ๋กœ ๋จผ์ € ํ’€์–ด๋ณด์•˜๋‹ค.
  • ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ํ™œ์šฉํ•ด A๋„์‹œ์—์„œ B๋„์‹œ๋กœ ๊ฐ€๋Š” ๋น„์šฉ์„ ์ตœ์†Œ๊ฐ’์œผ๋กœ ๊ฐฑ์‹ ํ•ด์ฃผ์—ˆ๊ณ , 0๋ถ€ํ„ฐ N(์ฝ”๋“œ ์ƒ city_cnt)๊นŒ์ง€ ๋Œ๋ฉฐ ์ถœ๋ฐœ ๋„์‹œ ๋ชฉ๋ก(bus)์— ์†ํ•œ ๊ฒฝ์šฐ ๋‹ค์ต์ŠคํŠธ๋ผ๋ฅผ ๋Œ๋ ค์ฃผ์—ˆ๋‹ค.

2๏ธโƒฃ ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ(Floyd-Warshall) ํ’€์ด

๋ฉ”๋ชจ๋ฆฌ : 31256 KB / ์‹œ๊ฐ„ : 404 ms

import sys

  city_cnt = int(sys.stdin.readline())
  bus_cnt = int(sys.stdin.readline())

  city = [[2e9]*city_cnt for _ in range(city_cnt)]

  def make_myself_zero(cnt):
      # ์ž๊ธฐ ์ž์‹ ์œผ๋กœ ๊ฐ€๋Š” ๊ฒฝ์šฐ๋ฅผ 0์œผ๋กœ ๊ฐฑ์‹ 
      for y in range(cnt):
          city[y][y] = 0

  def floyd_warshall(cnt):
      # i๋ฒˆ์งธ ๋„์‹œ๋ฅผ ๊ฒฝ์œ ํ•˜๋Š” ๊ฒฝ์šฐ
      for i in range(cnt):
          for j in range(cnt):
              for k in range(cnt):
                  # j์—์„œ k๋กœ ๋ฐ”๋กœ ๊ฐ€๋Š” ๊ฒƒ VS j -> i -> k ๋กœ i๋ฅผ ๊ฒฝ์œ ํ•˜๋Š” ๊ฒƒ ๋น„๊ต
                  city[j][k] = min(city[j][k], city[j][i] + city[i][k])

  for _ in range(bus_cnt):
      a, b, c = map(int, sys.stdin.readline().split())
      # ์‹œ์ž‘ ๋„์‹œ์™€ ๋„์ฐฉ ๋„์‹œ๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๋…ธ์„  ์ค‘ ์ตœ์†Œ ๋น„์šฉ
      city[a-1][b-1] = min(city[a-1][b-1], c)

  make_myself_zero(city_cnt)

  floyd_warshall(city_cnt)

  for y in range(city_cnt):
      for x in range(city_cnt):
          if city[y][x] < 2e9:
              print(city[y][x], end=" ")
          else:
              print(0, end=" ")
      print()
  • ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ์— ๋Œ€ํ•ด ๊ณต๋ถ€ํ•œ ํ›„ ํ’€์ดํ•ด๋ณด์•˜๋‹ค.
  • A ๋„์‹œ์—์„œ A ๋„์‹œ๋ฅผ ๊ฐˆ ๊ฒฝ์šฐ๋ฅผ 0์œผ๋กœ ๊ฐฑ์‹ ํ•ด์ค€๋‹ค.
  • A ๋„์‹œ์—์„œ B ๋„์‹œ๋ฅผ ๊ฐˆ ๋•Œ, C ๋„์‹œ๋ฅผ ๊ฒฝ์œ ํ•˜์—ฌ ๊ฐ€๋Š” ๊ฒฝ์šฐ๋ฅผ ์‚ผ์ค‘for๋ฌธ์œผ๋กœ ๊ตฌํ˜„ํ•œ๋‹ค.
  • ๋‹ค์ต์ŠคํŠธ๋ผ์™€ ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ์„ ๋น„๊ตํ•˜๋Š” ์žฌ๋ฏธ๊ฐ€ ์žˆ๋Š” ๋ฌธ์ œ์˜€๋‹ค! ํ™•์‹คํžˆ ๋‹ค์ต์ŠคํŠธ๋ผ๋ณด๋‹ค ์ˆ˜ํ–‰ํ•˜๋Š” ์ž‘์—…์ด ์ ์–ด์„œ์ธ์ง€ ์‹œ๊ฐ„์ด ์•ฝ 1/3 ์ •๋„๋กœ ์ค„์—ˆ๋‹ค.