-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_1311.cpp
More file actions
54 lines (46 loc) · 969 Bytes
/
BOJ_1311.cpp
File metadata and controls
54 lines (46 loc) · 969 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
48
49
50
51
52
53
54
//21 09 30
#include <iostream>
using namespace std;
int dp[20][(1<<20)-1];
int edge[20][20];
int n;
int DFS(int num, int state)
{
if (num==0)
{
for (int i=0; i<n; i++)
{
if (state&(1<<i))
{
dp[num][state] = edge[num][i];
return dp[num][state];
}
}
}
if (dp[num][state])
return dp[num][state];
int x = 987654321;
for (int i=0; i<n; i++)
{
if (!(state &(1<<i)))
continue;
int temp_state = state&(~(1<<i));
if (DFS(num-1, temp_state) + edge[num][i] < x)
x = dp[num-1][temp_state] + edge[num][i];
}
dp[num][state] = x;
return dp[num][state];
}
int main(void)
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n;
for (int i=0; i<n; i++)
{
for (int j=0; j<n; j++)
cin >> edge[i][j];
}
cout << DFS(n-1, (1<<n)-1);
return 0;
}