ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Python] ๋ฐฑ์ค€ 11265 ๋๋‚˜์ง€ ์•Š๋Š” ํŒŒํ‹ฐ (Floyd-Warshall)
    ๐Ÿ’ป Algorithm/Python 2022. 7. 8. 23:43

    ๐Ÿ“Œ ๋ฌธ์ œ

    ํŒŒํ‹ฐ๋ฅผ ์ข‹์•„ํ•˜๋Š” ๋ฏผํ˜ธ๋Š” ๋์—†์ด ํŒŒํ‹ฐ๊ฐ€ ์—ด๋ฆฌ๋Š” ๋†€์ด๋™์‚ฐ "๋ฏผํ˜ธ์›”๋“œ"๋ฅผ ์„ธ์› ๋‹ค. ์ฒ˜์Œ์—๋Š” ํ•œ๊ฐœ์˜ ํŒŒํ‹ฐ์žฅ๋งŒ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ž‘์€ ๋†€์ด๋™์‚ฐ์ด์—ˆ์ง€๋งŒ, ์‚ฌ๋žŒ๋“ค์˜ ์ ์  ๋งŽ์ด ์ฐพ์•„์™€ ํŒŒํ‹ฐ์žฅ์„ ์ฆ์ถ•ํ–ˆ๊ณ  ํ˜„์žฌ๋Š” N๊ฐœ์˜ ํŒŒํ‹ฐ์žฅ์„ ๊ฐ€์ง„ ํฐ ๋†€์ด๋™์‚ฐ์ด ๋˜์—ˆ๋‹ค. ๋ฏผํ˜ธ๋Š” ํŒŒํ‹ฐ์žฅ์„ ์ฆ์ถ•ํ• ๋•Œ๋งˆ๋‹ค ํŽธ์˜๋ฅผ ์œ„ํ•ด ์ƒˆ๋กœ์šด ํŒŒํ‹ฐ์žฅ๊ณผ ๊ธฐ์กด์˜ ๋ชจ๋“  ํŒŒํ‹ฐ์žฅ์ด ์ง์ ‘์ ์œผ๋กœ ์—ฐ๊ฒฐ์ด ๋  ์ˆ˜ ์žˆ๋Š” ๋„๋กœ๋“ค์„ ๋งŒ๋“ค์—ˆ๋‹ค. ์ด๋•Œ ๋งŒ๋“ค์–ด์ง„ ๋„๋กœ๋“ค์€ ์‚ฌ์šฉ์ž๋“ค์˜ ํŽธ์˜๋ฅผ ์œ„ํ•ด ์ผ๋ฐฉํ†ตํ–‰์œผ๋กœ ์„ค๊ณ„๊ฐ€ ๋˜์—ˆ๋‹ค.

     

    ํŒŒํ‹ฐ์žฅ์ด ์ ์„๋•Œ๋Š” ๊ดœ์ฐฎ์•˜์ง€๋งŒ ํŒŒํ‹ฐ์žฅ์ด ๋งŽ์•„์ง„ ์ง€๊ธˆ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‘ ๊ฐ€์ง€ ๋ฌธ์ œ์ ์ด ๋ฐœ์ƒํ–ˆ๋‹ค.

     

    1. A ํŒŒํ‹ฐ์žฅ์—์„œ B ํŒŒํ‹ฐ์žฅ์œผ๋กœ ๋นจ๋ฆฌ ๊ฐˆ ์ˆ˜ ์žˆ๋„๋ก ์ง์ ‘ ์—ฐ๊ฒฐ์ด ๋œ ์ผ๋ฐฉํ†ตํ–‰ ๋„๋กœ๋ฅผ ๋งŒ๋“ค์—ˆ์ง€๋งŒ A์™€ B๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ํŒŒํ‹ฐ์žฅ์„ ๊ฒฝ์œ ํ•ด์„œ ๋” ๋นจ๋ฆฌ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค.
    2. ์ง€๊ธˆ์œผ๋กœ๋ถ€ํ„ฐ C๋งŒํผ์˜ ์‹œ๊ฐ„ ๋’ค์— B๋ฒˆ ํŒŒํ‹ฐ์žฅ์—์„œ ์ƒˆ๋กญ๊ฒŒ ํŒŒํ‹ฐ๊ฐ€ ์—ด๋ฆฌ๋Š”๋ฐ 1๋ฒˆ๊ณผ ๊ฐ™์€ ์ด์œ ๋•Œ๋ฌธ์— ํ˜„์žฌ ์žˆ๋Š” AํŒŒํ‹ฐ์žฅ์—์„œ B๋ฒˆ ํŒŒํ‹ฐ์žฅ๊นŒ์ง€ ํŒŒํ‹ฐ๊ฐ€ ์—ด๋ฆฌ๋Š” ์‹œ๊ฐ„๊นŒ์ง€ ๋งž์ถฐ ๊ฐˆ ์ˆ˜ ์žˆ๋Š”์ง€ ์‰ฝ๊ฒŒ ์•Œ ์ˆ˜ ์—†๋‹ค.

     

    ์ด๋Ÿฌํ•œ ๋ฌธ์ œ์ ์œผ๋กœ ์ด์šฉ๊ฐ๋“ค์˜ ๋ถˆ๋งŒ์ด ์ ์  ์ปค์ ธ๊ฐ”๊ณ  ๋ฏผํ˜ธ๋Š” ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋น ๋ฅธ ๋„ค๋น„๊ฒŒ์ด์…˜ ์„œ๋น„์Šค๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ๋กœ ํ•˜์˜€์œผ๋‚˜ ์„œ๋น„์Šค ์š”์ฒญ์ด ๋„ˆ๋ฌด ๋งŽ์•„ ์—…๋ฌด๊ฐ€ ๋งˆ๋น„๋˜๊ธฐ์— ์ด๋ฅด๋ €๋‹ค. ์ด์— ๋ฏผํ˜ธ๋Š” ์ฒœ์žฌํ”„๋กœ๊ทธ๋ž˜๋จธ์ธ ๋‹น์‹ ์—๊ฒŒ ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ด ๋‹ฌ๋ผ๊ณ  ์š”์ฒญํ•˜์˜€๋‹ค. ๋ฏผํ˜ธ๋ฅผ ๋„์™€ ํ•œ ํŒŒํ‹ฐ์žฅ์—์„œ ๋‹ค๋ฅธ ํŒŒํ‹ฐ์žฅ์—๊นŒ์ง€ ์‹œ๊ฐ„๋‚ด์— ๋„์ฐฉํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ์•Œ์•„๋ด์ฃผ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์ž.

    ์ž…๋ ฅ

    ์ž…๋ ฅ์˜ ์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ํŒŒํ‹ฐ์žฅ์˜ ํฌ๊ธฐ N(5 ≤ N ≤ 500)๊ณผ ์„œ๋น„์Šค๋ฅผ ์š”์ฒญํ•œ ์†๋‹˜์˜ ์ˆ˜ M(1 ≤ M ≤ 10,000) ์ด ์ฃผ์–ด์ง„๋‹ค. ๊ฐ๊ฐ์˜ ํŒŒํ‹ฐ์žฅ์€ 1๋ฒˆ๋ถ€ํ„ฐ N๋ฒˆ๊นŒ์ง€ ๋ฒˆํ˜ธ๊ฐ€ ๋ถ™์—ฌ์ ธ ์žˆ๋‹ค. ๋‹ค์Œ์—๋Š” N๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ๊ฐ๊ฐ N๊ฐœ์˜ ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. i๋ฒˆ์งธ ์ค„์˜ j๋ฒˆ์งธ ์ˆ˜ T(1 ≤ T ≤ 1,000,000)๋Š” i๋ฒˆ ํŒŒํ‹ฐ์žฅ์—์„œ j๋ฒˆ ํŒŒํ‹ฐ์žฅ์œผ๋กœ ์ง์ ‘์ ์œผ๋กœ ์—ฐ๊ฒฐ๋œ ๋„๋กœ๋ฅผ ํ†ตํ•ด ์ด๋™ํ•˜๋Š” ์‹œ๊ฐ„์„ ์˜๋ฏธํ•œ๋‹ค.

     

    ๋‹ค์Œ M๊ฐœ์˜ ์ค„์—๋Š” ์„ธ๊ฐœ์˜ ์ •์ˆ˜ A, B, C๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. A(1 ≤ A ≤ N) ๋Š” ์„œ๋น„์Šค๋ฅผ ์š”์ฒญํ•œ ์†๋‹˜์ด ์œ„์น˜ํ•œ ํŒŒํ‹ฐ์žฅ์˜ ๋ฒˆํ˜ธ, B(1 ≤ B ≤ N) ๋‹ค์Œ ํŒŒํ‹ฐ๊ฐ€ ์—ด๋ฆฌ๋Š” ํŒŒํ‹ฐ์žฅ์˜ ๋ฒˆํ˜ธ, C(1 ≤ C ≤ 1,000,000,000)๋Š” ์ง€๊ธˆ์œผ๋กœ๋ถ€ํ„ฐ ๋‹ค์Œ ํŒŒํ‹ฐ๊ฐ€ ์—ด๋ฆฌ๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์„ ์˜๋ฏธํ•œ๋‹ค.

    ์ถœ๋ ฅ

    M๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ์„œ๋น„์Šค๋ฅผ ์š”์ฒญํ•œ ์†๋‹˜์ด ์‹œ๊ฐ„๋‚ด์— ๋‹ค๋ฅธ ํŒŒํ‹ฐ์žฅ์— ๋„์ฐฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉด “Enjoy other party”๋ฅผ, ์‹œ๊ฐ„๋‚ด์— ๋„์ฐฉํ•  ์ˆ˜ ์—†์œผ๋ฉด "Stay here”๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

    ์˜ˆ์ œ ์ž…๋ ฅ 1

    5 10
    0 4 4 8 7
    7 0 7 7 4
    1 4 0 5 4
    5 2 2 0 7
    1 4 1 6 0
    1 3 8
    2 4 1
    4 1 1
    1 5 5
    3 2 1
    3 2 5
    4 5 10
    5 3 2
    1 4 1
    1 4 11

    ์˜ˆ์ œ ์ถœ๋ ฅ 1

    Enjoy other party
    Stay here
    Stay here
    Stay here
    Stay here
    Enjoy other party
    Enjoy other party
    Enjoy other party
    Stay here
    Enjoy other party

     


    ๐Ÿ“Œ ํ’€์ด

    ๐Ÿ’ฌ Code

    import sys
    input = sys.stdin.readline
    
    n, m = map(int, input().split())
    graph = [list(map(int, input().split())) for _ in range(n)]
    
    for k in range(n):
        for i in range(n):
            for j in range(n):
                if graph[i][j] > graph[i][k] + graph[k][j]:
                    graph[i][j] = graph[i][k] + graph[k][j]
    
    for _ in range(m):
        a, b, time = map(int, input().split())
        if graph[a-1][b-1] <= time:
            print('Enjoy other party')
        else:
            print('Stay here')

    ๐Ÿ’ก Solution

    A ํŒŒํ‹ฐ์žฅ์—์„œ B ํŒŒํ‹ฐ์žฅ์œผ๋กœ ๋นจ๋ฆฌ ๊ฐˆ ์ˆ˜ ์žˆ๋„๋ก
    ์ง์ ‘ ์—ฐ๊ฒฐ์ด ๋œ ์ผ๋ฐฉํ†ตํ–‰ ๋„๋กœ๋ฅผ ๋งŒ๋“ค์—ˆ์ง€๋งŒ
    A์™€ B๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ํŒŒํ‹ฐ์žฅ์„ ๊ฒฝ์œ ํ•ด์„œ
    ๋” ๋นจ๋ฆฌ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค.

    ์œ„์˜ ์กฐ๊ฑด ๋•Œ๋ฌธ์— ์ด ๋ฌธ์ œ๋Š” ์ตœ๋‹จ๊ฒฝ๋กœ ๋ฌธ์ œ์— ํ•ด๋‹นํ•˜๊ณ , ๋ชจ๋“  ๋…ธ๋“œ์˜ ์ตœ๋‹จ๊ฒฝ๋กœ๋ฅผ ๊ณ„์‚ฐํ•ด์•ผ ํ•˜๋ฏ€๋กœ ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

     

    for k in range(n):
        for i in range(n):
            for j in range(n):
                if graph[i][j] > graph[i][k] + graph[k][j]:
                    graph[i][j] = graph[i][k] + graph[k][j]

    i ํŒŒํ‹ฐ์žฅ์—์„œ j ํŒŒํ‹ฐ์žฅ์œผ๋กœ ๊ฐ€๋ ค๊ณ  ํ•  ๋•Œ, k ํŒŒํ‹ฐ์žฅ์„ ๊ฒฝ์œ ํ•ด์„œ ๊ฐ€๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋” ๋น ๋ฅด๋‹ค๋ฉด ๊ฒฝ์œ ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์ตœ๋‹จ๊ฒฝ๋กœ๋ฅผ ๊ฐฑ์‹ ํ•ด์ค๋‹ˆ๋‹ค.

     

    for _ in range(m):
        a, b, time = map(int, input().split())
        if graph[a-1][b-1] <= time:
            print('Enjoy other party')
        else:
            print('Stay here')

    ๋ฌธ์ œ์—์„œ ํŒŒํ‹ฐ์žฅ์˜ ๋ฒˆํ˜ธ๋ฅผ 1๋ฒˆ๋ถ€ํ„ฐ n๋ฒˆ๊นŒ์ง€๋กœ ์ง€์ •ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, graph์— ์ ‘๊ทผํ•  ๋•Œ๋Š” ์ธ๋ฑ์Šค์— 1์”ฉ ๋นผ ์ค€ ์ƒํƒœ๋กœ ์ ‘๊ทผํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

     

    ๐Ÿค” ์‹œ๊ฐ„์ดˆ๊ณผ Issue

    min์œผ๋กœ ์ตœ์†Ÿ๊ฐ’ ๊ฐฑ์‹  โŒ

    for k in range(n):
        for i in range(n):
            for j in range(n):
                graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j])

    if๋ฌธ์œผ๋กœ ์ตœ์†Ÿ๊ฐ’ ๊ฐฑ์‹  โญ•

    for k in range(n):
        for i in range(n):
            for j in range(n):
                if graph[i][j] > graph[i][k] + graph[k][j]:
                    graph[i][j] = graph[i][k] + graph[k][j]

    min()์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ graph[i][j], graph[i][k] + graph[k][j]๋ฅผ ๋น„๊ตํ•œ ํ›„ graph[i][j]์— ํ• ๋‹นํ•˜๋Š” ์ž‘์—…์ด ๋งค๋ฒˆ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ๋•Œ๋ฌธ์— graph[i][j]๋ฅผ graph[i][j]์— ํ• ๋‹นํ•˜๋Š” ์ผ์ด ๋นˆ๋ฒˆํ•˜๊ฒŒ ๋ฐœ์ƒํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

     

    ๋ฐ˜๋ฉด if๋ฌธ์€ ์กฐ๊ฑด์ด ๋งž์ง€ ์•Š์œผ๋ฉด ํ• ๋‹นํ•˜์ง€ ์•Š๊ณ  ๋„˜์–ด๊ฐ€๊ธฐ ๋•Œ๋ฌธ์— ๋ถˆํ•„์š”ํ•œ ํ• ๋‹น ์ž‘์—…์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.


    for i in range(n) โŒ

    for i in range(m):
        a, b, time = map(int, input().split())

    for _ in range(n) โญ•

    for _ in range(m):
        a, b, time = map(int, input().split())

    for๋ฌธ์—์„œ ๋ณ€์ˆ˜ i๋ฅผ ์‚ฌ์šฉํ•  ์ผ์ด ์—†์œผ๋ฉด ๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š๋Š” ์Šต๊ด€ ๋“ค์ด๊ธฐ ใ…  ์ „ ์ด๊ฑฐ ํ•˜๋‚˜ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„์ดˆ๊ณผ์— ๊ฑธ๋ ค๋ฒ„๋ฆด ์ค„ ๋ชฐ๋ž์–ด์š”........


    pypy3์œผ๋กœ ์ฑ„์ ํ•˜๋ฉด ๋‘˜ ๋‹ค โŒ๋กœ ์ž‘์„ฑํ•ด๋„ ์‹œ๊ฐ„์ดˆ๊ณผ ํŒ์ • ์—†์ด ์ •๋‹ตํŒ์ •์„ ๋ฐ›์„ ์ˆ˜ ์žˆ์ง€๋งŒ, python3์œผ๋กœ ์ฑ„์ ํ•˜๋ฉด ๋‘˜ ๋‹ค โญ•๋กœ ์ž‘์„ฑํ•ด์•ผ ์ •๋‹ตํŒ์ •์„ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

     

    ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๋…ธ๋“œ๊ฐ€ 500๊ฐœ ์ดํ•˜์ผ ๊ฒฝ์šฐ์— ์ ํ•ฉํ•œ๋ฐ, ์ด ๋ฌธ์ œ์—์„œ๋Š” ๋…ธ๋“œ๊ฐ€ ์ตœ๋Œ€ 500๊ฐœ๊ฐ€ ์ฃผ์–ด์ ธ์„œ ์‹œ๊ฐ„์ด ์ƒ๋‹นํžˆ ๊ฐ„๋‹น๊ฐ„๋‹นํ•ฉ๋‹ˆ๋‹ค. ์‹œ๊ฐ„์ด ๋„๋„ํ•˜๋ฉด ๋ฌด์‹œํ• ๋งŒํ•œ ์ฐจ์ด๊ฒ ์œผ๋‚˜, 500๊ฐœ์˜ ๋…ธ๋“œ๋กœ 3์ค‘ for๋ฌธ์„ ๋Œ๋ฆฌ๋‹ค ๋ณด๋‹ˆ ์ž‘์—…์ด ๋งŽ์•„ ๋ฏธ๋ฌ˜ํ•œ ์ฐจ์ด๊ฐ€ ์‹œ๊ฐ„์ดˆ๊ณผ ํŒ์ • ์—ฌ๋ถ€๋ฅผ ๊ฐ€๋ฅด๊ฒŒ ๋˜๋Š” ๊ฒƒ ^_ใ… 

     

     

     

    ๋Œ“๊ธ€

Designed by Tistory.