HDU-2159 二維完全揹包

數組dp[j][l]記錄經驗值,j表示忍耐值,l表示殺怪的個數,狀態轉移方程dp[j][l] = Math.max(dp[j][l],dp[j-b[i]][l-1]+a[i])

import java.util.Arrays;
import java.util.Scanner;

public class Main_HDU2159 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n,m,k,s;
		int[] a = new int[100];
		int[] b = new int[100];
		int[][] dp = new int[100][100];
		while(sc.hasNext()) {
			n = sc.nextInt();
			m = sc.nextInt();
			k = sc.nextInt();
			s = sc.nextInt();
			for(int i=1;i<=k;i++) {
				a[i] = sc.nextInt();
				b[i] = sc.nextInt();
			}
			int res = 99999999;
			for(int j=0;j<=m;j++)
				Arrays.fill(dp[j], 0);
			for(int i=1;i<=k;i++) {
				A:for(int j=b[i];j<=m;j++) {
					for(int l=1;l<=s;l++) {
						dp[j][l] = Math.max(dp[j][l],dp[j-b[i]][l-1]+a[i]);
						if(dp[j][l]>=n) {   //說明已經滿足了所需要的經驗值
							res = Math.min(j, res);   //記錄殺得怪所用最小的忍耐值
							break A;
						}
					}
				}
			}
			if(res==99999999)
				System.out.println(-1);
			else
				System.out.println(m-res);
		}
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章