HDU 3182 Hamburger Magi

题目:http://acm.hdu.edu.cn/showproblem.php?pid=3182

题意:有n个汉堡,每个汉堡有一个价值和做汉堡需要的体力,并且做有些汉堡前需要先做别的汉堡,问最多能做出多大价值

每个汉堡只能做一次并且有先后顺序,所以需要状压dp而不能直接暴力判断

做汉堡的前置关系可以用状压来简化判断

还可以预处理出每个状态需要花费的体力

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
int n,m,maxx;
int cost[1<<15];
int a[20];
int dp[1<<15];
int v[20],e[20];
int main()
{
    ios::sync_with_stdio(false);
    int T;
    cin>>T;
    while(T--)
    {
        cin>>n>>m;
        for(int i=0;i<n;i++)
            cin>>v[i];
        for(int i=0;i<n;i++)
            cin>>e[i];
        for(int i=0;i<n;i++)
        {
            a[i]=0;
            int x,y;
            cin>>x;
            while(x--)
            {
                cin>>y;
                y--;
                a[i]|=(1<<y);
            }
        }
        int tot=1<<n;
        memset(cost,0,sizeof(cost));
        for(int i=0;i<tot;i++)
        {
            for(int j=0;j<n;j++)
                if ((i>>j)&1)
                    cost[i]+=e[j];
        }
        memset(dp,-1,sizeof(dp));
        dp[0]=0;
        maxx=0;
        for(int i=0;i<tot;i++)
        {
            if (dp[i]==-1) continue;
            for(int j=0;j<n;j++)
            {
                 if ((i>>j)&1) continue;
                 if (cost[i]+e[j]>m) continue;
                 if ((i&a[j])==a[j])
                 {
                     dp[i|(1<<j)]=dp[i]+v[j];
                     maxx=max(maxx,dp[i|(1<<j)]);
                 }
            }
        }
        cout<<maxx<<endl;
    }
    return 0;
}

  

时间: 2024-10-09 10:58:28

HDU 3182 Hamburger Magi的相关文章

HDU 3182 Hamburger Magi(状压dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3182 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 foo

hdu 3182 Hamburger Magi 状压

Hamburger Magi Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 241    Accepted Submission(s): 83 Problem Description In the mysterious forest, there is a group of Magi. Most of them like to eat

Hamburger Magi(hdu 3182)

Hamburger Magi Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 437    Accepted Submission(s): 144 Problem Description In the mysterious forest, there is a group of Magi. Most of them like to eat

[状压dp]HDU3182 Hamburger Magi

题意 大致是: 有n个汉堡 m块钱  (n<=15) 然后分别给n个汉堡的能量 再分别给n个汉堡所需的花费 然后下面n行 第i行有x个汉堡要在i汉堡之前吃 然后给出这x个汉堡的编号 输出 能获得的最大能量 分析: n那么小, 很明显状压 状压吃的顺序 每个汉堡的花费是固定的, 因此只要一维的dp  再加个数组 记录当前状态的花费 即可 状态转移前判断 当前是否满足 顺序 以及 花费 1 #include <cstdio> 2 #include <cstdlib> 3 #inc

状态压缩 HDU 3182

t组数据 n个汉堡 e的能量 接下来的2行 val    n个 得到的权 cost  n个 花去的能量 接下来n行 每行一个q  q个数字 代表这类汉堡做好要的前提  每个汉堡只能用一次 #include<stdio.h> #include<algorithm> #include<string.h> using namespace std; #define MAXN 1<<15 int dp[MAXN]; int val[101],co[101]; int

hdu 3182 状压dp

1 #include "bits/stdc++.h" 2 using namespace std; 3 int T; 4 5 int dp[40000]; 6 int totene[40000]; 7 8 int N, E; 9 int val[20], ene[20], cond[20]; 10 11 const int INF = 0x3f3f3f3f; 12 13 int main() 14 { 15 scanf("%d", &T); 16 while

hdu3182---Hamburger Magi(状压dp)

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

HDU_3182_Hamburger Magi_状态压缩dp

Hamburger Magi Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 509    Accepted Submission(s): 163 Problem Description In the mysterious forest, there is a group of Magi. Most of them like to eat

概率dp HDU 3853

H - LOOPS Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 3853 Appoint description:  System Crawler  (2014-10-22) Description Akemi Homura is a Mahou Shoujo (Puella Magi/Magical Girl). Homura wa