• codeforces 580D:Kefa and Dishes


    Description

    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, n, m 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 xi, yi and ci (1 ≤ xi, yi ≤ n, 0 ≤ 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 indexes i 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
    Input
    4 3 2
    1 2 3 4
    2 1 5
    3 4 2
    Output
    12
    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.

    正解:状压DP

    解题报告:

      直接状压,f[i][S]表示刚吃完i后状态为S的最大值,然后直接转就可以了。

      so easy

     1 //It is made by jump~
     2 #include <iostream>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <cstdio>
     6 #include <cmath>
     7 #include <algorithm>
     8 #include <ctime>
     9 #include <vector>
    10 #include <queue>
    11 #include <map>
    12 #include <set>
    13 using namespace std;
    14 typedef long long LL;
    15 const int inf = (1<<30);
    16 const int MAXN = 19;
    17 const int MAXS = (1<<18);
    18 int n,m,k;
    19 LL a[MAXN],ans;
    20 LL f[MAXN][MAXS];//f[i][S]表示刚吃完i后状态为S的最大值
    21 LL w[MAXN][MAXN];
    22 
    23 inline int getint()
    24 {
    25     int w=0,q=0; char c=getchar();
    26     while((c<'0' || c>'9') && c!='-') c=getchar(); if(c=='-') q=1,c=getchar(); 
    27     while (c>='0' && c<='9') w=w*10+c-'0', c=getchar(); return q ? -w : w;
    28 }
    29 
    30 inline int count(int x){ int total=0; while(x) total+=x&1,x>>=1; return total; }
    31 
    32 inline void work(){
    33     n=getint(); m=getint(); k=getint(); int x,y; for(int i=1;i<=n;i++) a[i]=getint();
    34     for(int i=1;i<=k;i++) x=getint(),y=getint(),w[x][y]=getint(); int end=(1<<n)-1;
    35     for(int i=1;i<=n;i++) f[i][(1<<(i-1))]=a[i];
    36     for(int i=1;i<=end;i++) {
    37     for(int j=1;j<=n;j++) {
    38         if(( i|(1<<(j-1)) )!=i) continue;
    39         for(int to=1;to<=n;to++) {
    40         if(to==j) continue; if(( i|(1<<(to-1)) )==i) continue;
    41         f[to][i|(1<<(to-1))]=max(f[to][i|(1<<(to-1))],f[j][i]+a[to]+w[j][to]);
    42         }
    43     }
    44     }
    45     for(int i=1;i<=end;i++) if(count(i)==m) for(int j=1;j<=n;j++) ans=max(ans,f[j][i]);
    46     printf("%lld",ans);
    47 }
    48 
    49 int main()
    50 {
    51     work();
    52     return 0;
    53 }
  • 相关阅读:
    ie不支持 event.stopPropagation 的解决方式
    jquery 获取 元素.text( ) 里面的值 需要进行去空格去换行符操作
    使用插件实现某元素展示特定行文字,超出显示...
    css文字超出显示省略号
    纯css实现各种三角形图案
    jQuery判断鼠标是否在某个元素内
    z-tree相关设置
    js提取字符串中的汉字
    扩展ie中不兼容的startsWith,endsWith方法
    获取浏览器URL携带过来的参数/获取url中"?"符后的字串
  • 原文地址:https://www.cnblogs.com/ljh2000-jump/p/5933168.html
Copyright © 2020-2023  润新知