-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10971.java
More file actions
52 lines (46 loc) Β· 1.59 KB
/
10971.java
File metadata and controls
52 lines (46 loc) Β· 1.59 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
import java.io.*;
import java.util.*;
public class Main{
public static int N;
public static int[][] arr;
public static boolean[] visited;
public static int mn;
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
arr = new int[N][N];
visited = new boolean[N];
mn = Integer.MAX_VALUE;
for(int i=0;i<N;i++){
arr[i] = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
}
// κ° λμλ₯Ό μμμ μΌλ‘ ν΄μ λ€ λλ € λ΄μΌν¨
for(int k =0;k<N;k++){
dfs(k,k,0,0);
}
System.out.println(mn);
}
public static void dfs(int start, int d, int depth, int sum){
// λμ μ λ§νΌ λμκ³ μμλμλ λλμλ κ°μλλ§ κ°μΌ λ°κΏμ€
if(depth == N && start == d){
if(sum<mn){
mn = sum;
}
return;
}
for(int i = 0;i<N;i++){
// λμκ°μ μ°κ²° λκ²»μλ
if (arr[d][i]==0) {continue;}
if(!visited[i]){
visited[i] = true;
sum += arr[d][i];
// κ΅³μ΄ μ΄λ―Έ sumκ°μ΄ μ΅μκ°λ³΄λ€ ν°λ° μ¬κ·μ λ€μ΄κ° νμκ° μμΌλ back tracking
if (sum <= mn){
dfs(start, i, depth + 1, sum);
}
visited[i] = false;
sum -= arr[d][i];
}
}
}
}