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 }
  • 相关阅读:
    asp.net将图片转成二进制存入数据库
    ionic2打包生成APK报错 Error: Could not find gradle wrapper within Android SDK. Might need to update your Android SDK. Looked here: D:AndroidSDK ools emplatesgradlewrapper
    'ionic' 不是内部或外部命令,也不是可运行的程序或批处理文件。
    ABP-vs2017执行Add-Migration出现的问题
    关闭页面时,弹出JS提示框提示是否关闭
    C#生成Bar Code Image
    MemoryStream转imageSource
    RadControls RadGridView 显示加载数据时间
    RadGridView 分页控件
    CRM2011弹出asp.net模态窗口关闭的问题
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/7775285.html
Copyright © 2011-2022 走看看