forked from lshtar13/PS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1311.cc
63 lines (53 loc) · 1.36 KB
/
1311.cc
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
55
56
57
58
59
60
61
62
63
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(void)
{
// ios::sync_with_stdio(false);
// cin.tie(NULL);
int n;
scanf("%d", &n);
int d[20][20];
for(int i = 0; i<n; ++i)
{
for(int l = 0; l<n; ++l)
{
scanf("%d", &d[i][l]);
}
}
const int MAX_COST = 20 * 10000 + 1;
int cases = 1<<n;
int cost[1<<20] = {0}, status[1<<20] = {0};
for(int i = 1; i<cases; ++i)
{
cost[i] = MAX_COST;
for(int toRemove = 0; toRemove<n; ++toRemove)
{
if(!(i&(1<<toRemove)))
{
continue;
}
int tgt = i & (~(1<<toRemove)), tgtStatus = status[tgt],
minTgtCost = MAX_COST, minTgtStatus;
for(int job = 0; job<n; ++job)
{
if((tgtStatus&(1<<job)))
{
continue;
}
if(minTgtCost > d[toRemove][job])
{
minTgtCost = d[toRemove][job];
minTgtStatus = tgtStatus|(1<<job);
}
}
if(cost[tgt] + minTgtCost < cost[i])
{
cost[i] = cost[tgt] + minTgtCost;
status[i] = minTgtStatus;
}
}
}
printf("%d", cost[cases-1]);
return 0;
}