[Python - ๋ฐฑ์ค€ 14889] ์Šคํƒ€ํŠธ์™€ ๋งํฌ

2021. 4. 14. 16:35ใ†๐Ÿ“š Algorithm/๐Ÿ’ช๐Ÿป Python ๋ฌธ์ œ ํ’€์ด

๋ฐ˜์‘ํ˜•

 

 

14889๋ฒˆ: ์Šคํƒ€ํŠธ์™€ ๋งํฌ

์˜ˆ์ œ 2์˜ ๊ฒฝ์šฐ์— (1, 3, 6), (2, 4, 5)๋กœ ํŒ€์„ ๋‚˜๋ˆ„๋ฉด ๋˜๊ณ , ์˜ˆ์ œ 3์˜ ๊ฒฝ์šฐ์—๋Š” (1, 2, 4, 5), (3, 6, 7, 8)๋กœ ํŒ€์„ ๋‚˜๋ˆ„๋ฉด ๋œ๋‹ค.

www.acmicpc.net

๋ฌธ์ œ ์„ค๋ช…

์˜ค๋Š˜์€ ์Šคํƒ€ํŠธ๋งํฌ์— ๋‹ค๋‹ˆ๋Š” ์‚ฌ๋žŒ๋“ค์ด ๋ชจ์—ฌ์„œ ์ถ•๊ตฌ๋ฅผ ํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค. ์ถ•๊ตฌ๋Š” ํ‰์ผ ์˜คํ›„์— ํ•˜๊ณ  ์˜๋ฌด ์ฐธ์„๋„ ์•„๋‹ˆ๋‹ค. ์ถ•๊ตฌ๋ฅผ ํ•˜๊ธฐ ์œ„ํ•ด ๋ชจ์ธ ์‚ฌ๋žŒ์€ ์ด N๋ช…์ด๊ณ  ์‹ ๊ธฐํ•˜๊ฒŒ๋„ N์€ ์ง์ˆ˜์ด๋‹ค. ์ด์ œ N/2๋ช…์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ์Šคํƒ€ํŠธ ํŒ€๊ณผ ๋งํฌ ํŒ€์œผ๋กœ ์‚ฌ๋žŒ๋“ค์„ ๋‚˜๋ˆ ์•ผ ํ•œ๋‹ค.

BOJ๋ฅผ ์šด์˜ํ•˜๋Š” ํšŒ์‚ฌ ๋‹ต๊ฒŒ ์‚ฌ๋žŒ์—๊ฒŒ ๋ฒˆํ˜ธ๋ฅผ 1๋ถ€ํ„ฐ N๊นŒ์ง€๋กœ ๋ฐฐ์ •ํ–ˆ๊ณ , ์•„๋ž˜์™€ ๊ฐ™์€ ๋Šฅ๋ ฅ์น˜๋ฅผ ์กฐ์‚ฌํ–ˆ๋‹ค. ๋Šฅ๋ ฅ์น˜ Sij๋Š” i๋ฒˆ ์‚ฌ๋žŒ๊ณผ j๋ฒˆ ์‚ฌ๋žŒ์ด ๊ฐ™์€ ํŒ€์— ์†ํ–ˆ์„ ๋•Œ, ํŒ€์— ๋”ํ•ด์ง€๋Š” ๋Šฅ๋ ฅ์น˜์ด๋‹ค. ํŒ€์˜ ๋Šฅ๋ ฅ์น˜๋Š” ํŒ€์— ์†ํ•œ ๋ชจ๋“  ์Œ์˜ ๋Šฅ๋ ฅ์น˜ Sij์˜ ํ•ฉ์ด๋‹ค. Sij๋Š” Sji์™€ ๋‹ค๋ฅผ ์ˆ˜๋„ ์žˆ์œผ๋ฉฐ, i๋ฒˆ ์‚ฌ๋žŒ๊ณผ j๋ฒˆ ์‚ฌ๋žŒ์ด ๊ฐ™์€ ํŒ€์— ์†ํ–ˆ์„ ๋•Œ, ํŒ€์— ๋”ํ•ด์ง€๋Š” ๋Šฅ๋ ฅ์น˜๋Š” Sij์™€ Sji์ด๋‹ค.

