苹果 简单的 01 背包

最简单的背包  直接上代码吧  .

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<math.h>
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<queue>
 7 #include<vector>
 8 #include<set>
 9 #include<stack>
10 #include<string>
11 #include<sstream>
12 #include<map>
13 #include<cctype>
14 #include<limits.h>
15 using namespace std;
16 #define LEN 1005
17 int c[LEN],w[LEN],dp[LEN];
18 int main()
19 {
20     int n,V;
21     while(scanf("%d%d",&n,&V)&&!(n==V&&n==0))
22     {
23         for(int i=0;i<n;i++)
24             scanf("%d%d",&c[i],&w[i]);
25         memset(dp,0,sizeof(dp));
26         for(int i=0;i<n;i++)    //一个个的 苹果
27         {
28             for(int j=V;j>=0;j--)
29             {
30                 if(j<c[i])
31                     continue;
32                 dp[j]=dp[j]>dp[j-c[i]]+w[i]?dp[j]:dp[j-c[i]]+w[i];
33             }
34         }
35         printf("%d\n",dp[V]);
36     }
37     return 0;
38 }
时间: 2024-08-17 19:03:51

苹果 简单的 01 背包的相关文章

hdu 1864 最大报销额 模型为简单的01背包

很简单的01背包,但是题目还是很蛋疼的注意题目中600,和1000这两个数,还有就是double和int的转换,1A了感觉还是不错的 /************************************************************************* > File Name: hdu1864.cpp > Author: yang > Mail:[email protected] > Created Time: 2014年08月23日 星期六 14:0

HDU 2602 (简单的01背包) Bone Collector

很标准的01背包问题 1 //#define LOCAL 2 #include <algorithm> 3 #include <cstdio> 4 #include <cstring> 5 using namespace std; 6 7 const int maxn = 1000 + 10; 8 int w[maxn], v[maxn], dp[maxn]; 9 10 int main(void) 11 { 12 #ifdef LOCAL 13 freopen(&qu

饭卡-HDU2546(01背包)

http://acm.hdu.edu.cn/showproblem.php?pid=2546 饭卡 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 17947    Accepted Submission(s): 6258 Problem Description 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个

Charm Bracelet-POJ3624(01背包)

http://poj.org/problem?id=3624 Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29444   Accepted: 13198 Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it wi

HDU -1864最大报销额(01背包)

这道题属于简单的01背包,但是背包问题还算简单,就是前面的细节处理的时候要注意,题意大致说了三条限制吧 1. 只有a, b, c 三种类型的发票可以报销,其它的一律不报销 2. 物品单项的报销额不超过600 3. 每个发票总额不超过1000 有了这三个,还有一个要小心的就是报销额可以为浮点数,所以这里有个小技巧,就是将它乘100,到最后再除以100, 为什么要乘100呢, 因为最后要求保留两位小数 代码如下: 1 #include <iostream> 2 #include <cstdi

0-1背包打印路径(递归和非递归版本)

简单的0-1背包打印路径问题,我们可以记录一个p[][]数组来判断,当前物品是否被选中,最后按照记录输出,注意是逆序. #include<stdio.h> #include<string.h> int main() { int a[25],p[25][10005],i,j,n,m,s[10005]; while(scanf("%d%d",&m,&n)!=EOF){ for(i=1;i<=n;i++) scanf("%d"

hdu 3466 Proud Merchants(有排序的01背包)

Proud Merchants Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 4039    Accepted Submission(s): 1677 Problem Description Recently, iSea went to an ancient country. For such a long time, it was

hdu1203--D - I NEED A OFFER!(转化01背包)

D - I NEED A OFFER! Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description Speakless很早就想出国,现在他已经考完了所有需要的考试,准备了所有要准备的材料,于是,便需要去申请学校了.要申请国外的任何大学,你都要交纳一定的申请费用,这可是很惊人的.Speakless没有多少钱,总共只攒了n万美元.他将在m个学校中选择若干的

51 Nod 1007 正整数分组【类01背包】

1007 正整数分组 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 将一堆正整数分为2组,要求2组的和相差最小. 例如:1 2 3 4 5,将1 2 4分为1组,3 5分为1组,两组和相差1,是所有方案中相差最少的. Input 第1行:一个数N,N为正整数的数量. 第2 - N+1行,N个正整数. (N <= 100, 所有正整数的和 <= 10000) Output 输出这个最小差 Input示例 5 1 2 3 4 5 Output示例 1 题目链接