华师一附中OI组

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1626|回复: 3
打印 上一主题 下一主题

Codeforce 580D Kefa and Dishes

[复制链接]

738

主题

1485

帖子

5422

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
5422
跳转到指定楼层
楼主
发表于 2018-8-24 15:39:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
outputstandard output
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
inputCopy
2 2 1
1 1
2 1 1
outputCopy
3
inputCopy
4 3 2
1 2 3 4
2 1 5
3 4 2
outputCopy
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.
回复

使用道具 举报

738

主题

1485

帖子

5422

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
5422
沙发
 楼主| 发表于 2018-8-24 15:43:37 | 只看该作者
dp[i][sta] 表示当前吃的菜是i,状态为sta的情况。
更新总答案的时候在更新sta的时候可以获得。
回复 支持 反对

使用道具 举报

2

主题

105

帖子

306

积分

中级会员

Rank: 3Rank: 3

积分
306
板凳
发表于 2018-8-25 07:33:02 | 只看该作者
没用状压的WA代码(能过部分点)

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <map>
  6. #include <cstdlib>
  7. #include <algorithm>
  8. #include <queue>
  9. #include <stack>
  10. using namespace std;
  11. int f[20][20],a[20],pri[20][20],book[20][20][20],n,m,k,x,y,p,ans=-0x3f3f3f3f;
  12. int main()
  13. {
  14.         scanf("%d%d%d",&n,&m,&k);
  15.     for(int i=1;i<=n;i++)
  16.             scanf("%d",&a[i]);
  17.     for(int i=1;i<=k;i++)
  18.     {
  19.             scanf("%d%d%d",&x,&y,&p);
  20.             pri[x][y]=p;
  21.         }
  22.         for(int i=1;i<=n;i++)
  23.                 f[1][i]=a[i],book[1][i][i]=1;
  24.         for(int i=2;i<=m;i++)
  25.                 for(int j=1;j<=n;j++)
  26.                         for(int k=1;k<=n;k++)
  27.                                 if(!book[i-1][j][k])
  28.                                         if(f[i][k]<f[i-1][j]+pri[j][k]+a[k])
  29.                                         {
  30.                                                 for(int l=1;l<=n;l++)
  31.                                                         book[i][k][l]=book[i-1][j][l];
  32.                                                 book[i][k][k]=1;
  33.                                                 f[i][k]=f[i-1][j]+pri[j][k]+a[k];
  34.                                         }
  35.         for(int i=1;i<=n;i++)
  36.                 ans=max(ans,f[m][i]);
  37.         printf("%d",ans);
  38.     return 0;
  39. }
复制代码

回复 支持 反对

使用道具 举报

2

主题

105

帖子

306

积分

中级会员

Rank: 3Rank: 3

积分
306
地板
发表于 2018-8-25 07:33:49 | 只看该作者
AC代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <map>
  6. #include <cstdlib>
  7. #include <algorithm>
  8. #include <queue>
  9. #include <stack>
  10. using namespace std;
  11. const int maxn=(1<<18)+10;
  12. long long f[maxn][20],a[20],pri[20][20],n,m,k,u,v,w,ans=0;
  13. int main()
  14. {
  15.    
  16.     cin>>n>>m>>k;
  17.     for(int i=0;i<n;i++)
  18.             cin>>a[i];
  19.     for(int i=0;i<k;i++)
  20.     {
  21.         cin>>u>>v>>w;
  22.         pri[u-1][v-1]=w;
  23.     }
  24.     for(int i=0;i<n;i++)
  25.         f[1<<i][i]=a[i];
  26.     int big=1<<n;
  27.     for(int s=0;s<big;s++)
  28.     {
  29.         int cnt=0;
  30.         for (int i=0;i<n;i++)
  31.             if ((s&(1<<i))!=0)
  32.             {
  33.                 cnt++;
  34.                 for(int j=0;j<n;j++)
  35.                     if (!(s&(1<<j)))
  36.                     {
  37.                         int ns=s|(1<<j);
  38.                         f[ns][j]=max(f[ns][j],f[s][i]+a[j]+pri[i][j]);
  39.                     }
  40.             }
  41.         if(cnt==m)
  42.             for (int i=0;i<n;i++)
  43.                     ans=max(ans,f[s][i]);
  44.     }
  45.     cout<<ans;
  46.     return 0;
  47. }
复制代码
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|服务支持:DZ动力|华师一附中OI组  

GMT+8, 2024-12-26 13:41 , Processed in 0.099814 second(s), 25 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表