codeforces 321# D. Kefa and Dishes (状态压缩DP)

题目:http://codeforces.com/contest/580/problem/D

题意:有n种菜(每一种菜有一个满意值ai>=0),你准备吃m种,每种一次。但是如果你按某种规则吃两种菜的话会增加额外的满意值,比如规则(xi yi ci)就是你先吃第xi个菜,然后马上吃第yi个菜,那么你就会额外增加ci点满意值。有k个这样的规则,问你吃m种菜后的最大满意值是多少。

分析:定义dp[i][j],i是bitmask,第x位为1代表选了第x种菜,j代表最后一次选的菜的下标,dp[i][j]是当前状态为(bitmask,lastnum)的剩余状态的最优解。然后记忆化搜索就行了。与之前习惯性写的含义不一样,之前写的是到达当前状态所选物品的最优解,这样肯定会错,因为和已选物品的先后顺序有关,假如求出dp[b][l],下次在到达这个状态(b,l)就直接返回了先前的那个可能更差的解。

错误代码:

long long DFS(int bitmask,int lastnum,long long val)
{
    int c=cal(bitmask);
    if(c>=m)
    {
        if(val>ans)
            ans=val;
        return val; 
    }
    if(dp[bitmask][lastnum]!=-1)
        return dp[bitmask][lastnum];
    long long ret=-1;
    for(int i=0;i
ac代码:

#include 
#include 
#include 
#include 
using namespace std;
int n,m,k;
long long v[20],buf[20][20];
long long ans,dp[1<<19][20];

long long DFS(int bitmask,int lastnum,int p)
{
	if(dp[bitmask][lastnum]!=-1)
		return dp[bitmask][lastnum];
	if(p==m)
		return 0;
	long long ret=0;
	for(int i=0;i




D. Kefa and Dishes
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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, nm and k (1 ≤ m ≤ n ≤ 180 ≤ 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 xiyi and ci (1 ≤ xi, yi ≤ n0 ≤ 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 indexesi 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.

Sample test(s)
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.



你可能感兴趣的:(ACM-动态规划)