1354 - Rubiks

时间限制:1秒 内存限制:64兆

452 次提交 102 次通过
题目描述
Isun is a genius. Not only he is an expert in algorithm, but also he plays damn-good in many funny games. Besides, he can recover a Rubik in 16 seconds or even less. The man is very crazy about Rubiks, and he has bought a lot of Rubiks. As we know, there are
so many kinds of Rubiks in the world. Isun wants to buy the most valuable ones with his limited money. There are N kinds of Rubiks in all. Each of them has a price Pi(1<=i<=N) RMB and a value Vi(1<=i<=N). Isun will
pay no more than M (RMB) in total. In addition, there are some Rubik families like “甲X” or “封X”. And a kind of Rubik belongs to one family at most. If Isun buys a group of them, the value of them as a family will increase. Can you get the largest value of
the Rubiks that Isun can get with M (RMB). (Isun just buy one Rubik each kind at most)
输入
The input contains several test cases and is ended by EOF. Each test case begins with two integers: N (1<=N<=1000) and M (1<=M<=10000). The second line contains N integers representing the prices of the Rubiks. (1<=Pi<=10000) The third line contains N integers
representing the value of the Rubiks. (1<=Vi<=10000) Then a line contains an integer G(0<=G<=15) representing the number of the Rubik families. Next G lines each with a start of an integer Si(1<=Si<=N) representing the number of Rubiks in the ith family. The
following Si integers represent Rubik’s id (which start from 1 to N). And an integer Yi at the end means the value increased if you buy them all.(1<=Yi<=10000)
输出
There should be one line per test case containing the largest value.
样例输入
4 10
4 5 3 6
1 2 100 200
1
2 1 2 330
样例输出

333

动态规划

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <stdio.h> using namespace std;
int v[1020];
int w[1020];
int v2[20];
int w2[20];
int dp[10005];
int bp[10005];
int tag[1005];
int b[20][1005];
int n,m;
int g;
int s[20],sv;
int a;
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=1;i<=n;i++)
scanf("%d",&w[i]);
for(int i=1;i<=n;i++)
scanf("%d",&v[i]);
scanf("%d",&g);
memset(tag,0,sizeof(tag));
for(int i=1;i<=g;i++)
{
scanf("%d",&s[i]);
int value=0;int weight=0;
for(int j=1;j<=s[i];j++)
{
scanf("%d",&b[i][j]);
tag[b[i][j]]=i;
value+=v[b[i][j]];
weight+=w[b[i][j]];
}
scanf("%d",&a);
value+=a;
w2[i]=weight;
v2[i]=value;
}
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
{
if(!tag[i])
{
for(int j=m;j>=w[i];j--)
dp[j]=max(dp[j],dp[j-w[i]]+v[i]);
}
}
for(int i=1;i<=g;i++)
{
memcpy(bp, dp, sizeof(dp));
for(int j=m;j>=w2[i];j--)
bp[j]=max(bp[j],bp[j-w2[i]]+v2[i]);
for(int k=1;k<=s[i];k++)
{
for(int j=m;j>=w[b[i][k]];j--)
dp[j]=max(dp[j],dp[j-w[b[i][k]]]+v[b[i][k]]);
}
for(int j=1;j<=m;j++)
dp[j]=max(dp[j],bp[j]); }
printf("%d\n",dp[m]);
}
return 0;
}

