๋‚ด ์ธ์ƒ์—์„œ ๋ฏฟ์„ ๊ฑด ์˜ค์ง ๋‚˜ ์ž์‹ ๋ฟ!

The only one you can truly trust is yourself.

๊ฒŒ์ž„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ/Python ํ”„๋กœ๊ทธ๋ž˜๋ฐ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ฐฐ๋‹ฌ

๐ŸŽฎinspirer9 2023. 2. 18. 15:52
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ์„ค๋ช…

N๊ฐœ์˜ ๋งˆ์„๋กœ ์ด๋ฃจ์–ด์ง„ ๋‚˜๋ผ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋‚˜๋ผ์˜ ๊ฐ ๋งˆ์„์—๋Š” 1๋ถ€ํ„ฐ N๊นŒ์ง€์˜ ๋ฒˆํ˜ธ๊ฐ€ ๊ฐ๊ฐ ํ•˜๋‚˜์”ฉ ๋ถ€์—ฌ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋งˆ์„์€ ์–‘๋ฐฉํ–ฅ์œผ๋กœ ํ†ตํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๋„๋กœ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š”๋ฐ, ์„œ๋กœ ๋‹ค๋ฅธ ๋งˆ์„ ๊ฐ„์— ์ด๋™ํ•  ๋•Œ๋Š” ์ด ๋„๋กœ๋ฅผ ์ง€๋‚˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋„๋กœ๋ฅผ ์ง€๋‚  ๋•Œ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์€ ๋„๋กœ๋ณ„๋กœ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ํ˜„์žฌ 1๋ฒˆ ๋งˆ์„์— ์žˆ๋Š” ์Œ์‹์ ์—์„œ ๊ฐ ๋งˆ์„๋กœ ์Œ์‹ ๋ฐฐ๋‹ฌ์„ ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ๋งˆ์„๋กœ๋ถ€ํ„ฐ ์Œ์‹ ์ฃผ๋ฌธ์„ ๋ฐ›์œผ๋ ค๊ณ  ํ•˜๋Š”๋ฐ, N๊ฐœ์˜ ๋งˆ์„ ์ค‘์—์„œ K ์‹œ๊ฐ„ ์ดํ•˜๋กœ ๋ฐฐ๋‹ฌ์ด ๊ฐ€๋Šฅํ•œ ๋งˆ์„์—์„œ๋งŒ ์ฃผ๋ฌธ์„ ๋ฐ›์œผ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ์€ N = 5, K = 3์ธ ๊ฒฝ์šฐ์˜ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค.

์œ„ ๊ทธ๋ฆผ์—์„œ 1๋ฒˆ ๋งˆ์„์— ์žˆ๋Š” ์Œ์‹์ ์€ [1, 2, 4, 5] ๋ฒˆ ๋งˆ์„๊นŒ์ง€๋Š” 3 ์ดํ•˜์˜ ์‹œ๊ฐ„์— ๋ฐฐ๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ 3๋ฒˆ ๋งˆ์„๊นŒ์ง€๋Š” 3์‹œ๊ฐ„ ์ด๋‚ด๋กœ ๋ฐฐ๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ๋กœ๊ฐ€ ์—†์œผ๋ฏ€๋กœ 3๋ฒˆ ๋งˆ์„์—์„œ๋Š” ์ฃผ๋ฌธ์„ ๋ฐ›์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 1๋ฒˆ ๋งˆ์„์— ์žˆ๋Š” ์Œ์‹์ ์ด ๋ฐฐ๋‹ฌ ์ฃผ๋ฌธ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ๋งˆ์„์€ 4๊ฐœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.
๋งˆ์„์˜ ๊ฐœ์ˆ˜ N, ๊ฐ ๋งˆ์„์„ ์—ฐ๊ฒฐํ•˜๋Š” ๋„๋กœ์˜ ์ •๋ณด road, ์Œ์‹ ๋ฐฐ๋‹ฌ์ด ๊ฐ€๋Šฅํ•œ ์‹œ๊ฐ„ K๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์Œ์‹ ์ฃผ๋ฌธ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ๋งˆ์„์˜ ๊ฐœ์ˆ˜๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • ๋งˆ์„์˜ ๊ฐœ์ˆ˜ N์€ 1 ์ด์ƒ 50 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • road์˜ ๊ธธ์ด(๋„๋กœ ์ •๋ณด์˜ ๊ฐœ์ˆ˜)๋Š” 1 ์ด์ƒ 2,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • road์˜ ๊ฐ ์›์†Œ๋Š” ๋งˆ์„์„ ์—ฐ๊ฒฐํ•˜๊ณ  ์žˆ๋Š” ๊ฐ ๋„๋กœ์˜ ์ •๋ณด๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
  • road๋Š” ๊ธธ์ด๊ฐ€ 3์ธ ๋ฐฐ์—ด์ด๋ฉฐ, ์ˆœ์„œ๋Œ€๋กœ (a, b, c)๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
    • a, b(1 ≤ a, b ≤ N, a != b)๋Š” ๋„๋กœ๊ฐ€ ์—ฐ๊ฒฐํ•˜๋Š” ๋‘ ๋งˆ์„์˜ ๋ฒˆํ˜ธ์ด๋ฉฐ, c(1 ≤ c ≤ 10,000, c๋Š” ์ž์—ฐ์ˆ˜)๋Š” ๋„๋กœ๋ฅผ ์ง€๋‚˜๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์ž…๋‹ˆ๋‹ค.
    • ๋‘ ๋งˆ์„ a, b๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๋„๋กœ๋Š” ์—ฌ๋Ÿฌ ๊ฐœ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • ํ•œ ๋„๋กœ์˜ ์ •๋ณด๊ฐ€ ์—ฌ๋Ÿฌ ๋ฒˆ ์ค‘๋ณตํ•ด์„œ ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • K๋Š” ์Œ์‹ ๋ฐฐ๋‹ฌ์ด ๊ฐ€๋Šฅํ•œ ์‹œ๊ฐ„์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ, 1 ์ด์ƒ 500,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • ์ž„์˜์˜ ๋‘ ๋งˆ์„๊ฐ„์— ํ•ญ์ƒ ์ด๋™ ๊ฐ€๋Šฅํ•œ ๊ฒฝ๋กœ๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.
  • 1๋ฒˆ ๋งˆ์„์— ์žˆ๋Š” ์Œ์‹์ ์ด K ์ดํ•˜์˜ ์‹œ๊ฐ„์— ๋ฐฐ๋‹ฌ์ด ๊ฐ€๋Šฅํ•œ ๋งˆ์„์˜ ๊ฐœ์ˆ˜๋ฅผ return ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ์˜ˆ

