您的位置:首页 > 其它

CF - 580D. Kefa and Dishes - 状态压缩DP

2017-05-06 14:54 393 查看
1.题目描述:

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, 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 iand 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.

2.题意概述:

给出n个物品,选出m个,如果有两个物品存在一条有向边,那么获得这条边的边权,每个点有一个点权,问如何选取能获得最多的权值。

3.解题思路:

考虑二维DP解决问题。 第一维是已经吃过的菜肴的状态,第二个是当前状态下最后一个吃的菜。 因为n较小,考虑第一维用状态压缩(其中1 << i表示吃第i道菜)。 对于每一个状态,枚举倒数第二个吃的是哪一道菜,再根据这个菜去枚举它的边,更新这个状态下的最大值。 当吃了m道菜以后,最后的答案就是DP的所有m状态的最大值。复杂度 O(2n * n2).
4.AC代码:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define maxn 100100
#define lson root << 1
#define rson root << 1 | 1
#define lent (t[root].r - t[root].l + 1)
#define lenl (t[lson].r - t[lson].l + 1)
#define lenr (t[rson].r - t[rson].l + 1)
#define N 1111
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
typedef unsigned long long ull;
int a[19], mp[20][20];
ll dp[1 << 19][20];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
int n, m, k;
while (~scanf("%d%d%d", &n, &m, &k))
{
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
for (int i = 0; i < k; i++)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
mp[a - 1][b - 1] = c;
}
memset(dp, 0, sizeof(dp));
for (int i = 0; i < n; i++)
dp[1 << i][i] = a[i];
int tot = 1 << n;
ll ans = 0;
for (int s = 0; s < tot; s++) //枚举state
{
int cnt = 0;
for (int i = 0; i < n; i++) // 枚举先吃i再吃j
{
if ((s & 1 << i) == 0)
continue;
cnt++;
for (int j = 0; j < n; j++)
{
if (s & 1 << j) // 在i之前已经吃了
continue;
int state = s | 1 << j;
dp[state][j] = max(dp[state][j], dp[s][i] + mp[i][j] + a[j]);
}
}
if (cnt == m)	//吃够了m盘菜
for (int i = 0; i < n; i++)
if (s & 1 << i)
ans = max(ans, dp[s][i]);
}
printf("%lld\n", ans);
}
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms.", _end_time - _begin_time);
#endif
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  acm codeforces