File tree Expand file tree Collapse file tree 4 files changed +77
-0
lines changed Expand file tree Collapse file tree 4 files changed +77
-0
lines changed Original file line number Diff line number Diff line change 376
376
| 30 | | [ SWEA-1861 μ μ¬κ°ν λ°©] ( src/DP/swea1861 ) | |
377
377
| 31 | | [ SWEA-1952 μμμ₯] ( src/DP/swea1952 ) | |
378
378
| 32 | | [ JUNGOL-1681 ν΄λ°ν΄ μννλ‘] ( ./src/DP/jo1681 ) | TSP |
379
+ | 33 | | [ Baekjoon-2098 μΈνμ μν] ( ./src/DP/P2098 ) | TSP |
379
380
380
381
## Time Complexity
381
382
Original file line number Diff line number Diff line change
1
+ package DP .P2098 ;
2
+
3
+ import java .io .*;
4
+ import java .util .*;
5
+
6
+ public class Main {
7
+
8
+ static int INF = 160000000 ;
9
+ static int N ;
10
+ static int [][] W , dp ;
11
+
12
+ public static void main (String [] args ) throws Exception {
13
+ System .setIn (new FileInputStream ("src/DP/P2098/input.txt" ));
14
+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
15
+
16
+ N = Integer .parseInt (br .readLine ());
17
+ W = new int [N ][N ];
18
+ for (int i = 0 ; i < N ; i ++) {
19
+ StringTokenizer st = new StringTokenizer (br .readLine ());
20
+ for (int j = 0 ; j < N ; j ++) {
21
+ W [i ][j ] = Integer .parseInt (st .nextToken ());
22
+ }
23
+ }
24
+
25
+ dp = new int [1 <<N ][N ];
26
+ for (int i = 0 ; i < 1 << N ; i ++) Arrays .fill (dp [i ], INF );
27
+
28
+ System .out .println (TSP (1 , 0 ));
29
+ }
30
+
31
+ static int TSP (int mask , int now ) {
32
+ if (mask == (1 <<N )-1 ) {
33
+ if (W [now ][0 ] > 0 ) return W [now ][0 ];
34
+ return INF ;
35
+ }
36
+
37
+ if (dp [mask ][now ] != INF ) return dp [mask ][now ];
38
+ for (int i = 0 ; i < N ; i ++) {
39
+ if ((mask & 1 <<i ) == 0 && W [now ][i ] > 0 ) {
40
+ dp [mask ][now ] = Math .min (dp [mask ][now ], TSP (mask |1 <<i , i ) + W [now ][i ]);
41
+ }
42
+ }
43
+ return dp [mask ][now ];
44
+ }
45
+ }
Original file line number Diff line number Diff line change
1
+ ## [ baekjoon-2098] μΈνμ μν
2
+
3
+ ![ image] ( https://user-images.githubusercontent.com/22045163/112101265-b37ebb00-8be9-11eb-8302-789243835946.png )
4
+
5
+ ### μΈνμ μν λ¬Έμ
6
+
7
+ μΈνμ μν λ¬Έμ λ λΉνΈλ§μ€ν¬ + DP λ₯Ό μ΄μ©ν λ¬Έμ μ΄λ€. νμ΄ λ° μ½λλ₯Ό μ΄ν΄ν ν, μΈμ°λ κ²μ΄ μ’μ κ² κ°λ€.
8
+
9
+ ``` java
10
+ int TSP (int mask, int now) {
11
+ if (mask == (1 << N )- 1 ) {
12
+ if (W [now][0 ] > 0 ) return W [now][0 ];
13
+ return INF ;
14
+ }
15
+
16
+ if (dp[mask][now] != INF ) return dp[mask][now];
17
+ for (int i = 0 ; i < N ; i++ ) {
18
+ if ((mask & 1 << i) == 0 && W [now][i] > 0 ) {
19
+ dp[mask][now] = Math . min(dp[mask][now], TSP (mask| 1 << i, i) + W [now][i]);
20
+ }
21
+ }
22
+ return dp[mask][now];
23
+ }
24
+ ```
25
+
26
+ ![ image] ( https://user-images.githubusercontent.com/22045163/112101291-bc6f8c80-8be9-11eb-97ee-5add4fa8208f.png )
Original file line number Diff line number Diff line change
1
+ 4
2
+ 0 10 15 20
3
+ 5 0 9 10
4
+ 6 13 0 12
5
+ 8 8 9 0
You canβt perform that action at this time.
0 commit comments