본문 바로가기

백준/DP

백준 1311 - 할 일 정하기 1

728x90

www.acmicpc.net/problem/1311

 

어렵지 않은 비트마스크 dp이다.

 

다음 dp테이블을 정의하고 탑 다운 dp로 풀면 된다.

 

dp[i] = 비트마스크 i의 조합으로 일을 할당했을 때 비용의 최솟값

 

전체 코드

더보기
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
38
39
40
41
42
43
44
45
46
#include <bits/stdc++.h>
 
using namespace std;
 
int n;
int ar[20][20];
int dp[1 << 20];
 
int memo(int bt, int idx);
 
int main()
{
    cin.tie(0); ios::sync_with_stdio(false);
    cin >> n;
 
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j)
            cin >> ar[i][j];
    
    memset(dp, -1sizeof dp);
    int res = 1e9;
 
    for (int i = 0; i < n; ++i)
        res = min(res, ar[0][i] + memo(1 << i, 1));
 
    cout << res;
}
 
int memo(int bt, int idx)
{
    if (idx == n)
        return 0;
    
    int &ret = dp[bt];
 
    if (ret != -1)
        return ret;
    
    ret = 1e9;
 
    for (int i = 0; i < n; ++i)
        if (!(bt & (1 << i)))
            ret = min(ret, ar[idx][i] + memo(bt | (1 << i), idx + 1));
 
    return ret;
}
cs
728x90

'백준 > DP' 카테고리의 다른 글

백준 2315 - 가로등 끄기  (0) 2021.03.12
[KOI] 백준 10835 - 카드게임  (0) 2021.02.10
[USACO] 백준 14165 - Team Building  (0) 2021.01.31
[KOI] 백준 1866 - 택배  (0) 2021.01.28
백준 15678 - 연세워터파크  (0) 2021.01.26