您的位置:首页 > 其它

HDU - 3182:Hamburger Magi(状压DP)

2017-08-04 09:00 295 查看





Hamburger Magi

                                                       Time Limit: 2000/1000 MS (Java/Others)    Memory Limit:
32768/32768 K (Java/Others)

                                                                              Total Submission(s): 1071    Accepted Submission(s): 396


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,E
4000
2…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



思路:状压DP。

#include<bits/stdc++.h>
using namespace std;
struct lenka
{
int v,c;
vector<int>need;
}a[20];
int E,n;
int d[1<<15][101];
bool check(int x,int j)
{
for(int k=0;k<a[j].need.size();k++)
{
int nex=a[j].need[k];
nex--;
if(((1<<nex)&x)==0)return 0;
}
return 1;
}
int main()
{
int T;cin>>T;
while(T--)
{
cin>>n>>E;
memset(a,0,sizeof a);
for(int i=0;i<n;i++)cin>>a[i].v;
for(int i=0;i<n;i++)cin>>a[i].c;
for(int i=0;i<n;i++)
{
int skt,x;
cin>>skt;
while(skt--)cin>>x,a[i].need.push_back(x);
}
memset(d,-1,sizeof d);
int ans=0;
d[0][E]=0;
for(int i=0;i<(1<<n);i++)
{
for(int j=0;j<n;j++)
{
if(i&(1<<j))continue;     //判断这个汉堡是否已经做了
if(check(i,j)==0)continue;    //判断做这个汉堡所需要的汉堡做没做
for(int k=0;k<=E-a[j].c;k++)
{
if(d[i][k+a[j].c]!=-1)  //如果这个状态曾经到达过
{
d[i|(1<<j)][k]=max(d[i|(1<<j)][k],d[i][k+a[j].c]+a[j].v);
}
}
}
for(int k=0;k<=E;k++)ans=max(ans,d[i][k]);
}
cout<<ans<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: