• hdu 2602 (01背包)


    Bone Collector

    Problem Description
    Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave …
    The bone collector had a big bag with a volume of V ,and along his trip of collecting there are a lot of bones , obviously , different bone has different value and different volume, now given the each bone’s value along his trip , can you calculate out the maximum of the total value the bone collector can get ?
     
    Input
    The first line contain a integer T , the number of cases.
    Followed by T cases , each case three lines , the first line contain two integer N , V, (N <= 1000 , V <= 1000 )representing the number of bones and the volume of his bag. And the second line contain N integers representing the value of each bone. The third line contain N integers representing the volume of each bone.
     
    Output
    One integer per line representing the maximum of the total value (this number will be less than 231).
     
    Sample Input
     
    1
    5 10
    1 2 3 4 5
    5 4 3 2 1
     
    Sample Output
     
    14

     

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<algorithm>
     6 #include<map>
     7 #include<vector>
     8 #include<set>
     9 using namespace std;
    10 #define ll long long 
    11 const int mod=1e9+7;
    12 const int inf=1e9+7;
    13 const int maxn=1005;
    14 int dp[maxn][maxn];
    15 int value[maxn];
    16 int capacity[maxn];
    17 int main()
    18 {
    19     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    20     int T;
    21     cin>>T;
    22     while(T--)
    23     {
    24         memset(dp,0,sizeof(dp));
    25         int n,v;
    26         cin>>n>>v;
    27         for(int i=1;i<=n;i++)
    28             cin>>value[i];
    29         for(int i=1;i<=n;i++)
    30             cin>>capacity[i];
    31         for(int i=1;i<=n;i++)
    32         {
    33             for(int j=0;j<=v;j++)//注意体积为0时的情况 
    34             {
    35                 if(j<capacity[i])//放不进 
    36                     dp[i][j]=dp[i-1][j];
    37                 else
    38                 {
    39                     if(dp[i-1][j]>dp[i-1][j-capacity[i]]+value[i])//放得进但是亏了 
    40                         dp[i][j]=dp[i-1][j];
    41                     else
    42                         dp[i][j]=dp[i-1][j-capacity[i]]+value[i];//不亏 
    43                 }
    44             }
    45            }
    46         cout<<dp[n][v]<<endl;  
    47     }
    48     return 0;
    49 }

    一维优化:

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<algorithm>
     6 #include<map>
     7 #include<vector>
     8 #include<set>
     9 using namespace std;
    10 #define ll long long 
    11 const int mod=1e9+7;
    12 const int inf=1e9+7;
    13 const int maxn=1005;
    14 int dp[maxn];
    15 int value[maxn];
    16 int capacity[maxn];
    17 int main()
    18 {
    19     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    20     int T;
    21     cin>>T;
    22     while(T--)
    23     {
    24         memset(dp,0,sizeof(dp));
    25         int n,v;
    26         cin>>n>>v;
    27         for(int i=1;i<=n;i++)
    28             cin>>value[i];
    29         for(int i=1;i<=n;i++)
    30             cin>>capacity[i];
    31         for(int i=1;i<=n;i++)
    32         {
    33             for(int j=v;j>=capacity[i];j--)
    34             {
    35                 //if(j>=capacity[i])//由j的循环条件知,j一定大于等于capacity[i] 
    36                 dp[j]=max(dp[j],dp[j-capacity[i]]+value[i]);
    37             }
    38         }
    39         cout<<dp[v]<<endl;  
    40     }
    41     return 0;
    42 }
    大佬见笑,,
  • 相关阅读:
    【高级内部资料】.NET数据批量写入性能分析 第二篇
    负载均衡原理与实践详解 第五篇 负载均衡时数据包流程详解
    负载均衡原理与实践详解 第三篇 服务器负载均衡的基本概念网络基础
    如何提高Linq查询的性能(上)
    【全面解析DeepZoom 之二】Silverlight2及Deep Zoom环境的搭建
    关于让WPF软件界面支持全球化和本地化
    在WPF中自定义控件(3) CustomControl (上)
    【全面解析DeepZoom 之一】酷!Deep Zoom
    谈谈我理解的WPF团队模型——在UI Designer与Developer之间
    [WPF疑难]在WPF中显示动态GIF
  • 原文地址:https://www.cnblogs.com/xwl3109377858/p/10933081.html
Copyright © 2020-2023  润新知