N road K result
5 [[1,2,1],[2,3,3],[5,2,2],[1,4,2],[5,3,1],[5,4,2]] 3 4
6 [[1,2,1],[1,3,2],[2,3,2],[3,4,3],[3,5,2],[3,5,3],[5,6,1]] 4 4

์ž…์ถœ๋ ฅ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ #1
๋ฌธ์ œ์˜ ์˜ˆ์‹œ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2
์ฃผ์–ด์ง„ ๋งˆ์„๊ณผ ๋„๋กœ์˜ ๋ชจ์–‘์€ ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

1๋ฒˆ ๋งˆ์„์—์„œ ๋ฐฐ๋‹ฌ์— 4์‹œ๊ฐ„ ์ดํ•˜๊ฐ€ ๊ฑธ๋ฆฌ๋Š” ๋งˆ์„์€ [1, 2, 3, 5] 4๊ฐœ์ด๋ฏ€๋กœ 4๋ฅผ return ํ•ฉ๋‹ˆ๋‹ค.

ํ’€์ด

  • ์ฒ˜์Œ์—” ๊ทธ๋ƒฅ ๋ธŒ๋ฃจํŠธ ํฌ์Šค(Brute Force)๋กœ ๋Œ€์ถฉ ๋”ฐ๋ผ๊ฐ€๋ณด๋‹ค๊ฐ€ ์ ๋‹นํžˆ ์•„๋ฌด๊ฐ’์ด๋‚˜ ๋ฆฌํ„ดํ•˜๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ... ์•ˆ๋จใ…‹
  • ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋„ ๊ณ„์† ์‹คํŒจ.
  • ์ฑ„์  ํ•ด๋ณด๋‹ˆ ํšจ์œจ์„ฑ ํ…Œ์ŠคํŠธ๋Š” ์—†๊ณ  ์ •ํ™•์„ฑ ํ…Œ์ŠคํŠธ๋งŒ 30๊ฐœ ์žˆ๋‹ค. ์ •ํ™•ํ•œ ๋‹ต์„ ์ฐพ๋Š”๊ฒŒ ์ค‘์š”ํ•˜๋‹ค.
  • ๊ฒฐ๊ตญ ๋งˆ์„ NxN 2์ฐจ์› ๋ฐฐ์—ด์— ์Œ์‹ ๋ฐฐ๋‹ฌ ์ตœ๋Œ€๊ฐ’์„ ๋„ฃ์—ˆ๋‹ค.
def solution(N, road, K):
    answer = 0
    town = [[500001] * N for i in range(N)]
    
    for _t in town:
        print(_t)
    return answer

ํ…Œ์ŠคํŠธ 1
์ถœ๋ ฅ ใ€‰	
[500001, 500001, 500001, 500001, 500001]
[500001, 500001, 500001, 500001, 500001]
[500001, 500001, 500001, 500001, 500001]
[500001, 500001, 500001, 500001, 500001]
[500001, 500001, 500001, 500001, 500001]
  • ์ด์ œ road๋ฅผ ์ˆœํšŒํ•˜๊ณ  O(N) 2์ฐจ์› ๋ฐฐ์—ด์— ๊ฐ’์„ ์ฑ„์›Œ๋„ฃ๋Š”๋‹ค.
  • ๊ธฐ์กด์— ๋“ค์–ด์žˆ๋˜ ๊ฐ’๋ณด๋‹ค ํฐ ๊ฐ’์ผ ๊ฒฝ์šฐ์—๋งŒ ๋ฎ์–ด์”Œ์šด๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰์œผ๋กœ 1๋ฒˆ ๋งˆ์„์—์„œ 1๋ฒˆ ๋งˆ์„๋กœ ์ฃผ๋ฌธํ•˜๋ฉด ๋‹น์—ฐํžˆ 0์‹œ๊ฐ„์ด๋ฏ€๋กœ ๋งˆ์ง€๋ง‰ ๋‹ต+1 ๋ฆฌํ„ด์œผ๋กœ ์ˆ˜์ •ํ•œ๋‹ค.
def solution(N, road, K):
    answer = 0
    town = [[500001] * (N+1) for i in range(N+1)]
    
    for (s,e,t) in road:
        town[s][e] = t
        town[e][s] = t
    
    for _t in town:
        print(_t)
    
    return answer + 1
    
