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 }

 

转载于:https://www.cnblogs.com/ljh2000-jump/p/5933168.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值