zoukankan      html  css  js  c++  java
  • codeforces 580d 状压DP

    题意:有n种菜,现在选m种菜来吃,如果在吃y的前一道菜是x的话,那么就可以获得额外满意度。每一种菜都有一个满意度。

    思路:设dp[i][S]表示为最后一道菜为i,现在的菜吃的状态为S。S中的二进位如果为1表示已经吃了,如果是0则表示没吃,状压DP,答案就出了。

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <iostream>
    #include <algorithm>
    #include <map>
    #include <vector>
    #include <queue>
    using namespace std;
    #define EPS 1e-10
    typedef long long ll;
    const int maxn = 1<<20;
    const int INF = 1e9 ;
    const double eps = 1e-8;
    const int mod = 2520;
    int gra[20][20],a[20],num[20],cnt;
    ll dp[maxn][20];
    int cal(int u){
        cnt = 0;
        int cntp = 0;
        while(u > 0){
            if(u % 2 == 1) num[++cnt] = cntp;
            cntp++;
            u /= 2;
        }
        return cnt;
    }
    int main(){
      //  freopen("in.txt","r",stdin);
        int n,m,k;
        scanf("%d%d%d",&n,&m,&k);
        for(int i = 1;i <= n;i++)
            scanf("%d",&a[i]);
        int se = (1<<n) - 1;
        int a1,b,c;
        for(int i = 1;i <= k;i++){
            scanf("%d%d%d",&a1,&b,&c);
            gra[a1][b] = c;
        }
        ll smax = 0;
    
        for(int i = 1;i <= se;i++){
            if(cal(i) > m) continue;
            ll sum = 0;
            for(int j = 1;j <= cnt;j++){
                int posj = num[j];
               // cout<<posj+1<<" ";
               // int posj = num[j];
               // dp[i][posj] = sum;
                for(int k = 1;k <= cnt;k++){
                    if(k == j&&cnt != 1) continue;
                    int posk = num[k];
                    dp[i][posj] = max(dp[i][posj],dp[i-(1<<posj)][posk] + gra[posk+1][posj+1] + a[posj+1]);
                   // cout<<posk+1<<" "<<dp[i][posj]<<" "<<"b";
                }
    
               // cout<<endl;
                smax = max(smax,dp[i][posj]);
            }
        }
        printf("%I64d
    ",smax);
        return 0;
    }
  • 相关阅读:
    ugui点击穿透判断
    c#字符串代码,动态创建编译器
    github项目分享
    unity 2d 版的lookAt
    unity全屏截图
    shader例子
    AcWing 329. 围栏障碍训练场
    AcWing 326. XOR和路径
    AcWing 324. 贿赂FIPA
    AcWing 322. 消木块
  • 原文地址:https://www.cnblogs.com/shimu/p/5917868.html
Copyright © 2011-2022 走看看