为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

一道好的DP

2010-08-03 4页 doc 34KB 17阅读

用户头像

is_061274

暂无简介

举报
一道好的DPConsumer Submit: 786 Accepted:169 Time Limit: 2000MS Memory Limit: 65536K Description FJ is going to do some shopping, and before that, he needs some boxes to carry the different kinds of stuff he is going to buy. Each box is assigned to carry some specific kinds...
一道好的DP
Consumer Submit: 786 Accepted:169 Time Limit: 2000MS Memory Limit: 65536K Description FJ is going to do some shopping, and before that, he needs some boxes to carry the different kinds of stuff he is going to buy. Each box is assigned to carry some specific kinds of stuff (that is to say, if he is going to buy one of these stuff, he has to buy the box beforehand). Each kind of stuff has its own value. Now FJ only has an amount of W dollars for shopping, he intends to get the highest value with the money. Input The first line will contain two integers, n (the number of boxes 1 <= n <= 50), w (the amount of money FJ has, 1 <= w <= 100000) Then n lines follow. Each line contains the following number pi (the price of the ith box 1<=pi<=1000), mi (1<=mi<=10 the number goods ith box can carry), and mi pairs of numbers, the price cj (1<=cj<=100), the value vj(1<=vj<=1000000) Output For each test case, output the maximum value FJ can get Sample Input 3 800 300 2 30 50 25 80 600 1 50 130 400 3 40 70 30 40 35 60 Sample Output 210 http://acm.scs.bupt.cn/onlinejudge/showproblem.php?problem_id=1848 #include #include #include #include using namespace std; int dp[100010],f[100010],V,N,m,n,cost,weight; int main() { while(scanf("%d%d",&N,&V)!=EOF) { memset(f,0,sizeof(f)); memset(dp,0,sizeof(dp)); for(int i=0;i=cost+m;v--)//对每一组/1背包 dp[v]=max(dp[v],dp[v-cost]+weight); } for(int k=cost;k<=V;k++)//如果更新了就赋给原dp if(dp[k]>f[k])f[k]=dp[k]; } printf("%d\n",f[V]); } return 0; } 还可以把有附件的背包转换为分组背包来做!(暂时TLE有待优化) #include #include #include #include using namespace std; int n,m,N,V,f[100010],dp[100010],total[100]; struct bag { int cost,weight; }New[60][100]; int main() { int cost,weight; while(scanf("%d%d",&N,&V)!=EOF) { memset(total,0,sizeof(total)); int mmin=1<<30; for(int i=0;icost)mmin=cost; for(int v=V;v>=m+cost;v--) dp[v]=max(dp[v],dp[v-cost]+weight); } for(int v=2;v<=V;v++) { if(dp[v]==dp[v-1])continue; New[i][total[i]].cost=v; New[i][total[i]++].weight=dp[v]; } } memset(f,0,sizeof(f)); for(int i=0;i=mmin;v--) { int mmax=0; for(int k=0;kmmax)mmax=f[v-New[i][k].cost]+New[i][k].weight; } if(f[v]
/
本文档为【一道好的DP】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
热门搜索

历史搜索

    清空历史搜索