HDU 3182 Hamburger Magi(状压DP)
2015-04-22 18:49
351 查看
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
Sample Output
简单的状压DP:
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 307298 79 58 0 72 3 2 3 4 2 1 4 1 1 0
Sample Output
298
简单的状压DP:
#include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<string> #include<iostream> #include<queue> #include<cmath> #include<map> #include<stack> #include<bitset> using namespace std; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) typedef long long LL; typedef pair<int,int>pil; const int INF = 0x3f3f3f3f; int dp[(1<<15)+100][110]; int f[20],v[20],e[20]; int c,N,E; int main() { int q,x; scanf("%d",&c); while(c--) { scanf("%d%d",&N,&E); REP(i,N) scanf("%d",&v[i]); REP(i,N) scanf("%d",&e[i]); CLEAR(dp,-1);CLEAR(f,0); REP(i,N) { scanf("%d",&q); while(q--) { scanf("%d",&x); f[i]|=(1<<(x-1)); } } int status=(1<<N)-1;int ans=0; dp[0][E]=0; for(int i=0;i<status;i++) { for(int k=0;k<N;k++) { if((i|f[k])!=i) continue; for(int j=E;j>=e[k];j--) { if(dp[i][j]==-1) continue; if(i&(1<<k)) continue; int st=i^(1<<k); dp[st][j-e[k]]=max(dp[st][j-e[k]],dp[i][j]+v[k]); if(dp[st][j-e[k]]>ans) ans=dp[st][j-e[k]]; } } } printf("%d\n",ans); } return 0; } /* 1 4 90 243 464 307 298 79 58 0 72 3 2 3 4 2 1 4 1 1 0 */
相关文章推荐
- HDU 3182 Hamburger Magi(状压DP)
- HDU 3182 Hamburger Magi(状压DP)
- hdu_3182_Hamburger Magi(状压DP)
- hdu_3182_Hamburger Magi(状压DP)
- HDU 3182 Hamburger Magi(状压dp)
- 【DP】 HDU 3182 Hamburger Magi 状压
- 【DP】 HDU 3182 Hamburger Magi 状压
- HDU 3182 Hamburger Magi(状压dp)
- hdu 3182 Hamburger Magi(状压dp)
- HDU 3182 Hamburger Magi(状压dp)
- HDU 3182 Hamburger Magi(状压DP解决有顺序背包问题)
- hdu 3182 Hamburger Magi 状压
- hdu 3182 Hamburger Magi(状态压缩DP)
- HDU - 3182:Hamburger Magi(状压DP)
- HDU 3181 Hamburger Magi(状压DP)
- hdu 3182(状压dp)
- HDU_3182_Hamburger Magi_状态压缩dp
- Hdu 4856 Tunnels(状压dp)
- hdu 1074 Doing Homework(状压DP)
- hdu--4856--状压dp