Hamburger Magi
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 241 Accepted Submission(s): 83
Problem Description
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!
Input
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.
Output
For each line, output an integer indicating the maximum total value HamMagi can get.
Sample Input
1 4 90 243 464 307 298 79 58 0 72 3 2 3 4 2 1 4 1 1 0
Sample Output
298
题意
输入n e
n代表n个汉堡, e代表最大负重,每个汉堡只能拿一次
接下来一行四个数表示 n个汉堡的价值
再一行表示n个汉堡的花费
再接下来n行表示 第i汉堡如果要的话 需要先拿哪几个
如案例 3 2 3 4 ,表示要拿 第一个汉堡要先拿 编号2 3 4 这三个汉堡
思路
历遍所有状态,求最大值
#include<stdio.h> int nd[20],vi[20],ci[20]; int main() { int t,n,c,i,j; int tn,tem,tt; int lim,ans,flag,allc,allv; int zuiduo,dangqian,youbian; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&c); for(i=0;i<n;i++) scanf("%d",&vi[i]); for(i=0;i<n;i++) scanf("%d",&ci[i]); for(i=0;i<n;i++) { tem=0; scanf("%d",&tn); while(tn--) { scanf("%d",&tt); tem|=(1<<(tt-1)); } nd[i]=tem; } lim=1<<n; ans=0; for(i=0;i<lim;i++)//zhuangtai { allv=0; allc=0; zuiduo=15; dangqian=0; while(zuiduo--) { youbian=0; for(j=0;j<n;j++)//meigewei { if((i>>j)&1&&((dangqian>>j)&1)==0)//xuyao { if((dangqian&nd[j])==nd[j])//条件符合 { allv+=vi[j]; allc+=ci[j]; youbian=1; dangqian|=1<<j; } } } if(youbian==0) break; } if(allc>c||allv<ans||dangqian!=i) continue; ans=allv; } printf("%d\n",ans); } return 0; }