N=4์ด๊ณ , S๊ฐ€ ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฝ์šฐ๋ฅผ ์‚ดํŽด๋ณด์ž.

i\j12341234

  1 2 3
4   5 6
7 1   2
3 4 5  

์˜ˆ๋ฅผ ๋“ค์–ด, 1, 2๋ฒˆ์ด ์Šคํƒ€ํŠธ ํŒ€, 3, 4๋ฒˆ์ด ๋งํฌ ํŒ€์— ์†ํ•œ ๊ฒฝ์šฐ์— ๋‘ ํŒ€์˜ ๋Šฅ๋ ฅ์น˜๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

  • ์Šคํƒ€ํŠธ ํŒ€: S12 + S21 = 1 + 4 = 5
  • ๋งํฌ ํŒ€: S34 + S43 = 2 + 5 = 7

1, 3๋ฒˆ์ด ์Šคํƒ€ํŠธ ํŒ€, 2, 4๋ฒˆ์ด ๋งํฌ ํŒ€์— ์†ํ•˜๋ฉด, ๋‘ ํŒ€์˜ ๋Šฅ๋ ฅ์น˜๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

  • ์Šคํƒ€ํŠธ ํŒ€: S13 + S31 = 2 + 7 = 9
  • ๋งํฌ ํŒ€: S24 + S42 = 6 + 4 = 10

์ถ•๊ตฌ๋ฅผ ์žฌ๋ฏธ์žˆ๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์Šคํƒ€ํŠธ ํŒ€์˜ ๋Šฅ๋ ฅ์น˜์™€ ๋งํฌ ํŒ€์˜ ๋Šฅ๋ ฅ์น˜์˜ ์ฐจ์ด๋ฅผ ์ตœ์†Œ๋กœ ํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ์œ„์˜ ์˜ˆ์ œ์™€ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” 1, 4๋ฒˆ์ด ์Šคํƒ€ํŠธ ํŒ€, 2, 3๋ฒˆ ํŒ€์ด ๋งํฌ ํŒ€์— ์†ํ•˜๋ฉด ์Šคํƒ€ํŠธ ํŒ€์˜ ๋Šฅ๋ ฅ์น˜๋Š” 6, ๋งํฌ ํŒ€์˜ ๋Šฅ๋ ฅ์น˜๋Š” 6์ด ๋˜์–ด์„œ ์ฐจ์ด๊ฐ€ 0์ด ๋˜๊ณ  ์ด ๊ฐ’์ด ์ตœ์†Œ์ด๋‹ค.

์ž…๋ ฅํ˜•์‹

์ฒซ์งธ ์ค„์— N(4 ≤ N ≤ 20, N์€ ์ง์ˆ˜)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— S๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ์ค„์€ N๊ฐœ์˜ ์ˆ˜๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๊ณ , i๋ฒˆ ์ค„์˜ j๋ฒˆ์งธ ์ˆ˜๋Š” Sij ์ด๋‹ค. Sii๋Š” ํ•ญ์ƒ 0์ด๊ณ , ๋‚˜๋จธ์ง€ Sij๋Š” 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 100๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ •์ˆ˜์ด๋‹ค.

์ถœ๋ ฅํ˜•์‹

์ฒซ์งธ ์ค„์— ์Šคํƒ€ํŠธ ํŒ€๊ณผ ๋งํฌ ํŒ€์˜ ๋Šฅ๋ ฅ์น˜์˜ ์ฐจ์ด์˜ ์ตœ์†Ÿ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ์‹œ

ํ’€์ด ๊ณผ์ •

