DP using bitfield
외판원 순회 1과 똑같은 문제이다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
import sys
from math import sqrt
input = sys.stdin.readline
INF = 12345678
dp = [[-1 for _ in range(1<<16)] for _ in range(16)]
a = [[0 for _ in range(16)] for _ in range(16)]
b = []
def getDist(a1,a2):
return sqrt((a1[0] - a2[0])*(a1[0] - a2[0]) + (a1[1] - a2[1])*(a1[1] - a2[1]))
def f(now, bit):
if bit == (1<<n)-1:
if a[now][0]: return a[now][0]
return INF
if dp[now][bit] != -1: return dp[now][bit]
dp[now][bit] = INF
for i in range(n):
if bit&(1<<i)==0 and a[now][i] != 0:
dp[now][bit] = min(dp[now][bit], f(i, bit|(1<<i)) + a[now][i])
return dp[now][bit]
n = int(input())
for i in range(n):
b.append(list(map(int, input().split())))
for i in range(n):
for j in range(n):
if i==j: continue
a[i][j] = getDist(b[i], b[j])
print(f(0,1))
|
cs |
'백준 문제풀이' 카테고리의 다른 글
백준 1994 - 등차수열 [Python / C++] (0) | 2021.04.28 |
---|---|
백준 3687 - 성냥개비 [Python] (0) | 2021.04.28 |
백준 1306 - 달려라 홍준 [Python] (0) | 2021.04.08 |
백준 17291 - 새끼치기 [Python] (0) | 2021.04.08 |
백준 1321 - 군인 [Python] (0) | 2021.04.08 |