HDU_3182_Hamburger Magi_状态压缩dp
2016-05-13 17:39
351 查看
Hamburger Magi
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 509 Accepted Submission(s): 163
[align=left]Problem Description[/align]
In the mysterious forest, there is a group of Magi. Most of them like to eat human beings, so they are called “The Ogre Magi”, but there is an special one whose favorite food is hamburger, having been jeered by the others as “The Hamburger Magi”.
Let’s give The Hamburger Magi a nickname “HamMagi”, HamMagi don’t only love to eat but also to make hamburgers, he makes N hamburgers, and he gives these each hamburger a value as Vi, and each will cost him Ei energy, (He can use in total M energy each day). In addition, some hamburgers can’t be made directly, for example, HamMagi can make a “Big Mac” only if “New Orleams roasted burger combo” and “Mexican twister combo” are all already made. Of course, he will only make each kind of hamburger once within a single day. Now he wants to know the maximal total value he can get after the whole day’s hard work, but he is too tired so this is your task now!
[align=left]Input[/align]
The first line consists of an integer C(C<=50), indicating the number of test cases.
The first line of each case consists of two integers N,E(1<=N<=15,0<=E<=100) , indicating there are N kinds of hamburgers can be made and the initial energy he has.
The second line of each case contains N integers V1,V2…VN, (Vi<=1000)indicating the value of each kind of hamburger.
The third line of each case contains N integers E1,E2…EN, (Ei<=100)indicating the energy each kind of hamburger cost.
Then N lines follow, each line starts with an integer Qi, then Qi integers follow, indicating the hamburgers that making ith hamburger needs.
[align=left]Output[/align]
For each line, output an integer indicating the maximum total value HamMagi can get.
[align=left]Sample Input[/align]
1
4 90
243 464 307 298
79 58 0 72
3 2 3 4
2 1 4
1 1
0
[align=left]Sample Output[/align]
298
做的第二道状态压缩dp。
状态压缩dp。虽然感觉不难,但是没能独立写出来。自己的代码估计是在关联的汉堡的处理上存在问题。
看了题解,很高兴基本套路没错,状态转移有问题(思路有问题)。
最后改为从后往前推,初始dp数组为-1,dp[0]=0,这样往后推,那么假如现在是dp[10100],处理到第5个汉堡,而第5个汉堡跟第1个和第3个相关联,那么在没有制作1和3时dp[10100]为-1,而为-1直接跳过,这样很好地处理了关联的问题。
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; struct Node { int value; int cost; int cnt; int rela[20]; } ham[20]; struct Node1 { int value; int cost; }; Node1 dp[1<<15]; int main() { int t,kind,ener; scanf("%d",&t); while(t--) { memset(dp,-1,sizeof(dp)); scanf("%d%d",&kind,&ener); for(int i=1; i<=kind; i++) scanf("%d",&ham[i].value); for(int i=1; i<=kind; i++) scanf("%d",&ham[i].cost); for(int i=1; i<=kind; i++) { scanf("%d",&ham[i].cnt); for(int j=0; j<ham[i].cnt; j++) scanf("%d",&ham[i].rela[j]); } int en=1<<kind; int ans=0; dp[0].cost=dp[0].value=0; for(int j=0; j<en; j++) { if(dp[j].value==-1) continue; for(int i=1; i<=kind; i++) { int tem=1<<(kind-i); if(j&tem) continue; int flag=1; for(int k=0; k<ham[i].cnt; k++) { int temp=1<<(kind-ham[i].rela[k]); if(!(temp&j)) { flag=0; break; } } if(flag) { dp[j+tem].value=dp[j].value+ham[i].value; dp[j+tem].cost=dp[j].cost+ham[i].cost; } } } for(int i=0; i<en; i++) { if(dp[i].cost>ener) continue; if(ans<dp[i].value) ans=dp[i].value; } printf("%d\n",ans); } return 0; }
View Code
相关文章推荐
- 并查集
- 知道这20个正则表达式 能让你少写1,000行代码
- keyCode对照表
- APP开机自启动
- AD域账户自动登陆(仅限IE浏览器)Java简单实现
- 【REACT NATIVE 系列教程之三】函数绑定与FLEXBOX是用好REACT的基础
- jq的slideToggle()在ie8里遇到bug:下盒子的margin—top失效,margin-top塌陷解决方法
- 常浏览的博客和网站
- java中文乱码解决之道(八)—–解决URL中文乱码问题
- 如何调用Android隐藏API
- 算法15 之二叉树排序
- 算法14 之归并排序
- CentOS7 PostgreSQL 主从配置( 三)
- 整理:Linux网络编程之sockaddr与sockaddr_in,sockaddr_un结构体详细讲解
- docker学习-简单安装及使用
- Invalid Regular Expression Options
- mysql表的连接方式
- codevs 1242 布局(查分约束+SPFA)
- 对于风行小组第一阶段冲刺成果的概括
- Python常见问题(6):Python在Windows上的问题 Python on Windows FAQ