ํ…Œ์ŠคํŠธ 1
์ถœ๋ ฅ ใ€‰	
[500001, 500001, 500001, 500001, 500001, 500001]
[500001, 500001, 1, 500001, 2, 500001]
[500001, 1, 500001, 3, 500001, 2]
[500001, 500001, 3, 500001, 500001, 1]
[500001, 2, 500001, 500001, 500001, 2]
[500001, 500001, 2, 1, 2, 500001]
  • ์—ฌ๊ธฐ๊นŒ์ง€๋Š” ๋Œ€์ถฉ ๋งŒ๋“ค์—ˆ๋Š”๋ฐ ์ด์ œ ์–ด๋–ป๊ฒŒ ํ•˜๋Š๋ƒ?
  • ๊ทธ๋ƒฅ ๋…ธ๊ฐ€๋‹ค๋กœ ๋”ํ•˜๋ฉด์„œ K ๋‚ฎ์€ ๊ฐ’๋“ค ๊ณ„์† ์ €์žฅํ•˜๋Š” ์ˆ˜ ๋ฐ–์— ์—†๋‚˜?ใ…‹ใ…‹ใ…‹
์ถœ๋ฐœ \ ๋„์ฐฉ 0 1 2 3 4 5
0 500001 500001 500001 500001 500001 500001
1 500001 500001 1 500001 2 500001
2 500001 1 500001 3 500001 2
3 500001 500001 3 500001 500001 1
4 500001 2 500001 500001 500001 2
5 500001 500001 2 1 2 500001
  • ๋…ธ๊ฐ€๋‹ค ๋ฐฉ์‹
    • 1๋ฒˆ ๋งˆ์„์—์„œ K(=3) ์‹œ๊ฐ„์•ˆ์— ๋„์ฐฉํ•  ์ˆ˜ ์žˆ๋Š” ๋งˆ์„์€...
      • 2๋ฒˆ(1์‹œ๊ฐ„), 4๋ฒˆ(2์‹œ๊ฐ„)
        • 2๋ฒˆ ๋งˆ์„์—์„œ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๋งˆ์„์€...
          • 1๋ฒˆ(1์‹œ๊ฐ„), 3๋ฒˆ(3์‹œ๊ฐ„), 5๋ฒˆ(2์‹œ๊ฐ„)
            • 1๋ฒˆ : ...
            • 3๋ฒˆ ๋งˆ์„์—์„œ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๋งˆ์„์€... 2(3), 5(2)
            • 5๋ฒˆ ๋งˆ์„์—์„œ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๋งˆ์„์€... 2(2), 3(1), 4(2)
        • 4๋ฒˆ ๋งˆ์„์—์„œ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๋งˆ์„์€...
          • 1๋ฒˆ(2์‹œ๊ฐ„), 5๋ฒˆ(2์‹œ๊ฐ„)
            • 1๋ฒˆ ...
            • 5๋ฒˆ ...
  • ์ด๊ฑฐ๋„ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค๋ฉด ๋˜๊ฒ ๋„ค?
  • ์ตœ๋‹จ ๋„์ฐฉ ์‹œ๊ฐ„ ๋ฐฐ์—ด.
    • ๊ธฐ์กด ๊ฐ’๋ณด๋‹ค ์ž‘์œผ๋ฉด ๋ฎ์–ด์”Œ์šฐ๊ณ  ๊ณ„์†ํ•˜๋ฉด ๋˜๊ณ ,
    • ๊ธฐ์กด ๊ฐ’๋ณด๋‹ค ํฌ๋ฉด ๋ฉˆ์ถ”๋ฉด ๋จ
๋งˆ์„ 0 1 2 3 4 5
์ดˆ๊ธฐ๊ฐ’ 500001 500001 500001 500001 500001 500001
1ํŠธ 500001 500001 1 500001 2 500001
2ํŠธ 500001 1+1   1+3    
  • ๊ทธ๋Ÿฌ๋ฉด n=n์ผ ๊ฒฝ์šฐ์—๋Š” 0์„ ๋„ฃ์–ด๋‘ฌ์•ผํ•˜๊ฒ ๊ตฌ๋‚˜?
  • ๊ทธ๋ž˜์•ผ ์ด๋™์„ ์•ˆํ•˜๋„ค...
def solution(N, road, K):
    answer = 0
    town = [[500001] * (N+1) for i in range(N+1)]
    
    for (s,e,t) in road:
        town[s][e] = t
        town[e][s] = t

    for i in range(N+1):
        town[i][i] = 0
        
    for _t in town:
        print(_t)
    
    return answer + 1
    
ํ…Œ์ŠคํŠธ 1
์ถœ๋ ฅ ใ€‰	
[0, 500001, 500001, 500001, 500001, 500001]
[500001, 0, 1, 500001, 2, 500001]
[500001, 1, 0, 3, 500001, 2]
[500001, 500001, 3, 0, 500001, 1]
[500001, 2, 500001, 500001, 0, 2]
[500001, 500001, 2, 1, 2, 0]
  • ์ตœ๋‹จ ๋„์ฐฉ ์‹œ๊ฐ„ ๋ฐฐ์—ด.
  • 0ํŠธ : ์‹œ์ž‘ ๋งˆ์„์ธ 1๋ฒˆ ๋งˆ์„์€ 0.
  • 1ํŠธ : 2๋ฒˆ ๋งˆ์„๊ณผ 4๋ฒˆ ๋งˆ์„ ๋„์ฐฉ, ์ถœ๋ฐœ ๋งˆ์„์˜ ์‹œ๊ฐ„์„ ํ•ฉ์‚ฐ
    • K ๋ณด๋‹ค ์ž‘์œผ๋ฉด ๊ณ„์†, ๊ธฐ์กด ๊ฐ’์ด ๋” ์ž‘์œผ๋ฉด ๊ณ„์†, ๊ทธ ์™ธ ๋ฉˆ์ถค (์ด๊ฑด ๊ณ„์†)
