zoukankan      html  css  js  c++  java
  • Codeforces Round #321 (Div. 2) D. Kefa and Dishes 状压DP

    D. Kefa and Dishes
     

    When Kefa came to the restaurant and sat at a table, the waiter immediately brought him the menu. There were n dishes. Kefa knows that he needs exactly m dishes. But at that, he doesn't want to order the same dish twice to taste as many dishes as possible.

    Kefa knows that the i-th dish gives him ai units of satisfaction. But some dishes do not go well together and some dishes go very well together. Kefa set to himself k rules of eating food of the following type — if he eats dish x exactly before dish y (there should be no other dishes between x and y), then his satisfaction level raises by c.

    Of course, our parrot wants to get some maximal possible satisfaction from going to the restaurant. Help him in this hard task!

    Input

    The first line of the input contains three space-separated numbers, nm and k (1 ≤ m ≤ n ≤ 18, 0 ≤ k ≤ n * (n - 1)) — the number of dishes on the menu, the number of portions Kefa needs to eat to get full and the number of eating rules.

    The second line contains n space-separated numbers ai, (0 ≤ ai ≤ 109) — the satisfaction he gets from the i-th dish.

    Next k lines contain the rules. The i-th rule is described by the three numbers xiyi and ci (1 ≤ xi, yi ≤ n0 ≤ ci ≤ 109). That means that if you eat dish xi right before dish yi, then the Kefa's satisfaction increases by ci. It is guaranteed that there are no such pairs of indexesi and j (1 ≤ i < j ≤ k), that xi = xj and yi = yj.

    Output

    In the single line of the output print the maximum satisfaction that Kefa can get from going to the restaurant.

    Examples
    input
    2 2 1
    1 1
    2 1 1
    output
    3
     
    Note

    In the first sample it is best to first eat the second dish, then the first one. Then we get one unit of satisfaction for each dish and plus one more for the rule.

    In the second test the fitting sequences of choice are 4 2 1 or 2 1 4. In both cases we get satisfaction 7 for dishes and also, if we fulfill rule 1, we get an additional satisfaction 5.

    题意:

      给你n个菜品,每个菜品有相应的满意度,让你从这n个中选择m个,其中有k个增加满意度的rule

      x,y,c,表示选择x,y并且x选择在y之前可以获得c的额外满意度

      问你在选择m个情况下最大的满意度是多少

    题解:

      看到nm的范围就是状态压缩了

      设定dp[i][j]表示在j状态下前i个物品的最大满意度

      枚举状态递推就好了

    #include <bits/stdc++.h>
    using namespace std;
    const int N = 20, M = 30005, mod = 1e9 + 7, inf = 0x3f3f3f3f;
    typedef long long ll;
    
    ll dp[19][(1<<19)],n,m,k,c[N][N],a[N];
    int main() {
        scanf("%I64d%I64d%I64d",&n,&m,&k);
        for(int i=0;i<n;i++) scanf("%I64d",&a[i]);
        for(int i=1;i<=k;i++) {
            ll x,y;
            scanf("%I64d%I64d",&x,&y);
            x--,y--;
            scanf("%I64d",&c[x][y]);
        }
        for(int i=0;i<n;i++) {
            dp[i][(1<<i)] = a[i];
        }
        for(int now=1;now<(1<<n);now++) {
            for(int i=0;i<n;i++) {
                if(!(now&(1<<i))) {
                    for(int j=0;j<n;j++) {
                        if(now&(1<<j)) {
                            dp[i][now|(1<<i)] = max( dp[i][now|(1<<i)], dp[j][now]+a[i]+c[j][i]);
                        }
                    }
                }
            }
        }
        ll ans = 0;
        for(int i=0;i<(1<<n);i++) {
            int cnt = 0;
            for(int j=0;j<n;j++) if(i&(1<<j)) cnt++;
            if(cnt==m) {
                for(int k=0;k<n;k++) ans=max(ans,dp[k][i]);
            }
        }
        cout<<ans<<endl;
    }
  • 相关阅读:
    FastApi 进阶
    flask为多个接口添加同一个拦截器的方法
    记一次flask上传文件返回200前端却504的问题
    Python在项目外更改项目内引用
    go mod 拉取私有仓库
    go跳出多层循环的几种方式
    Zap简单使用
    记一次Goroutine与wg导致的问题
    go判断字符串是否是IP地址
    SpringBoot的启动流程
  • 原文地址:https://www.cnblogs.com/zxhl/p/5422444.html
Copyright © 2011-2022 走看看