HDU 3182 Hamburger Magi(状压DP)

原创 2016年08月31日 12:10:15
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.

题意:

做汉堡吧!给出n个汉堡的价值和能量花费,以及其依赖关系(要做这个汉堡必须先完成其他一些汉堡),给出初始能量,求能做出汉堡的最大价值。

解题思路:

单纯的状压DP,三层注意循环的顺序。

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int dp[1<<15][105];                  //dp[i][j]表示 当前状态为i使用了j的能量所能获得的最大价值。
bool vis[1<<15][105];                //表示当前状态是否能够达到
int w[20],c[20],need[20][20];
int main()
{
        int T,n,maxv,nn;
        cin>>T;
        while(T--)
        {
                cin>>n>>maxv;
                for(int i=0;i<n;i++)
                        cin>>w[i];
                for(int i=0;i<n;i++)
                        cin>>c[i];
                for(int i=0;i<n;i++)
                {
                        cin>>need[i][0];
                        for(int j=1;j<=need[i][0];j++)
                                cin>>need[i][j];
                }
                int ans=0;
                memset(dp,0,sizeof dp);
                memset(vis,0,sizeof vis);
                int cnt=(1<<n)-1;
                vis[0][0]=1;                           
                for(int j=0;j<=maxv;j++)               //刷表
                        for(int k=0;k<=cnt;k++)
                        {
                                if(!vis[k][j])         //只用能够达到的状态
                                        continue;
                                for(int i=0;i<n;i++)   //每种状态尝试做新的汉堡
                                {
                                        if(k&(1<<i))
                                                continue;
                                        if(j+c[i]>maxv)
                                                continue;
                                        int flag=0;
                                        for(int t=1;t<=need[i][0];t++)
                                        {
                                                int id=1<<(need[i][t]-1);
                                                if((k&id)==0)
                                                {
                                                        flag=1;
                                                        break;
                                                }
                                        }
                                        if(flag)
                                                continue;
                                        dp[k|(1<<i)][j+c[i]]=max(dp[k|(1<<i)][j+c[i]],dp[k][j]+w[i]);
                                        vis[k|(1<<i)][j+c[i]]=1;
                                        ans=max(ans,dp[k|(1<<i)][j+c[i]]);       //找答案
                                }
                        }
                cout<<ans<<endl;
        }
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

HDU 3182 Hamburger Magi(状压dp)

Problem Description In the mysterious forest, there is a group of Magi. Most of them like to eat hu...

HDU3182 - Hamburger Magi(状压DP)

题目链接 HDU3182 【题意】

HDU 3182 Hamburger Magi(状压dp)

HDU 3182 Hamburger Magi(状压dp)

hdu3182---Hamburger Magi(状压dp)

Problem Description In the mysterious forest, there is a group of Magi. Most of them like to eat hu...

hdu 3182 Hamburger Magi 状压

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

hdu 3182 Hamburger Magi(状态压缩DP)

这道题做得很有成就感!虽然被某牛称为水题。。。 假如把题目给的数据放入一个n*n的矩阵中,对于每一层第i个格子为1,表示做出了第i个汉堡,为0表示没有做出这个汉堡。那么这一层可以做出这个汉堡的条件就...

hdu 3182 Hamburger Magi

http://acm.hdu.edu.cn/showproblem.php?pid=3182 建议先做hdu 1074题。 同1074题思路差不多,但这里加了条件判断,即判断做第k个汉堡是是否做出...

HDU3182 Hamburger Magi(状态压缩)

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

HDU - 3182:Hamburger Magi

HDU - 3182:Hamburger Magi In the mysterious forest, there is a group of Magi. Most of them like to ...

HDU3182-状压dp

Hamburger Magi Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)