-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path백준-스타트와 링크.cpp.txt
117 lines (103 loc) · 1.63 KB
/
백준-스타트와 링크.cpp.txt
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// ConsoleApplication3.cpp: 콘솔 응용 프로그램의 진입점을 정의합니다.
//
#include<iostream>
#include<string.h>
#include<string>
#include<queue>
#include<utility>
#include<math.h>
using namespace std;
int N;
int arr[21][21];
bool check[11];
int start_t[11];
int link_t[11];
int num[21];
int result = 1000000001;
void go(int x, int depth)
{
if (depth == N/2)
{
for (int i = 1; i <= N; i++)
{
num[i] = i;
}
int start_num = 0;
int link_num = 0;
for (int i = 1; i <= N / 2; i++)
{
for (int j = i + 1; j <= N / 2; j++)
{
int s = start_t[i];
int k = start_t[j];
for (int i = 1; i <= N; i++)
{
if (num[i] == s || num[i] == k)
{
num[i] = 0;
}
}
start_num += arr[s][k] + arr[k][s];
}
}
int t = 1;
for (int i = 1; i <= N; i++)
{
if (t > N / 2)
{
break;
}
if (num[i] != 0)
{
link_t[t] = num[i];
t++;
}
}
for (int i = 1; i <= N / 2; i++)
{
for (int j = i + 1; j <= N / 2; j++)
{
int s = link_t[i];
int k = link_t[j];
link_num += arr[s][k] + arr[k][s];
}
}
int re = abs(link_num - start_num);
if (re < result)
result = re;
return;
}
for (int i = 1; x+i <= N; i++)
{
if (check[x + i] == false)
{
check[x + i] = true;
start_t[depth+1] = x + i;
go(x + i, depth + 1);
check[x + i] = false;
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin >> N;
for (int i = 1; i <= N; i++)
{
num[i] = i;
}
for (int i = 1; i <= N; i++)
{
for (int j = 1; j <= N; j++)
{
cin >> arr[i][j];
}
}
for (int i = 0; i <= N; i++)
{
check[i] = false;
}
go(0,0);
cout << result << endl;
return 0;
}