[TIL #23] 23์ผ์ฐจ ์˜ค๋Š˜์˜ ๊ณต๋ถ€ (2023.03.25 ํ† )

2023. 3. 26. 14:39ใ†Today I Learned

TIL 2023.03.25 ๐Ÿฃ

์ˆ˜ ~ ๊ธˆ TIL์„ ์ž‘์„ฑํ•˜์ง€ ์•Š์•˜๋Š”๋ฐ,,,, ๊ณต๋ถ€๋ฅผ ์•ˆํ•œ๊ฑด ์•„๋‹ˆ๊ณ  ์ž‘์„ฑํ•˜๋Š”๊ฑธ ์žŠ์—ˆ์Šต๋‹ˆ๋‹ค.... ์‹ค๋ง์Šค๋Ÿฝ๋„ค์š” ์ €์ž์‹ ์—๊ฒŒ...

์˜ค๋Š˜ ๋ฐฐ์šด ๊ฒƒ ๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป

์•Œ๊ณ ๋ฆฌ์ฆ˜

  • ๋ฐฑ์ค€ 1389 : ์ผ€๋นˆ๋ฒ ์ด์ปจ ์ˆ˜๊ฐ€ ์ตœ์†Œ์ธ ์‚ฌ๋žŒ ๊ตฌํ•˜๊ธฐ
    • ๋ชจ๋“  ์‚ฌ๋žŒ๊ณผ ์ผ€๋นˆ ๋ฒ ์ด์ปจ ๊ฒŒ์ž„์„ ํ–ˆ์„ ๋•Œ, ๋‚˜์˜ค๋Š” ๋‹จ๊ณ„์˜ ํ•ฉ
    • ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜๋ฉด ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์ด๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์˜ค๋žœ๋งŒ์— ๊ตฌํ˜„ํ–ˆ๋”๋‹ˆ ์‚ฌ์‹ค ๊ธฐ์–ต์ด ์ž˜ ์•ˆ๋‚˜์„œ ์• ๋ฅผ ๋จน์—ˆ๋‹ค.
      # ์ผ€๋นˆ๋ฒ ์ด์ปจ : ์ผ€๋นˆ ๋ฒ ์ด์ปจ์˜ ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ์‚ฌ๋žŒ์„ ์ถœ๋ ฅ   
      # ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ•˜๊ธฐ
      import sys
      n, m = map(int, sys.stdin.readline().rstrip().split())
      graph = [[0] * n for _ in range(n)]
      
      for _ in range(m):
          a, b = map(int, sys.stdin.readline().rstrip().split())
          graph[a - 1][b - 1] = 1
          graph[b - 1][a - 1] = 1
      
      for i in range(n):
          for j in range(n): 
              for k in range(n):
                  if j == k:
                      continue            
      
                  if graph[j][i] and graph[i][k]: # j์—์„œ i๋ฅผ ๊ฑฐ์ณ k๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋‹ค๋ฉด
                      if graph[j][k] == 0:    # j - k ์—ฐ๊ฒฐ๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ
                          graph[j][k] = graph[j][i] + graph[i][k]
                      else:
                          graph[j][k] = min(graph[j][k], graph[j][i] + graph[i][k])
      result_idx = 0
      min_count = 10e9
      for i in range(n):
          count = 0
          for j in range(n):
              count += graph[i][j]   # i์˜ ์ผ€๋นˆ ๋ฒ ์ด์ปจ ์ˆ˜ ๊ตฌํ•˜๊ธฐ
          if min_count > count:
              min_count = count
              result_idx = i
      
      print(result_idx + 1)

React

  • ๋ฆฌ์•กํŠธ ๊ฐ•์˜ ์„น์…˜ 2 ์ •๋ฆฌ ๋‚ด์šฉ์„ ํฌ์ŠคํŒ… ํ–ˆ๋‹ค. ๋‚ด์šฉ์ด ๊ธธ์–ด์„œ ๋‘ ๊ฐœ์˜ ํฌ์ŠคํŒ…์œผ๋กœ ๋‚˜๋ˆ  ์˜ฌ๋ฆด ์˜ˆ์ •์ด๋‹ค.

๊ธฐํƒ€ ๐Ÿค”

์ถ”๊ฐ€์ ์œผ๋กœ ํ•˜๋ฉด ์ข‹์„ ๊ฒƒ