2014
03-06

# Hamburger Magi

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!

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.

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.

1
4 90
243 464 307 298
79 58 0 72
3 2 3 4
2 1 4
1 1
0

298

dp【statu】表示在该状态下消耗的能量  获得的价值

AC代码如下：

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

struct Node{
int id[101];
int tot;
};

struct DP{
int sumv;
int sume;
}dp[1<<16];
int v[101], e[101];
int N, E;
Node node[101];

int main(){
int T;
cin >> T;
while( T-- ){
cin >> N >> E;
for( int i = 1; i <= N; i++ ){
cin >> v[i];
}
for( int i = 1; i <= N; i++ ){
cin >> e[i];
}
for( int i = 1; i <= N; i++ ){
cin >> node[i].tot;
for( int j = 0; j < node[i].tot; j++ ){
cin >> node[i].id[j];
}
}
memset( dp, -1, sizeof( dp ) );
dp[0].sume = dp[0].sumv = 0;
for( int i = 0; i < ( 1 << N ); i++ ){
if( dp[i].sume == -1 ){
continue;
}
for( int j = 1; j <= N; j++ ){
if( i & ( 1 << ( j - 1 ) ) ){
continue;
}
int flag = 1;
for( int k = 0; k < node[j].tot; k++ ){
if( !( i & ( 1 << ( node[j].id[k] - 1 ) ) ) ){
flag = 0;
break;
}
}
if( flag == 1 ){
dp[i|(1<<(j-1))].sumv = dp[i].sumv + v[j];
dp[i|(1<<(j-1))].sume = dp[i].sume + e[j];
}
}
}
int ans = 0;
for( int i = 0; i < ( 1 << N ); i++ ){
if( dp[i].sume <= E ){
ans = max( ans, dp[i].sumv );
}
}
cout << ans << endl;
}
return 0;
}

1. A猴子认识的所有猴子和B猴子认识的所有猴子都能认识，这句话用《爱屋及乌》描述比较容易理解……