• 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
    ",ans);
    }
  • 相关阅读:
    python -- twisted初探
    python -- redis连接与使用
    redis使用
    python -- 异步编程
    python
    python
    福大软工 · 最终作业
    福大软工 · 第十二次作业
    Beta 冲刺(7/7)
    Beta 冲刺(6/7)
  • 原文地址:https://www.cnblogs.com/123-123/p/5572815.html
Copyright © 2020-2023  润新知