๋งˆ์„ 0 1 2 3 4 5
์ดˆ๊ธฐ๊ฐ’ 500001 500001 500001 500001 500001 500001
0ํŠธ (์ถœ๋ฐœ) 500001 0 500001 500001 500001 500001
1ํŠธ 500001 0 0+1 500001 0+2 500001
  • 2ํŠธ : 2๋ฒˆ ๋งˆ์„๊ณผ 4๋ฒˆ ๋งˆ์„์—์„œ ์ถœ๋ฐœ
    • ๋„์ฐฉํ•  ์ˆ˜ ์žˆ๋Š” ๋งˆ์„์€ 2 -> 1,3,5 / 4 -> 1 ,5
๋งˆ์„ 0 1 2 3 4 5
์ดˆ๊ธฐ๊ฐ’ 500001 500001 500001 500001 500001 500001
0ํŠธ (์ถœ๋ฐœ) 500001 0 500001 500001 500001 500001
1ํŠธ 500001 0

0+1
(K๋ณด๋‹ค ์ž‘์•„์„œ ์„ฑ๊ณต)
500001

0+2
(K๋ณด๋‹ค ์ž‘์•„์„œ ์„ฑ๊ณต)
500001

2ํŠธ 500001 0



0 + 1 (์‹คํŒจ)
0 + 2 (์‹คํŒจ)
1





500001



0 + 1 + 3
(K๋ณด๋‹ค ์ปค์„œ ์‹คํŒจ)
2




0+2
3

0+1+2
(K๋ณด๋‹ค ์ž‘์•„์„œ ์„ฑ๊ณต)
0+2+2
(K๋ณด๋‹ค ์ปค์„œ ์‹คํŒจ)
  • Q์— ๋”์ด์ƒ ๋ฐฐ๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฐ๋‹ฌ์›์ด ์—†์œผ๋‹ˆ๊นŒ,
    • ์ตœ๋‹จ ๋„์ฐฉ ์‹œ๊ฐ„ ๋ฐฐ์—ด์—์„œ K๋ณด๋‹ค ์ž‘์€ ๊ฐ’์„ ์ฐพ์•„์„œ ์นด์šดํŒ…ํ•ด์„œ ๋ฆฌํ„ด
    • ๋ฐ˜๋Œ€๋กœ 500001์˜ ๊ฐฏ์ˆ˜๋ฅผ N์—์„œ ๋นผ์„œ ์ฃผ๋ฉด? N=5 - 2๊ฐœ = 3
  • ์•„~ ๊ทผ๋ฐ 500001 ๋„ˆ๋ฌด ๋ฐฉํ•ด๋˜๋„ค?
    • ์ผ๋‹จ ํ…Œ์ŠคํŠธ ์ผ€์ด์ŠคํŠธ๋Š” 9๋กœ ํ’€์ž!
    • ๋‚˜์ค‘์— ๋˜๋Œ๋ฆฌ๋Š” ๊ฑฐ ๊นŒ๋จน์ง€ ๋ง๊ธฐ
def solution(N, road, K):
    answer = 0
    town = [[500001] * (N+1) for i in range(N+1)]
    shortest_road = town[0] 
    delivery_man = [False] * (N+1)
    
    for (s,e,t) in road:
        if town[s][e] > t:
            town[s][e] = town[e][s] = t
    for i in range(N+1): town[i][i] = 0
    
    shortest_road[1] = 0 # ์‹œ์ž‘ ๋งˆ์„ ์ถœ๋ฐœ ์‹œ๊ฐ„ ์„ธํŒ…!
    delivery_man[1] = True # ๋ฐฐ๋‹ฌ์› ์ถœ๋ฐœ!
    
    for _t in town: print(_t)
    print("shortest_road:", shortest_road)
    print("delivery_man :", delivery_man)
    
    while sum(delivery_man) > 0: # ๋ฐฐ๋‹ฌ์›์ด ์žˆ๋Š” ๋™์•ˆ ๋ฃจํ”„
        for rider in range(1, N+1):
            if delivery_man[rider] == True:
                print(rider,"๋ฒˆ ๋ฐฐ๋‹ฌ์›")
                print("shortest_road:", shortest_road)
                for i in range(1, N+1):
                    _time = town[rider][i]
                    _new_time = shortest_road[rider] + _time
                    if _new_time <= K and _new_time < shortest_road[i]:
                        shortest_road[i] = _new_time
                        delivery_man[i] = True
                delivery_man[rider] = False
                print("shortest_road:", shortest_road)
    for i in range(1, N+1):
        if shortest_road[i] < 500001:
            answer += 1
    return answer
  • ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ์ž˜ ํ’€๋ฆฐ๋‹ค. ๊ณผ์—ฐ?
ํ…Œ์ŠคํŠธ 1
์ž…๋ ฅ๊ฐ’ ใ€‰	5, [[1, 2, 1], [2, 3, 3], [5, 2, 2], [1, 4, 2], [5, 3, 1], [5, 4, 2]], 3
๊ธฐ๋Œ“๊ฐ’ ใ€‰	4
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.
์ถœ๋ ฅ ใ€‰	
[0, 0, 500001, 500001, 500001, 500001]
[500001, 0, 1, 500001, 2, 500001]
[500001, 1, 0, 3, 500001, 2]
[500001, 500001, 3, 0, 500001, 1]
[500001, 2, 500001, 500001, 0, 2]
[500001, 500001, 2, 1, 2, 0]
shortest_road: [0, 0, 500001, 500001, 500001, 500001]
delivery_man : [False, True, False, False, False, False]
1 ๋ฒˆ ๋ฐฐ๋‹ฌ์›
shortest_road: [0, 0, 500001, 500001, 500001, 500001]
shortest_road: [0, 0, 1, 500001, 2, 500001]
2 ๋ฒˆ ๋ฐฐ๋‹ฌ์›
shortest_road: [0, 0, 1, 500001, 2, 500001]
shortest_road: [0, 0, 1, 500001, 2, 3]
4 ๋ฒˆ ๋ฐฐ๋‹ฌ์›
shortest_road: [0, 0, 1, 500001, 2, 3]
shortest_road: [0, 0, 1, 500001, 2, 3]
5 ๋ฒˆ ๋ฐฐ๋‹ฌ์›
shortest_road: [0, 0, 1, 500001, 2, 3]
shortest_road: [0, 0, 1, 500001, 2, 3]

