华师一附中OI组

标题: Codeforce 580D Kefa and Dishes [打印本页]

作者: admin    时间: 2018-8-24 15:39
标题: Codeforce 580D Kefa and Dishes
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.
作者: admin    时间: 2018-8-24 15:43
dp[i][sta] 表示当前吃的菜是i,状态为sta的情况。
更新总答案的时候在更新sta的时候可以获得。
作者: 吴语林    时间: 2018-8-25 07:33
没用状压的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. }
复制代码


作者: 吴语林    时间: 2018-8-25 07:33
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. }
复制代码





欢迎光临 华师一附中OI组 (http://hsyit.cn/) Powered by Discuz! X3.2