-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2098.cpp
More file actions
61 lines (44 loc) · 1.16 KB
/
2098.cpp
File metadata and controls
61 lines (44 loc) · 1.16 KB
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>
#include<queue>
#include<cstring>
#include <string>
using namespace std;
int dp[17][1 << 17];
int N;
int arr[17][17];
const int INF = 123456789;
int TSP(int leng, int idx, int visited) {
if (leng == 0) {
return 0;
}
if (dp[idx][visited] != -1)return dp[idx][visited];
int val = INF;
if (leng == 1) {
//출발점으로 복귀 가능
if (arr[idx][0] != 0)val = TSP(leng - 1, 0, visited) + arr[idx][0];
}
else {
for (int i = 0; i < N; i++) {
if (i == idx)continue;
if (!(visited & (1 << i))) {
if (arr[idx][i] != 0) {
val = min(val, TSP(leng - 1, i, visited | (1 << i)) + arr[idx][i]);
}
}
}
}
return dp[idx][visited] = val;
}
int main() {
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
memset(dp, -1, sizeof(dp));
cin >> N;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cin >> arr[i][j];
}
}
cout << TSP(N, 0, 1);
}