-
Notifications
You must be signed in to change notification settings - Fork 1
/
14889.cpp
53 lines (46 loc) · 993 Bytes
/
14889.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
#include <iostream>
#include <vector>
#include <cmath>
#include <climits>
#include <algorithm>
using namespace std;
int n;
vector<vector<int> > s;
vector<bool> visited;
int backtrack(int idx, int cnt) {
if (idx == n/2) {
int start = 0, link = 0;
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
if (visited[i] && visited[j]) start += s[i][j];
if (!visited[i] && !visited[j]) link += s[i][j];
}
}
return abs(start-link);
}
int ret = INT_MAX;
for (int i=cnt; i<n; i++) {
if (visited[i]) continue;
visited[i] = true;
ret = min(ret, backtrack(idx+1, i+1));
visited[i] = false;
}
return ret;
}
void solve(void) {
cin >> n;
s.resize(n, vector<int>(n));
visited.resize(n, false);
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
cin >> s[i][j];
}
}
cout << backtrack(0, 0);
}
int main(void) {
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
solve();
return 0;
}