580D
来源:互联网 发布:企业号oa系统源码 编辑:程序博客网 时间:2024/05/16 08:03
When Kefa came to the restaurant and sat at a table, the waiter immediately brought him the menu. There weren dishes. Kefa knows that he needs exactlym 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 himai units of satisfaction. But some dishes do not go well together and some dishes go very well together. Kefa set to himselfk rules of eating food of the following type — if he eats dishx exactly before dish y (there should be no other dishes between x and y), then his satisfaction level raises byc.
Of course, our parrot wants to get some maximal possible satisfaction from going to the restaurant. Help him in this hard task!
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 numbersai, (0 ≤ ai ≤ 109) — the satisfaction he gets from thei-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 dishxi right before dishyi, then the Kefa's satisfaction increases byci. It is guaranteed that there are no such pairs of indexesi and j (1 ≤ i < j ≤ k), thatxi = xj andyi = yj.
In the single line of the output print the maximum satisfaction that Kefa can get from going to the restaurant.
2 2 11 12 1 1
3
4 3 21 2 3 42 1 53 4 2
12
有n中菜肴,每种菜肴都有一个愉悦度,同时制定k个规则,当吃了第i中菜的下一道菜吃第j种菜则愉悦度增加g[i][j]。问你吃m种菜可获得的最大愉悦度。
dp[i][j]表示当前状态为i且最后吃第j种菜的最大愉悦度,则递推公式为dp[i | (1 << k)][k] = max(dp[i | (1 << k)][k], dp[i][j] + a[k] + g[j][k])。
#include<stdio.h>#include<algorithm>#include<string.h>using namespace std;const int maxm = 1 << 20;#define ll long longll dp[maxm][25], g[25][25], a[maxm];int main(){int n, i, j, k, m, x, y, z, cnt, flag;ll ans;scanf("%d%d%d", &n, &m, &k);memset(dp, -1, sizeof(dp));for (i = 0;i < n;i++)scanf("%lld", &a[i]);for (i = 1;i <= k;i++){scanf("%d%d%d", &x, &y, &z);x--, y--;g[x][y] = z;}for (i = 0;i < n;i++)dp[1 << i][i] = a[i];ans = 0;for (i = 0;i < (1 << n);i++){ll now = i;cnt = 0;flag = 0;while (now){if (now & 1) cnt++;now >>= 1;}if (cnt == m) flag = 1;for (j = 0;j < n;j++){if (dp[i][j] == -1) continue;if (flag) ans = max(ans, dp[i][j]);for (k = 0;k < n;k++){if (i&(1 << k)) continue;dp[i | (1 << k)][k] = max(dp[i | (1 << k)][k], dp[i][j] + a[k] + g[j][k]);}}}printf("%lld\n", ans);return 0;}
- 580D
- codeforces 580D
- d
- d
- d
- d
- d
- D
- d
- d
- d
- d
- d
- %d
- d
- d
- d
- D
- CSS-浮动&清除浮动
- TextView实现中间文字两侧图片的样式 和 左侧图片右侧文字
- 老板不给涨工资—加薪三十七计
- Caffe源文件解释和依赖项
- java基础复习--复习总结2
- 580D
- 传文件到linux
- 【Linux C 多线程编程】互斥锁与条件变量
- 实现二级联动
- The JRE could not be found.Edit the server and change the JRE location.
- 现阶段我国大数据共享面临的问题
- list集合删除其中一个后,设置下一个的方法
- ES6模板字面量
- java基础复习--复习总结3