Description
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 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
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
01背包和拓扑排序结合。。。情况太多。。。就直接遍历所有情况了。。。->状压解决
位运算符号&不要手残写多了。。、
题意:
一个人做汉堡包,每个汉堡包有自己的花费和价值,某些汉堡包必须是在其他的某些汉堡包已经做好了的前提下才能制作,给你这个人的初始钱数,问最大的价值是多少。
思路:
比较简单的一个题目,首先我们开一个数组dp[i]表示i状态(状态压缩)时的最大价值,把每一个状态都用这n个汉堡包更新一下,还的开个数组money[i]表示的是i状态是的剩余钱数,更新的时候记住一点就是每个点只能用一次,也就是当前状态里如果有i这个点,那么i不能在来更新了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
using namespace std;
const int N = (1<<16)+10;
const int inf = -0x3f3f3f3f;
int dp[N], money[N], limit[N][20], v[N], w[N];
bool judge(int s,int j)
{
for(int i=1;i<=limit[j][0];i++)
{
if(!(s&(1<<(limit[j][i]-1)))) return 0;
}
return 1;
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n, m;
scanf("%d %d", &n, &m);
memset(money,0,sizeof(money));
for(int i=1;i<=n;i++)
scanf("%d", &v[i]);
for(int i=1;i<=n;i++)
scanf("%d", &w[i]);
for(int i=1;i<=n;i++)
{
scanf("%d", &limit[i][0]);
for(int j=1;j<=limit[i][0];j++)
scanf("%d",&limit[i][j]);
}
for(int i=1;i<(1<<n);i++)
dp[i]=-100000000, money[i]=0;
dp[0]=0,money[0]=m;
int ans=0;
for(int i=0;i<(1<<n);i++)
{
for(int j=1;j<=n;j++)
{
if(i&(1<<(j-1))) continue;
int state=i|(1<<(j-1));
if(dp[state]<dp[i]+v[j]&&money[i]>=w[j]&&judge(i,j))
{
dp[state]=dp[i]+v[j];
money[state]=money[i]-w[j];
ans=max(ans,dp[state]);
}
}
}
printf("%d\n",ans);
}
return 0;
}