-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
40 lines (40 loc) · 988 Bytes
/
Copy pathmain.cpp
File metadata and controls
40 lines (40 loc) · 988 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
#include <iostream>
#define MAX_SIZE 11
#define INF 987654321
using namespace std;
int N, ans;
int map[MAX_SIZE][MAX_SIZE];
bool check[MAX_SIZE];
void tsp(int start, int current, int sum, int count) {
if(start == current && count == N) {
if(ans > sum) ans = sum;
return;
}
for(int next=1; next<=N; next++) {
if(map[current][next] == 0) continue;
if(check[next] == false) {
check[next] = true;
sum += map[current][next];
if(sum <= ans)
tsp(start, next, sum, count+1);
check[next] = false;
sum -= map[current][next];
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N;
for(int i=1; i<=N; i++) {
for(int j=1; j<=N; j++) {
cin >> map[i][j];
}
}
ans = INF;
for(int start = 1; start <= N; start++) {
tsp(start, start, 0, 0);
}
cout << ans << '\n';
return 0;
}