-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10971.cpp
61 lines (56 loc) · 946 Bytes
/
10971.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
60
61
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int n;
int w[11][11];
bool b[11];
int m = 10000001;
typedef pair<int, int> P;
void dfs(int x, vector<P> u) {
if (u.size() == n) {
int s = 0;
bool a[11];
for (int i = 0; i < n; i++) {
a[u[i].first] = true;
s += w[u[i].first][u[i].second];
}
for (int i = 1; i <= n; i++)
if (!a[i])
return;
m = min(m, s);
}
else {
for (int i = 1; i <= n; i++) {
if (i == x)
continue;
if (b[i])
continue;
if (!w[x][i])
continue;
vector<P> v = u;
v.push_back({ x,i });
b[i] = true;
dfs(i, v);
b[i] = false;
}
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++)
scanf("%d", &w[i][j]);
}
for (int i = 2; i <= n; i++) {
if (!w[1][i])
continue;
vector<P> v;
v.push_back({ 1,i });
b[i] = true;
dfs(i, v);
b[i] = false;
}
printf("%d", m);
return 0;
}