-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path_14889.java
More file actions
90 lines (82 loc) ยท 2.2 KB
/
_14889.java
File metadata and controls
90 lines (82 loc) ยท 2.2 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
88
89
90
package backjoon;
// https://www.acmicpc.net/problem/14889
// ์คํํธ์ ๋งํฌ
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class _14889 {
public static int N; // ์ฃผ์ด์ง ์ซ์ ๊ฐ์
public static int[][] skill; // ๋ฅ๋ ฅ์น
static boolean[] visit;
static int Min = Integer.MAX_VALUE;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// memory 15696 runtime 324
N = Integer.parseInt(br.readLine());
skill = new int[N][N];
visit = new boolean[N];
// ์ซ์๋ค ๋ฐฐ์ด์ ๋ฃ๊ธฐ
for (int i = 0; i < N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
for(int j=0; j<N; j++){
skill[i][j] = Integer.parseInt(st.nextToken());
}
}
combination(0, 0);
System.out.println(Min);
}
static void combination(int idx, int cnt){
// ํ ์กฐํฉ์ด ์์ฑ๋ ๊ฒฝ์ฐ
if(cnt == N/2){
diff();
return;
}
for(int i=idx; i<N; i++){
// ๋ฐฉ๋ฌธํ์ง์์ ๊ฒฝ์ฐ
if(!visit[i]){
visit[i] = true;
combination(i+1, cnt+1); // ์ฌ๊ท
visit[i] = false;
}
}
}
// ๋ ํ์ ๋ฅ๋ ฅ์น ์ฐจ์ด๋ฅผ ๊ณ์ฐํ๋ ํจ์
static void diff(){
int team_start = 0;
int team_link = 0;
for(int i=0; i<N-1; i++){
for(int j=i+1; j<N; j++){
if(visit[i] == true && visit[j] == true){
team_start += skill[i][j];
team_start += skill[j][i];
}
// i ๋ฒ์งธ ์ฌ๋๊ณผ j ๋ฒ์งธ ์ฌ๋์ด false๋ผ๋ฉด ๋งํฌํ์ผ๋ก ์ ์ ํ๋ฌ์ค
else if (visit[i] == false && visit[j] == false) {
team_link += skill[i][j];
team_link += skill[j][i];
}
}
}
// ๋ ํ์ ์ ์ ์ฐจ์ด (์ ๋๊ฐ)
int val = Math.abs(team_start - team_link);
// ๋ ํ์ ์ฐจ์ด๊ฐ 0์ด๋ผ๋ฉด ๊ฐ๋ญ ๋ฎ์ ์ต์๊ฐ์ด๊ธฐ๋๋ฌธ์ ์ข
๋ฃ
if (val == 0) {
System.out.println(val);
System.exit(0);
}
Min = Math.min(val, Min);
}
}
/*
input
6
0 1 2 3 4 5
1 0 2 3 4 5
1 2 0 3 4 5
1 2 3 0 4 5
1 2 3 4 0 5
1 2 3 4 5 0
output
2
*/