[swea-D3] 3282. Knapsack
SWEA 0/1 Knapsack 문제를 JAVA를 이용해 dp 부분집합으로 해결하는 방법을 설명합니다.
🙋♂️ 들어가며
새로운 무게를 추가하기전 이전상태를 잘 활용하자
✅ 정답 코드
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
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
// test
import java.util.Arrays;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for (int tc = 1; tc < T+1; tc++) {
String[] NK = br.readLine().split(" ");
int N = Integer.parseInt(NK[0]);
int K = Integer.parseInt(NK[1]);
int[][] arr = new int[N][2];
// 1. 배열 생성
for (int i = 0; i < N; i++) {
String[] vi_ci = br.readLine().split(" ");
int vi = Integer.parseInt(vi_ci[0]);
int ci = Integer.parseInt(vi_ci[1]);
// 1-1. 부피, 가치
arr[i][0] = vi;
arr[i][1] = ci;
}
// 2. 정렬
Arrays.sort(arr, (a,b) -> {
return Integer.compare(a[0], b[0]);
});
// 3. DP
int[] DP = new int[K+1];
for (int i = 0; i < N; i++) {
int volume = arr[i][0];
int c = arr[i][1];
// 3-1. 부분집합
for (int j = K; j >= volume; j--) {
// 3-2. j를 넣기전 이전 무게상태 -> j - volume
DP[j] = Math.max(DP[j], DP[j - volume] + c);
}
}
System.out.println("#" + tc + " " + DP[K]);
}
}
}
This post is licensed under CC BY 4.0 by the author.
