zoukankan      html  css  js  c++  java
  • SOJ 4580 动态规划之01背包 (01背包)

    Description

      Sidney想去Gandtom家玩。但Sidney家和Gandtom家之间是高低不平、坑坑洼洼的土路。所以他需要用他的背包装几袋稀的泥,在路上铺平一些干的土,使路变成平整的泥土,才能到Gandtom家见到Gandtom。 
      已知现在有种稀的泥,第种稀的泥的质量为,体积为,数量为。Sidney的包能装体积不超过的稀的泥。Sidney出门时携带的稀的泥的质量应该尽可能的大。在此前提下,携带的稀的泥的体积也应该尽可能的大。 
      试求Sidney最多能携带多少质量的稀的泥与此时的最大体积上路。

     

    Input

    第一行有一个整数,表示组数。 
    每组数据第一行有两个正整数 。 
    每组数据第二行有个正整数,第个数为。 
    每组数据第三行有个正整数,第个数为

     

    Output

    每组样例第一行输出两个整数。表示Sidney最多能携带多少质量的稀的泥与此时的最大体积上路。

     

    Sample Input


    5 3 
    1 2 3 4 5 
    1 1 1 1 1 
    3 7 
    1 2 1 
    3 5 3

     

    Sample Output

    12 3 
    2 6

     

    #include <stdio.h>
    #include <iostream>
    #include <string.h>
    #include <math.h>
    #include <algorithm>
    using namespace std;
    int dp[1001],w[1001],v[1001];
    int main(){
        int T;
        scanf("%d",&T);
        while(T--){
            memset(dp,0,sizeof(dp));
            int n,V,i,j,max1=0,maxpos=0;
            scanf("%d%d",&n,&V);
            for(i=1;i<=n;i++)
              scanf("%d",&w[i]);
            for(i=1;i<=n;i++)
              scanf("%d",&v[i]);
            dp[v[1]]=w[1];
            for(i=2;i<=n;i++)
              for(j=V;j>=v[i];j--)
                if(j==v[i]||dp[j-v[i]])
                  dp[j]=max(dp[j],dp[j-v[i]]+w[i]);
            for(i=V;i>0;i--)
              if(dp[i]>max1){
                  max1=dp[i];
                  maxpos=i;
              }
           printf("%d %d
    ",max1,maxpos);
        }
        return 0;
    }
  • 相关阅读:
    hdu4841 圆桌问题[STL vector]
    hdu1002 A + B Problem II[大数加法]
    hdu1501 Zipper[简单DP]
    C语言学习之结构体
    C++学习之从C到C++
    一键自动格式化你的代码
    C标准库string.h中几个常用函数的使用详解
    Jlink使用技巧系列教程索引
    Jlink使用技巧之合并烧写文件
    Jlink使用技巧之烧写SPI Flash存储芯片
  • 原文地址:https://www.cnblogs.com/87hbteo/p/8946578.html
Copyright © 2011-2022 走看看