算法_动态规划_整数线性规划问题

算法_动态规划_整数线性规划问题_第1张图片

import java.util.Scanner;

public class Main {
    private static int n;
    private static int c;
    private static int[] weightArr;
    private static int[] valueArr;
    private static int[][] dp;

    /** * @param args */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc=new Scanner(System.in);
        n=sc.nextInt();
        c=sc.nextInt();
        weightArr=new int[n+1];
        valueArr=new int[n+1];
        dp=new int[n+1][c+1];
        for (int i = 1; i <= n; i++) {
            weightArr[i]=sc.nextInt();
        }
        for (int i = 1; i <= n; i++) {
            valueArr[i]=sc.nextInt();
        }

        for(int i=1;i<=n;i++){
            for(int j=1;j<=c;j++){
                if(j>=weightArr[i]){
                    dp[i][j]=Math.max(dp[i-1][j],dp[i][j-weightArr[i]]+valueArr[i]);
                }else{
                    dp[i][j]=dp[i-1][j];
                }
            }
        }

        System.out.println(dp[n][c]);
    }
}

你可能感兴趣的:(算法,dp,动态规划)