[BOJ][๐ŸŸก5][๋ฐฑ์ค€#15661] ๋งํฌ์™€ ์Šคํƒ€ํŠธ

์ž‘์„ฑ:    

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #15661


๋ฌธ์ œ

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

i\j 1 2 3 4

1 ย  1 2 3

2 4 ย  5 6

3 7 1 ย  2

4 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๊ฐœ์˜ ์ค„์— S๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ์ค„์€ N๊ฐœ์˜ ์ˆ˜๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๊ณ , i๋ฒˆ ์ค„์˜ j๋ฒˆ์งธ ์ˆ˜๋Š” Sij ์ด๋‹ค. Sii๋Š” ํ•ญ์ƒ 0์ด๊ณ , ๋‚˜๋จธ์ง€ Sij๋Š” 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 100๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ •์ˆ˜์ด๋‹ค.


์ถœ๋ ฅ

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


์˜ˆ์ œ

์˜ˆ์ œ 1

์ž…๋ ฅ

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


์ถœ๋ ฅ

0


์˜ˆ์ œ 2

์ž…๋ ฅ

6
0 1 2 3 4 5
1 0 2 3 4 5
1 2 0 3 4 5
1 2 3 0 4 5
1 2 3 4 0 5
1 2 3 4 5 0


์ถœ๋ ฅ

2


์˜ˆ์ œ 3

์ž…๋ ฅ

8
0 5 4 5 4 5 4 5
4 0 5 1 2 3 4 5
9 8 0 1 2 3 1 2
9 9 9 0 9 9 9 9
1 1 1 1 0 1 1 1
8 7 6 5 4 0 3 2
9 1 9 1 9 1 0 9
6 5 4 3 2 1 9 0


์ถœ๋ ฅ

1


์˜ˆ์ œ 4

์ž…๋ ฅ

5
0 3 1 1 1
3 0 1 1 1
1 1 0 1 1
1 1 1 0 1
1 1 1 1 0


์ถœ๋ ฅ

0


My Sol

import sys
from itertools import combinations

def calc_score(S):
    global mat
    ret = 0
    for i, j in combinations(S, 2):
        ret += mat[i][j]+mat[j][i]
    return ret

ans = 1e5
N = int(input())
S = set(range(N))
mat = [list(map(int, input().split())) for _ in range(N)]
for cnt in range(1, N//2+1):
    for comb in combinations(list(range(N)), cnt):
        C = set(comb)
        a = calc_score(C)
        b = calc_score(S-C)
        ret = abs(a-b)
        if ans > ret:
            ans = ret
            if not ans: break

print(ans)

๋ฌธ์ œ ํ’€์ด์˜ ๊ด€๊ฑด์€ ๊ฐ ํŒ€์˜ ์ธ์›์ด ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ด๋‹ค. ์ตœ์†Œ 1๋ช…์˜ ํŒ€์›๋งŒ ์žˆ์œผ๋ฉด ๋œ๋‹ค.

  1. ์ž…๋ ฅ์„ ์ฒ˜๋ฆฌํ•œ๋‹ค.
  2. 1๋ถ€ํ„ฐ N//2๊นŒ์ง€์˜ ์ธ์›์ˆ˜์˜ ๋ชจ๋“  ๊ตฌ์„ฑ ๊ฐ€๋Šฅํ•œ ์กฐํ•ฉ๊ณผ ๊ทธ์— ๋ฐ˜๋Œ€๋˜๋Š” ์กฐํ•ฉ์˜ ์‹œ๋„ˆ์ง€ ์ ์ˆ˜๋ฅผ ๊ตฌํ•ด์„œ ์ฐจ์ด๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค.
  3. ์ฐจ์ด์˜ ์ตœ์†Ÿ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.


๊ฒฐ๊ณผ

๋งž์•˜์Šต๋‹ˆ๋‹ค!!

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