-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01311_BitMask_DP.cpp
More file actions
47 lines (38 loc) · 835 Bytes
/
01311_BitMask_DP.cpp
File metadata and controls
47 lines (38 loc) · 835 Bytes
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
47
#include <iostream>
#include <algorithm>
#include <cmath>
#include <climits>
using namespace std;
const int INF = INT_MAX;
int n;
int cost[21][21];
int dp[1100000];
int count_bits(int b)
{
int cnt = 0;
for (int i = 0; i < n; i++)
if (b == (b | (1 << i))) cnt++;
return cnt;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
cin >> cost[i][j];
int max = pow(2, n);
dp[0] = 0;
for (int i = 1; i < max; i++)
dp[i] = INF;
for (int mask = 0; mask < max; mask++)
{
int x = count_bits(mask);
for (int i = 0; i < n; i++)
dp[mask|(1<<i)] = min(dp[mask|(1<<i)], dp[mask] + cost[x][i]);
}
cout << dp[max - 1];
return 0;
}