HUST 1354 - Rubiks (DP)的更多相关文章

  1. HUST 1354 Rubiks

    背包.注释写详细了. 本想这样写:每个组内各自做背包,然后组间做背包,但是由于这题M=10000,时间复杂度太大. #include<cstdio> #include<cstring ...

  2. 51nod 1354【DP】

    (我一定是A了一题假DP) 给定序列a[0],a[1],a[2],...,a[n-1] 和一个整数K时, 有多少子序列所有元素乘起来恰好等于K. K<=1e8; 思路: 感觉 k 的 约数是突破 ...

  3. 1354 选数字 DP背包 + 数学剪枝

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1354&judgeId=187448 其实这题和在若干个数字中,选 ...

  4. HUST 1569(Burnside定理+容斥+数位dp+矩阵快速幂)

    传送门:Gift 题意:由n(n<=1e9)个珍珠构成的项链,珍珠包含幸运数字(有且仅由4或7组成),取区间[L,R]内的数字,相邻的数字不能相同,且旋转得到的相同的数列为一种,为最终能构成多少 ...

  5. HUST 4681 String (DP LCS变形)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4681 题目大意:给定三个字符串A,B,C 求最长的串D,要求(1)D是A的字序列 (2)D是B的子序列 ...

  6. UVA1625Color Lenth(DP+LCS变形 未AC)

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105116#problem/C 紫书P276 res[i][j]表示第一个序列移动i个,第 ...

  7. HDU 4113 Construct the Great Wall(插头dp)

    好久没做插头dp的样子,一开始以为这题是插头,状压,插头,状压,插头,状压,插头,状压,无限对又错. 昨天看到的这题. 百度之后发现没有人发题解,hust也没,hdu也没discuss...在acm- ...

  8. DP(Dynamic programming)——尽力学习之中(2016 HUAS ACM 暑假集训-5)

    这周不打算按照以往的方式更新博客,而是采用整体的方式.一是因为学的太少,没东西写:二是这篇博客会经常更新的.如题,DP——尽力学习之中. ------------------------------- ...

  9. hdu1520 树形dp Anniversary party

    A - Anniversary party Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

随机推荐

  1. Unity3D教程宝典之Web服务器篇:(第三讲)PHP的Hello World

    转载自风宇冲Unity3D教程学院 引言:PHP是比较简单的编程语言,即使没接触过的也可以现学现用.PHP教程文档PHP100视频教程                           Unity接 ...

  2. lombok使用总结

    前提 这篇文章主要介绍lombok的使用,至于lombok的源码和原理暂不探究,可以看上一篇文章插件化注解处理API去了解lombok的基本原理.参考资料: lombok官网 lombok官方教程-l ...

  3. ipv6 dns list

    ip v6 DNS list: 将首选DNS服务器地址填上2001:778::37 备用DNS服务器地址填上2001:df8:0:7::1

  4. java之this关键字

    this使用范围 1.在类的方法定义中使用的this关键字代表调用该方法对象的引用. 2.当必须指出当前使用方法的对象是谁时,要使用关键字this. 3.有时使用this可以处理方法中成员变量和参数重 ...

  5. EntityFramework.SqlServer.dll 中发生 其他信息: 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: Named Pipes Provider, error: 40 - 无法打开到 SQL Server 的连接)

    解决方案: 1.打开Sql server 管理配置器 或者在命令行输入:SQLServerManager10.msc 2.点击MSSQLSERVER的协议,在右侧的页面中选择TCP/IP协议启用 3. ...

  6. redis实践一些要注意的事项

    不要放垃圾数据,及时清理无用数据实验性的数据和下线的业务数据及时删除; key尽量都设置过期时间对具有时效性的key设置过期时间,通过redis自身的过期key清理策略来降低过期key对于内存的占用, ...

  7. mysql 函数substring_index() 截取字符串

    函数: 1.从左开始截取字符串 left(str, length) 说明:left(被截取字段,截取长度) 例:select left(content,200) as abstract from my ...

  8. poj2184 Cow Exhibition(p-01背包的灵活运用)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:id=2184">http://poj.org/problem?id=2184 Descrip ...

  9. Django学习之第三方储存服务器的使用

    最近,越来越多的公司采用第三方储存来作为视频,图片的储存工具. 国内的像七牛,阿里云的OSS,国外的像亚马逊的S3,微软的azure都是非常有名的第三方储存. 下面以阿里的OSS为例,来介绍第三储存的 ...

  10. windows system.exe占用文件

    1)问题的原因是出于一个服务Application Experience,如果装好系统后就把此服务设为手动启动了,平时运行也没什么异常.但是win7在运行exe时如果没有这个服务的辅助就会长时间的占用 ...