๋ฐฑํŠธ๋ ˆํ‚น ๋ฌธ์ œ์˜€๋‹ค. ๋งŒ์•ฝ ์‚ฌ๋žŒ์ด N ๋ช…์ด ์žˆ๋‹ค๋ฉด N ๋ช…์ค‘ N/2๋ช…์„ ๋ฝ‘๋Š” ์กฐํ•ฉ ๋ฌธ์ œ์˜€๋‹ค. ์ดˆ๊ธฐ์—๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋‹ค ๊ตฌํ•˜์—ฌ backtracking ํ•ด์ฃผ์—ˆ์ง€๋งŒ, ๊ณ„์† ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋ถˆํ•„์š”ํ•œ ์กฐํ•ฉ์„ ๊ตฌํ•˜๋Š” ๊ฒƒ์€ ์ œ์™ธ์‹œ์ผœ์ฃผ๋Š” ๊ฑธ ์ƒ๊ฐํ•ด๋‚ด์•ผ ํ–ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด 0,1,3,4 ์ค‘ 2๊ฐœ๋ฅผ ๊ณ ๋ฅผ๋•Œ [0,3] ๊ณผ [3,0]์€ ๊ฐ™์€ ์‚ฌ๋žŒ์„ ๋ฝ‘๋Š” ๊ฑฐ๋ž‘ ๊ฐ™๊ธฐ ๋•Œ๋ฌธ์— ๋ฌด์กฐ๊ฑด ์ž๊ธฐ ์ž์‹ ๋ณด๋‹ค ๋†’์€ ์ˆซ์ž์—์„œ๋งŒ ์กฐํ•ฉ์„ ๊ตฌํ•˜๊ฒŒ ์ˆ˜์ •ํ•ด์ฃผ์—ˆ๋‹ค.

+) ๋ฌธ์ œ๋Š” ๊ธˆ๋ฐฉ ํ’€ ์ˆ˜ ์žˆ์—ˆ์ง€๋งŒ ์‹œ๊ฐ„์ดˆ๊ณผ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ๋ฌด์ฒ™์ด๋‚˜ ํž˜๋“ค์—ˆ๋‹ค ใ… 

์‹œ๊ฐ„ ์ค„์ด๊ธฐ ์œ„ํ•ด ํ–ˆ๋˜ ์ผ 

1. sys.stdin.readline์œผ๋กœ ์ž…๋ ฅ๋ฐ›๊ธฐ

2. n//2 ๋ฅผ ๋ณ€์ˆ˜๋กœ ์ €์žฅํ•ด์„œ ์‚ฌ์šฉํ•˜๊ธฐ

3. set() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ link_team ๊ตฌํ•˜๊ธฐ

4. start ํ•ฉ๊ณ„์™€ link ํ•ฉ๊ณ„ ๊ตฌํ• ๋•Œ ์ด์ค‘ for๋ฌธ ์ตœ๋Œ€ํ•œ ์ค„์ด๊ธฐ

import sys
import time


def backtracking(idx, start):
    global min_value
    if idx == number:
        sum_value = 0
        diff_value = 0
        link_team = list((total- set(start_team)))
        for i in range(number):
            for j in range(i+1, number):
                sum_value += power[start_team[i]][start_team[j]] + power[start_team[j]][start_team[i]]
                diff_value += power[link_team[i]][link_team[j]] + power[link_team[j]][link_team[i]]

        min_value = min(min_value, abs(sum_value-diff_value))
        return

    for i in range(start, n):
        if check[i]:
            continue
        check[i] = True
        start_team[idx] = i
        backtracking(idx+1, i+1)
        check[i] = False

if __name__ == '__main__':
    start = time.time()  # ์‹œ์ž‘ ์‹œ๊ฐ„ ์ €์žฅ

    n = int(sys.stdin.readline())
    number = n//2
    power = []
    total = set(range(0,n))
    for i in range(n):
        power.append(list(map(int,sys.stdin.readline().split())))
    min_value = float('inf')
    start_team = [0] * number
    check = [False] * n
    backtracking(0,0)
    print(min_value)
    print("time :", time.time() - start)  # ํ˜„์žฌ์‹œ๊ฐ - ์‹œ์ž‘์‹œ๊ฐ„ = ์‹คํ–‰ ์‹œ๊ฐ„

 

 

 

 

๋ฐ˜์‘ํ˜•