ํ…Œ์ŠคํŠธ 2
์ž…๋ ฅ๊ฐ’ ใ€‰	6, [[1, 2, 1], [1, 3, 2], [2, 3, 2], [3, 4, 3], [3, 5, 2], [3, 5, 3], [5, 6, 1]], 4
๊ธฐ๋Œ“๊ฐ’ ใ€‰	4
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.
์ถœ๋ ฅ ใ€‰	
[0, 0, 500001, 500001, 500001, 500001, 500001]
[500001, 0, 1, 2, 500001, 500001, 500001]
[500001, 1, 0, 2, 500001, 500001, 500001]
[500001, 2, 2, 0, 3, 2, 500001]
[500001, 500001, 500001, 3, 0, 500001, 500001]
[500001, 500001, 500001, 2, 500001, 0, 1]
[500001, 500001, 500001, 500001, 500001, 1, 0]
shortest_road: [0, 0, 500001, 500001, 500001, 500001, 500001]
delivery_man : [False, True, False, False, False, False, False]
1 ๋ฒˆ ๋ฐฐ๋‹ฌ์›
shortest_road: [0, 0, 500001, 500001, 500001, 500001, 500001]
shortest_road: [0, 0, 1, 2, 500001, 500001, 500001]
2 ๋ฒˆ ๋ฐฐ๋‹ฌ์›
shortest_road: [0, 0, 1, 2, 500001, 500001, 500001]
shortest_road: [0, 0, 1, 2, 500001, 500001, 500001]
3 ๋ฒˆ ๋ฐฐ๋‹ฌ์›
shortest_road: [0, 0, 1, 2, 500001, 500001, 500001]
shortest_road: [0, 0, 1, 2, 500001, 4, 500001]
5 ๋ฒˆ ๋ฐฐ๋‹ฌ์›
shortest_road: [0, 0, 1, 2, 500001, 4, 500001]
shortest_road: [0, 0, 1, 2, 500001, 4, 500001]
  • ์–ด? ํ•œ๋ฒˆ์— ์„ฑ๊ณต์ด์•ผ? ์™œ???
  • ์ด์ƒํ•˜๋‹ค. ์ด๊ฒŒ ์„ฑ๊ณตํ•˜๋„ค... ใ…ก.ใ…ก;
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.3MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.4MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.4MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.11ms, 10.3MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.21ms, 10.4MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (0.19ms, 10.3MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (0.37ms, 10.5MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (0.08ms, 10.4MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (0.21ms, 10.3MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (0.14ms, 10.2MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (0.48ms, 10.3MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (0.30ms, 10.4MB)
ํ…Œ์ŠคํŠธ 21 ใ€‰	ํ†ต๊ณผ (0.60ms, 10.5MB)
ํ…Œ์ŠคํŠธ 22 ใ€‰	ํ†ต๊ณผ (0.30ms, 10.3MB)
ํ…Œ์ŠคํŠธ 23 ใ€‰	ํ†ต๊ณผ (0.86ms, 10.4MB)
ํ…Œ์ŠคํŠธ 24 ใ€‰	ํ†ต๊ณผ (0.85ms, 10.4MB)
ํ…Œ์ŠคํŠธ 25 ใ€‰	ํ†ต๊ณผ (0.41ms, 10.4MB)
ํ…Œ์ŠคํŠธ 26 ใ€‰	ํ†ต๊ณผ (0.84ms, 10.5MB)
ํ…Œ์ŠคํŠธ 27 ใ€‰	ํ†ต๊ณผ (0.47ms, 10.6MB)
ํ…Œ์ŠคํŠธ 28 ใ€‰	ํ†ต๊ณผ (0.89ms, 10.6MB)
ํ…Œ์ŠคํŠธ 29 ใ€‰	ํ†ต๊ณผ (0.57ms, 10.5MB)
ํ…Œ์ŠคํŠธ 30 ใ€‰	ํ†ต๊ณผ (0.80ms, 10.5MB)
ํ…Œ์ŠคํŠธ 31 ใ€‰	ํ†ต๊ณผ (0.30ms, 10.3MB)
ํ…Œ์ŠคํŠธ 32 ใ€‰	ํ†ต๊ณผ (0.54ms, 10.3MB)

๊ณ ์ˆ˜์˜ ํ’€์ด

  • ์ฝ”๋“œ๋Š” ๋” ์งง์€๋ฐ ํŠ€๋Š” ๊ตฌ๊ฐ„์ด ์žˆ๋„ค?
def solution(N, road, K):
    visited = [False] * (N + 1)
    costs = [float('inf')] * (N + 1)
    costs[1] = 0
    parents = [1]          
    while (parents):
        parent = parents.pop(0)
        visited[parent] = True
        for a, b, cost in road:
            if (a == parent or b == parent):
                target = b if a == parent else a
                if costs[target] > costs[parent] + cost:
                    costs[target] = costs[parent] + cost
                    parents.append(target)

    return sum(1 for c in costs if c <= K)
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.2MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.4MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.4MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.11ms, 10.3MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.07ms, 10.2MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (8.59ms, 10.4MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (14.15ms, 10.3MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.4MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.2MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (2.43ms, 10.4MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (12.19ms, 10.3MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (1.71ms, 10.2MB)
ํ…Œ์ŠคํŠธ 21 ใ€‰	ํ†ต๊ณผ (23.51ms, 10.5MB)
ํ…Œ์ŠคํŠธ 22 ใ€‰	ํ†ต๊ณผ (4.09ms, 10.2MB)
ํ…Œ์ŠคํŠธ 23 ใ€‰	ํ†ต๊ณผ (20.51ms, 10.3MB)
ํ…Œ์ŠคํŠธ 24 ใ€‰	ํ†ต๊ณผ (10.55ms, 10.5MB)
ํ…Œ์ŠคํŠธ 25 ใ€‰	ํ†ต๊ณผ (35.28ms, 10.5MB)
ํ…Œ์ŠคํŠธ 26 ใ€‰	ํ†ต๊ณผ (37.42ms, 10.6MB)
ํ…Œ์ŠคํŠธ 27 ใ€‰	ํ†ต๊ณผ (32.64ms, 10.4MB)
ํ…Œ์ŠคํŠธ 28 ใ€‰	ํ†ต๊ณผ (25.05ms, 10.4MB)
ํ…Œ์ŠคํŠธ 29 ใ€‰	ํ†ต๊ณผ (38.29ms, 10.6MB)
ํ…Œ์ŠคํŠธ 30 ใ€‰	ํ†ต๊ณผ (6.95ms, 10.6MB)
ํ…Œ์ŠคํŠธ 31 ใ€‰	ํ†ต๊ณผ (0.15ms, 10.3MB)
ํ…Œ์ŠคํŠธ 32 ใ€‰	ํ†ต๊ณผ (0.54ms, 10MB)
  • sys.maxsize ๋ผ๋Š”๊ฒŒ ์žˆ๊ตฌ๋‚˜...
import sys
def solution(N, road, K):
    visited, D, r = [False]*(N+1), [sys.maxsize]*(N+1), [[(None, None)]] + [[] for _ in range(N)]
    for e in road:
        r[e[0]].append((e[1],e[2]))
        r[e[1]].append((e[0],e[2]))
    D[1] = 0
    for _ in range(1,N+1): 
        min_ = sys.maxsize
        for i in range(1,N+1): 
            if not visited[i] and D[i] < min_:
                min_ = D[i]
                m = i
        visited[m] = True
        for w, wt in r[m]:
            if D[m] + wt < D[w]:
                D[w] = D[m] + wt

    return len([d for d in D if d <= K])
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.4MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.4MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.3MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.3MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.11ms, 10.3MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.08ms, 10.4MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (0.59ms, 10.4MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (0.86ms, 10.5MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.3MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (0.07ms, 10.3MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (0.30ms, 10.2MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (0.80ms, 10.4MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (0.27ms, 10.1MB)
ํ…Œ์ŠคํŠธ 21 ใ€‰	ํ†ต๊ณผ (1.87ms, 10.7MB)
ํ…Œ์ŠคํŠธ 22 ใ€‰	ํ†ต๊ณผ (0.63ms, 10.3MB)
ํ…Œ์ŠคํŠธ 23 ใ€‰	ํ†ต๊ณผ (1.78ms, 10.5MB)
ํ…Œ์ŠคํŠธ 24 ใ€‰	ํ†ต๊ณผ (0.94ms, 10.3MB)
ํ…Œ์ŠคํŠธ 25 ใ€‰	ํ†ต๊ณผ (1.51ms, 10.6MB)
ํ…Œ์ŠคํŠธ 26 ใ€‰	ํ†ต๊ณผ (1.77ms, 10.4MB)
ํ…Œ์ŠคํŠธ 27 ใ€‰	ํ†ต๊ณผ (1.93ms, 10.6MB)
ํ…Œ์ŠคํŠธ 28 ใ€‰	ํ†ต๊ณผ (1.51ms, 10.5MB)
ํ…Œ์ŠคํŠธ 29 ใ€‰	ํ†ต๊ณผ (1.56ms, 10.6MB)
ํ…Œ์ŠคํŠธ 30 ใ€‰	ํ†ต๊ณผ (1.53ms, 10.5MB)
ํ…Œ์ŠคํŠธ 31 ใ€‰	ํ†ต๊ณผ (0.27ms, 10.2MB)
ํ…Œ์ŠคํŠธ 32 ใ€‰	ํ†ต๊ณผ (0.34ms, 10.2MB)
  • ๋‹ค์ต์ŠคํŠธ๋ผ
import heapq
import sys
inf = sys.maxsize


def dijkstra(start, graph, distance):
    q = []
    heapq.heappush(q, (0, start))
    distance[start] = 0
    while q:
        dist, now = heapq.heappop(q)
        if distance[now] < dist:
            continue
        for i, v in enumerate(graph[now]):
            cost = dist + v
            if cost < distance[i]:
                distance[i] = cost
                heapq.heappush(q, (cost, i))
    return distance


def solution(N, road, K):
    answer = 0
    graph = [[inf for _ in range(N + 1)] for _ in range(N + 1)]
    distance = [inf] * (N + 1)
    for r in road:
        s, e, c = r
        if graph[s][e] < c:
            continue
        graph[s][e] = c
        graph[e][s] = c

    for i in range(N):
        graph[i][i] = 0

    distance = dijkstra(1, graph, distance)
    for item in distance:
        if item <= K:
            answer += 1
    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.08ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.1MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.4MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.1MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.3MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.4MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.21ms, 10.1MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.16ms, 10.3MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (0.30ms, 10.3MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (0.41ms, 10.3MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (0.12ms, 10.3MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (0.25ms, 10.4MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (0.71ms, 10.3MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (1.05ms, 10.3MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (0.76ms, 10.3MB)
ํ…Œ์ŠคํŠธ 21 ใ€‰	ํ†ต๊ณผ (0.96ms, 10.4MB)
ํ…Œ์ŠคํŠธ 22 ใ€‰	ํ†ต๊ณผ (0.42ms, 10.1MB)
ํ…Œ์ŠคํŠธ 23 ใ€‰	ํ†ต๊ณผ (0.65ms, 10.4MB)
ํ…Œ์ŠคํŠธ 24 ใ€‰	ํ†ต๊ณผ (0.49ms, 10.2MB)
ํ…Œ์ŠคํŠธ 25 ใ€‰	ํ†ต๊ณผ (0.70ms, 10.5MB)
ํ…Œ์ŠคํŠธ 26 ใ€‰	ํ†ต๊ณผ (0.62ms, 10.3MB)
ํ…Œ์ŠคํŠธ 27 ใ€‰	ํ†ต๊ณผ (0.60ms, 10.5MB)
ํ…Œ์ŠคํŠธ 28 ใ€‰	ํ†ต๊ณผ (0.66ms, 10.5MB)
ํ…Œ์ŠคํŠธ 29 ใ€‰	ํ†ต๊ณผ (0.66ms, 10.6MB)
ํ…Œ์ŠคํŠธ 30 ใ€‰	ํ†ต๊ณผ (0.60ms, 10.6MB)
ํ…Œ์ŠคํŠธ 31 ใ€‰	ํ†ต๊ณผ (0.33ms, 10.2MB)
ํ…Œ์ŠคํŠธ 32 ใ€‰	ํ†ต๊ณผ (0.35ms, 10MB)
  • ์™ ์ง€ ๋‚ด๊ฐ€ ๊ตฌํ˜„ํ•œ ๋ฐฉ์‹์ด ์ œ์ผ ๋น ๋ฅธ ๋“ฏ?
  • ์ข€ ๋” ์ตœ์ ํ™” ๊ฐ€๋Šฅํ•˜๋ ค๋‚˜?
def solution(N, road, K):
    answer = 0
    town = [[500001] * (N+1) for i in range(N+1)]
    shortest_road = town[0] 
    delivery_man = [False] * (N+1)
    
    for (s,e,t) in road:
        if town[s][e] > t:
            town[s][e] = town[e][s] = t
    for i in range(N+1): town[i][i] = 0
    
    shortest_road[1] = 0 # ์‹œ์ž‘ ๋งˆ์„ ์ถœ๋ฐœ ์‹œ๊ฐ„ ์„ธํŒ…!
    delivery_man[1] = True # ๋ฐฐ๋‹ฌ์› ์ถœ๋ฐœ!
    
    while sum(delivery_man) > 0: # ๋ฐฐ๋‹ฌ์›์ด ์žˆ๋Š” ๋™์•ˆ ๋ฃจํ”„
        for rider in range(1, N+1):
            if delivery_man[rider] == True:
                shortest_road_rider = shortest_road[rider]
                for i in range(1, N+1):
                    if shortest_road[i] != 0:
                        _time = town[rider][i]
                        _new_time = shortest_road_rider + _time
                        if _new_time <= K and _new_time < shortest_road[i]:
                            shortest_road[i] = _new_time
                            delivery_man[i] = True
                delivery_man[rider] = False
    for i in range(1, N+1):
        if shortest_road[i] < 500001:
            answer += 1
    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.4MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.12ms, 10.3MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.12ms, 10.4MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (0.19ms, 10.2MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (0.36ms, 10.4MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.4MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (0.11ms, 10.4MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (0.15ms, 10.1MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (0.24ms, 10.3MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (0.43ms, 10.4MB)
ํ…Œ์ŠคํŠธ 21 ใ€‰	ํ†ต๊ณผ (0.33ms, 10.4MB)
ํ…Œ์ŠคํŠธ 22 ใ€‰	ํ†ต๊ณผ (0.33ms, 10.2MB)
ํ…Œ์ŠคํŠธ 23 ใ€‰	ํ†ต๊ณผ (0.53ms, 10.5MB)
ํ…Œ์ŠคํŠธ 24 ใ€‰	ํ†ต๊ณผ (0.47ms, 10.3MB)
ํ…Œ์ŠคํŠธ 25 ใ€‰	ํ†ต๊ณผ (0.43ms, 10.4MB)
ํ…Œ์ŠคํŠธ 26 ใ€‰	ํ†ต๊ณผ (0.48ms, 10.4MB)
ํ…Œ์ŠคํŠธ 27 ใ€‰	ํ†ต๊ณผ (0.48ms, 10.5MB)
ํ…Œ์ŠคํŠธ 28 ใ€‰	ํ†ต๊ณผ (0.51ms, 10.5MB)
ํ…Œ์ŠคํŠธ 29 ใ€‰	ํ†ต๊ณผ (0.62ms, 10.5MB)
ํ…Œ์ŠคํŠธ 30 ใ€‰	ํ†ต๊ณผ (0.58ms, 10.3MB)
ํ…Œ์ŠคํŠธ 31 ใ€‰	ํ†ต๊ณผ (0.31ms, 10.4MB)
ํ…Œ์ŠคํŠธ 32 ใ€‰	ํ†ต๊ณผ (0.30ms, 10.2MB)
  • ์ด๊ฑฐ ๋„ฃ์„๊นŒ ๋ง๊นŒ ๊ณ ๋ฏผํ–ˆ๋Š”๋ฐ...
    • if shortest_road[i] != 0:
    • ๋บ€ ๋ฒ„์ „์ด ๋” ๋น ๋ฅด๋‹ค. ์—ญ์‹œ ๊ทธ๋ƒฅ ์กฐ๊ฑด๋ฌธ์ด๋“  ๋ฐ˜๋ณต๋ฌธ์ด๋“  ๋นผ์„œ ์ฝ”๋”ฉ ๋ผ์ธ ์ˆ˜๋ฅผ ์ค„์ด๋Š” ๊ฒŒ ๋” ๋น ๋ฅธ ๋“ฏ...
๋„ฃ์€ ๋ฒ„์ „ ๋บ€ ๋ฒ„์ „
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰ ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰ ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰ ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰ ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰ ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰ ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰ ํ†ต๊ณผ (0.03ms, 10.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰ ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰ ํ†ต๊ณผ (0.01ms, 10.4MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰ ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰ ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰ ํ†ต๊ณผ (0.12ms, 10.3MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰ ํ†ต๊ณผ (0.12ms, 10.4MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰ ํ†ต๊ณผ (0.19ms, 10.2MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰ ํ†ต๊ณผ (0.36ms, 10.4MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰ ํ†ต๊ณผ (0.06ms, 10.4MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰ ํ†ต๊ณผ (0.11ms, 10.4MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰ ํ†ต๊ณผ (0.15ms, 10.1MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰ ํ†ต๊ณผ (0.24ms, 10.3MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰ ํ†ต๊ณผ (0.43ms, 10.4MB)
ํ…Œ์ŠคํŠธ 21 ใ€‰ ํ†ต๊ณผ (0.33ms, 10.4MB)
ํ…Œ์ŠคํŠธ 22 ใ€‰ ํ†ต๊ณผ (0.33ms, 10.2MB)
ํ…Œ์ŠคํŠธ 23 ใ€‰ ํ†ต๊ณผ (0.53ms, 10.5MB)
ํ…Œ์ŠคํŠธ 24 ใ€‰ ํ†ต๊ณผ (0.47ms, 10.3MB)
ํ…Œ์ŠคํŠธ 25 ใ€‰ ํ†ต๊ณผ (0.43ms, 10.4MB)
ํ…Œ์ŠคํŠธ 26 ใ€‰ ํ†ต๊ณผ (0.48ms, 10.4MB)
ํ…Œ์ŠคํŠธ 27 ใ€‰ ํ†ต๊ณผ (0.48ms, 10.5MB)
ํ…Œ์ŠคํŠธ 28 ใ€‰ ํ†ต๊ณผ (0.51ms, 10.5MB)
ํ…Œ์ŠคํŠธ 29 ใ€‰ ํ†ต๊ณผ (0.62ms, 10.5MB)
ํ…Œ์ŠคํŠธ 30 ใ€‰ ํ†ต๊ณผ (0.58ms, 10.3MB)
ํ…Œ์ŠคํŠธ 31 ใ€‰ ํ†ต๊ณผ (0.31ms, 10.4MB)
ํ…Œ์ŠคํŠธ 32 ใ€‰ ํ†ต๊ณผ (0.30ms, 10.2MB)
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰ ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰ ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰ ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰ ํ†ต๊ณผ (0.01ms, 10.4MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰ ํ†ต๊ณผ (0.03ms, 10.3MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰ ํ†ต๊ณผ (0.02ms, 10.4MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰ ํ†ต๊ณผ (0.02ms, 10.4MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰ ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰ ํ†ต๊ณผ (0.01ms, 10.4MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰ ํ†ต๊ณผ (0.03ms, 10.3MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰ ํ†ต๊ณผ (0.04ms, 10.3MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰ ํ†ต๊ณผ (0.18ms, 10.3MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰ ํ†ต๊ณผ (0.18ms, 10.2MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰ ํ†ต๊ณผ (0.18ms, 10.2MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰ ํ†ต๊ณผ (0.63ms, 10.2MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰ ํ†ต๊ณผ (0.05ms, 10.4MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰ ํ†ต๊ณผ (0.09ms, 10.4MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰ ํ†ต๊ณผ (0.13ms, 10.4MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰ ํ†ต๊ณผ (0.23ms, 10.3MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰ ํ†ต๊ณผ (0.19ms, 10.4MB)
ํ…Œ์ŠคํŠธ 21 ใ€‰ ํ†ต๊ณผ (0.34ms, 10.3MB)
ํ…Œ์ŠคํŠธ 22 ใ€‰ ํ†ต๊ณผ (0.27ms, 10.2MB)
ํ…Œ์ŠคํŠธ 23 ใ€‰ ํ†ต๊ณผ (0.58ms, 10.4MB)
ํ…Œ์ŠคํŠธ 24 ใ€‰ ํ†ต๊ณผ (0.40ms, 10.3MB)
ํ…Œ์ŠคํŠธ 25 ใ€‰ ํ†ต๊ณผ (0.39ms, 10.4MB)
ํ…Œ์ŠคํŠธ 26 ใ€‰ ํ†ต๊ณผ (0.43ms, 10.6MB)
ํ…Œ์ŠคํŠธ 27 ใ€‰ ํ†ต๊ณผ (0.42ms, 10.3MB)
ํ…Œ์ŠคํŠธ 28 ใ€‰ ํ†ต๊ณผ (0.45ms, 10.6MB)
ํ…Œ์ŠคํŠธ 29 ใ€‰ ํ†ต๊ณผ (0.53ms, 10.4MB)
ํ…Œ์ŠคํŠธ 30 ใ€‰ ํ†ต๊ณผ (0.44ms, 10.5MB)
ํ…Œ์ŠคํŠธ 31 ใ€‰ ํ†ต๊ณผ (0.24ms, 10.3MB)
ํ…Œ์ŠคํŠธ 32 ใ€‰ ํ†ต๊ณผ (0.25ms, 10.2MB)

์˜ค๋Š˜์€ ํ•œ๋ฐฉ์— ์„ฑ๊ณตํ•ด์„œ ๊ธฐ๋ถ„์ด ๋‚ ์•„๊ฐˆ ๊ฒƒ ๊ฐ™๋‹ค.

728x90
๋ฐ˜์‘ํ˜•