codeforce 580D Kefa and Dishes (状压DP)

原题地址:http://codeforces.com/contest/580/problem/D

 

题意:

 

题解

状压DP

 

/* http://codeforces.com/contest/580/problem/D */

#include<bits/stdc++.h>

#define clr(x,y) memset((x),(y),sizeof(x))

using namespace std;
typedef long long LL;

const int maxn=1<<18;

LL dp[maxn+5][20];
LL A[20];
LL B[20][20];

int Count(int x)
{
    int res=0;
    while (x>0)
    {
        x-=x&(-x);
        ++res;
    }
    return res;
}

int n,m,k;

int main(void)
{
    #ifdef ex
    freopen ("../in.txt","r",stdin);
    //freopen ("../out.txt","w",stdout);
    #endif

    int p,q;
    LL t;

    scanf("%d%d%d",&n,&m,&k);
    for (int i=0;i<=n-1;++i)
    {
        scanf("%I64d",&A[i]);
    }
    for (int i=1;i<=k;++i)
    {
        scanf("%d%d%I64d",&p,&q,&t);
        B[p-1][q-1]=t;
    }

    LL ans=0;
    clr(dp,0);
    for (int i=0;i<=n-1;++i)
    {
        dp[1<<i][i]=A[i];
    }
    int s=(1<<n)-1;
    for (int mask=0;mask<=s;++mask)
    {
        for (int i=0;i<=n-1;++i)
        {
            if (mask>>i & 1)
            {
                for (int j=0;j<=n-1;++j)
                {
                    if (!(mask>>j & 1))
                    {
                        dp[mask|1<<j][j]=max(dp[mask|1<<j][j],dp[mask][i]+B[j][i]+A[j]);
                    }
                }
            }
        }
    }

    for (int mask=0;mask<=s;++mask) /*处理完了之后再更新答案,可以避免遗漏*/
    {
        if (Count(mask)==m)
        for (int i=0;i<=n-1;++i)
        {
            ans=max(ans,dp[mask][i]);
        }
    }
    printf("%I64d\n",ans);
}

 

转载于:https://www.cnblogs.com/123-123/p/5572815.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值