zoukankan      html  css  js  c++  java
  • Kefa and Dishes CodeForces

    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.

    Example

    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[ S ][ i ]表示S状态下上一次吃的是i的情况下得到的最大值。

     1 typedef long long ll;            //忘加头文件
     2 
     3 const int maxn=(1<<18);
     4 
     5 int n,m,k;
     6 ll dp[maxn][20],a[20],map[20][20];
     7 
     8 void Solve(){
     9     for(int S=0;S<(1<<n);S++){
    10         //int num=__builtin_popcount(S);    不能在里面判断,会卡掉一些合理的状态!!!
    11         //if(num>=m) continue;
    12         for(int i=1;i<=n;i++){
    13             if(S&(1<<(i-1))){ 
    14                 if(S==(1<<(i-1))) dp[S][i]=a[i];
    15                 for(int j=1;j<=n;j++){
    16                     if(S&(1<<(j-1))) continue;
    17                     dp[S|(1<<(j-1))][j]=max(dp[S|(1<<(j-1))][j],dp[S][i]+a[j]+map[i][j]);
    18                 }
    19             } 
    20         }
    21     }
    22 }
    23 
    24 int main()
    25 {   cin>>n>>m>>k;
    26     for(int i=1;i<=n;i++) cin>>a[i];
    27     for(int i=1;i<=k;i++){
    28         ll u,v,w;
    29         cin>>u>>v>>w;
    30         map[u][v]=w;
    31     }
    32     if(n==1){ cout<<a[1]<<endl; return 0; }
    33     Solve();
    34     ll ans=0;
    35     for(int S=0;S<(1<<n);S++){
    36         int num=__builtin_popcount(S);
    37         if(num>m) continue;
    38         for(int i=1;i<=n;i++) ans=max(ans,dp[S][i]);
    39     }
    40     cout<<ans<<endl;
    41 }
  • 相关阅读:
    centos6系列更换阿里yum源
    javascript字符串方法学习汇总
    ORA-01439:要更改数据类型,则要修改的列必须为空
    Python之字典
    Python之元组
    Python之列表
    Tensorflow 快速学习
    Ubuntu 切换到root 授权失败
    受限玻尔兹曼机二
    Numpy 数组ndarray和常用函数速查
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/7775285.html
Copyright © 2011-2022 走看看