-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.java
More file actions
86 lines (75 loc) · 1.63 KB
/
test.java
File metadata and controls
86 lines (75 loc) · 1.63 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
import java.io.FileInputStream;
import java.util.Scanner;
public class test {
static int T;
static int N;
static int M;
static int[] src;
static int[] dst;
static int minCost;
static int[] visited;
static int[][] map;
static int AnswerN;
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/data/ex00"));
Scanner sc = new Scanner(System.in);
T = sc.nextInt();
for(int test_case = 1; test_case <= T; test_case ++) {
M = sc.nextInt();
N = sc.nextInt();
visited = new int[N+1];
AnswerN = Integer.MAX_VALUE;
minCost = 0;
map = new int[M][2];
for(int i = 0; i < M; i++) {
map[i][0] = sc.nextInt();
map[i][1] = sc.nextInt();
}
// src = new int[M];
// dst = new int[M];
visited[1] = 1;
minCost++;
move(1);
System.out.println(AnswerN);
}
}
//dfs????? 백트래킹?
public static void move(int idx) {
int cnt = 0;
if(minCost >= AnswerN){
return;
}
for(int i = 1; i <= N; i++) {
if(visited[i] == 1){
cnt++;
} else {
break;
}
}
if(cnt == N) {
if(AnswerN > minCost){
AnswerN = minCost;
}
return;
} else {
for(int i = 1; i <= N; i++) {
if(visited[i] == 0) {
int cost = 1;
// for(int j = 1; j <= N; j++){
for(int j = 0; j < M; j++){
// if(map[idx][i] == 1 ) {
if(map[j][0] == idx && map[j][1] == i){
cost = 0;
break;
}
}
visited[i] = 1;
minCost += cost;
move(i);
minCost -= cost;
visited[i] = 0;
}
}
}
}
}