HUST 1354 - Rubiks (DP)
来源:互联网 发布:大学人工智能的专业 编辑:程序博客网 时间:2024/06/13 07:47
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 104 5 3 61 2 100 20012 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;}
0 0
- HUST 1354 - Rubiks (DP)
- HUST 1354 Rubiks
- 【dp】HUST-1351-Group
- hust 1328 String (kmp+dp)
- Hust oj 2010 二等队形(dp)
- Hust oj 1186 青蛙过河(dp)
- Hust oj 1284 编辑距离(DP)
- Hust oj 1426 集训队的晚饭(DP)
- HUST
- HUST
- HUST
- HUST
- Hust oj 1812 小乐乐想出门(基础dp)
- Hust oj 1334 最好的心情(DP+DFS)
- Hust oj 1123 Grid(组合数||dp)
- Hust oj 1861 猥琐宅男——koko(DP)
- Hust oj 1813 小乐乐要下山(dp + 路径还原)
- HUST 1328
- SharePoint Server 2016正式发布,新增功能
- poj1942线性复杂度求组合数
- [javase]多线程(二)
- Objective-C Runtime 运行时之一:类与对象
- 棋盘问题
- HUST 1354 - Rubiks (DP)
- 跳表
- centos 70 在vmware中安装笔记
- 关于java编译时注解你需要知道的二三事。解除你的顾虑!
- 构建神器Gradle
- JavaScript -- 面向对象的简单分析
- HDFS 启动与关闭
- 数据库原理
- 向量空间模型(VSM)算法