poj3624 01背包

来源:互联网 发布:人工智能平台有哪些 编辑:程序博客网 时间:2024/06/05 06:54

Charm Bracelet

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 23   Accepted Submission(s) : 9
Problem Description

Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the best charms possible from theN (1 ≤N ≤ 3,402) available charms. Each charmi in the supplied list has a weightWi (1 ≤Wi ≤ 400), a 'desirability' factorDi (1 ≤Di ≤ 100), and can be used at most once. Bessie can only support a charm bracelet whose weight is no more thanM (1 ≤M ≤ 12,880).

Given that weight limit as a constraint and a list of the charms with their weights and desirability rating, deduce the maximum possible sum of ratings.

 

Input

* Line 1: Two space-separated integers: N and M
* Lines 2..N+1: Line i+1 describes charm i with two space-separated integers:Wi andDi

 

Output

* Line 1: A single integer that is the greatest sum of charm desirabilities that can be achieved given the weight constraints

 

Sample Input
4 61 42 63 122 7
 

Sample Output
23
 怎么说呢应该说自己以前没做过这种01背包的题这算是第一次、?岂不是有点晚了?
做这道题的时候多亏了Min的帮助不然自己也不理解,01背包,完全背包,多重背包
在背包九讲都很清楚
#include<stdio.h>#include<string.h>int m,n,i,j,w[3412],p[3412],c[12892];int main(){memset(w,0,sizeof(w));memset(p,0,sizeof(p));memset(c,0,sizeof(c));scanf("%d%d",&n,&m);for(i=1;i<=n;i++)scanf("%d%d",&w[i],&p[i]);for(i=1;i<=n;i++)for(j=m;j>=0;j--)//这里应该注意一下01背包跟完全背包的区别{if(j>=w[i]){if(c[j]<p[i]+c[j-w[i]])c[j]=p[i]+c[j-w[i]];elsec[j]=c[j];}}printf("%d\n",c[m]);return 0;}


原创粉丝点击