首页 > 其他 > 详细

洛谷 P3112 后卫马克 —— 状压DP

时间:2018-07-30 00:20:15      阅读:232      评论:0      收藏:0      [点我收藏+]

题目:https://www.luogu.org/problemnew/show/P3112

状压DP...转移不错。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
ll const maxn=(1<<20),inf=1e18;
ll n,H,h[25],w[25],s[25],f[maxn],mx,ans=-inf;
int main()
{
    scanf("%lld%lld",&n,&H); mx=(1<<n)-1;
    for(int i=1;i<=n;i++)scanf("%lld%lld%lld",&h[i],&w[i],&s[i]);
    f[0]=inf;//下面取min 
    for(int i=1;i<=mx;i++)
    {
        ll tmp=0; f[i]=-inf;
        for(int j=1;j<=n;j++)
            if((1<<(j-1))&i)
            {    
                tmp+=h[j];
                f[i]=max(f[i],min(f[i^(1<<(j-1))]-w[j],s[j]));
            }
        if(tmp>=H&&f[i]>0)ans=max(ans,f[i]);
    }
    if(ans<0)printf("Mark is too tall\n");
    else printf("%lld\n",ans);
    return 0;
}

 

洛谷 P3112 后卫马克 —— 状压DP

原文:https://www.cnblogs.com/Zinn/p/9388327.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!