-
Notifications
You must be signed in to change notification settings - Fork 1
/
LEM3.cpp
59 lines (52 loc) · 1.34 KB
/
LEM3.cpp
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
#include <bits/stdc++.h>
#define REP(i, a, b) for(int i = (a), _b = (b); i < _b; ++i)
#define FOR(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for(int i = (a), _b = (b); i >= _b; --i)
#define FILL(a, v, n) for(int i = 0, _n = (n); i < _n; ++i) a[i] = v
#define pr(x) cout << #x << " = " << x << endl
#define pra(a, n) REP(i, 0, n) cout << #a << "[" << i << "] = " << a[i] << " "; cout << "\n"
#define sz(x) (int)(x.size())
#define endl "\n"
#define newline cout << "\n"
using namespace std;
const int N = 20;
const int INF = 1e9;
int dp[1<<17][N];
int cty[N], cost[N][N];
int n;
int main() {
ios::sync_with_stdio(false); cin.tie(0);
#ifndef Home
freopen ("input.txt", "r", stdin);
#endif
cin >> n;
FOR(i, 1, n) {
FOR(j, 1, n) {
cin >> cost[i][j];
}
}
#define getBit(num, ith) ((num >> (ith)) & 1)
#define toggleBit(num, ith) (num ^ (1 << (ith)))
FOR(cur, 1, 1<<n) {
int k = 0;
FOR(i, 1, n) {
if(getBit(cur, i-1)) cty[++k] = i;
}
if(k == 1) continue;
FOR(i, 1, k) {
int pre = toggleBit(cur, cty[i]-1);
dp[cur][cty[i]] = INF;
FOR(j, 1, k) {
if(j == i) continue;
dp[cur][cty[i]] = min(dp[cur][cty[i]], dp[pre][cty[j]] + cost[cty[i]][cty[j]]);
}
}
}
int ans = INF;
int all = (1<<n)-1;
FOR(i, 1, n) {
ans = min(ans, dp[all][i]);
}
cout << ans << endl;
return newline, 0;
}