-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ10971_Traveling Salesman problem2.cpp
More file actions
87 lines (73 loc) · 1.61 KB
/
BOJ10971_Traveling Salesman problem2.cpp
File metadata and controls
87 lines (73 loc) · 1.61 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//
// main.cpp
// BOJ10971_Traveling Salesman problem2
//
// Created by 신지식 on 2018. 9. 24..
// Copyright © 2018년 Shin Ji Sik. All rights reserved.
//
#include <iostream>
#include <algorithm>
using namespace std;
/*
int city;
int arr[11][11];
int visited[11];
int ans;
void dfs(int now, int next, int cost, int visit){
if(now == next && visit == city){
ans = min(ans, cost);
return;
}
for(int i = 0; i < city; i++){
if(arr[next][i] == 0) continue;
if(cost + arr[next][i] > ans) continue;
if(!visited[i]){
visited[i] = 1;
dfs(now, i, cost + arr[next][i], visit+1);
visited[i] = 0;
}
}
}
int main(){
cin >> city;
for(int i = 0; i < city; i++)
for(int j = 0; j < city; j++)
cin >> arr[i][j];
ans = 987654321;
for(int i = 0; i < city; i++)
dfs(i, i, 0, 0);
printf("%d\n", ans);
}
*/
int n;
int arr[11][11];
bool visit[11];
int ans;
int start;
void dfs(int now, int cost, int cnt){
if(visit[now] == false){
visit[now] = true;
cnt++;
for(int i = 0; i < n; i++){
if(arr[now][i] != 0){
dfs(i, arr[now][i] + cost, cnt);
}
}
visit[now] = false;
}
if(cnt == n && start == now){
ans = min(ans, cost);
}
}
int main(){
cin >> n;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
cin >> arr[i][j];
ans = 987654321;
for(int i = 0; i < n; i++){
start = i;
dfs(i, 0, 0);
}
printf("%d\n", ans);
}