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 ≤ 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 xiyi 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 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.

这题可以用状压dp做,用dp[state][j]表示取了state里的1的这些菜,最后取的菜是j最多能得到的价值。状态转移方程是dp[state1][j]=max(dp[state1][j],dp[state][i]+a[i][j]+value[j] );

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef __int64 ll;
#define inf 99999999
#define pi acos(-1.0)
ll dp[280000][20],value[20];
ll a[20][20]; int panduan(int state,int m)
{
int tot=0;
while(state){
if(state&1)tot++;
state>>=1;
}
if(tot==m)return 1;
return 0;
}
int main()
{
int i,j,c,d,k,n,m,state,state1;
ll e;
while(scanf("%d%d%d",&n,&m,&k)!=EOF)
{
memset(dp,0,sizeof(dp));
memset(a,0,sizeof(a));
for(i=1;i<=n;i++){
scanf("%I64d",&value[i]);
}
for(i=1;i<=k;i++){
scanf("%d%d%I64d",&c,&d,&e);
a[c][d]=e;
}
ll maxnum=0;
if(m==1){
for(i=1;i<=n;i++){
maxnum=max(maxnum,value[i]);
}
printf("%I64d\n",maxnum);
continue;
} for(state=1;state<=(1<<n)-1;state++){
for(i=1;i<=n;i++){
if(state&(1<<(i-1))){
if(state==(1<<(i-1) )){
dp[state][i]=value[i];
}
for(j=1;j<=n;j++){
if( (state&(1<<(j-1) ) )==0){
state1=( state|(1<<(j-1)) );
dp[state1][j]=max(dp[state1][j],dp[state][i]+a[i][j]+value[j] );
}
} }
}
} maxnum=0;
for(state=1;state<=(1<<n)-1;state++){
if(panduan(state,m)){
for(i=1;i<=n;i++){
maxnum=max(maxnum,dp[state][i]);
}
}
}
printf("%I64d\n",maxnum);
}
return 0;
}

codeforces 580D. Kefa and Dishes的更多相关文章

  1. dp + 状态压缩 - Codeforces 580D Kefa and Dishes

    Kefa and Dishes Problem's Link Mean: 菜单上有n道菜,需要点m道.每道菜的美味值为ai. 有k个规则,每个规则:在吃完第xi道菜后接着吃yi可以多获得vi的美味值. ...

  2. Codeforces 580D Kefa and Dishes(状态压缩DP)

    题目链接:http://codeforces.com/problemset/problem/580/D 题目大意:有n盘菜每个菜都有一个满意度,k个规则,每个规则由x y c组成,表示如果再y之前吃x ...

  3. Codeforces 580D Kefa and Dishes(状压DP)

    题目大概说要吃掉n个食物里m个,吃掉各个食物都会得到一定的满意度,有些食物如果在某些食物之后吃还会增加满意度,问怎么吃满意度最高. dp[S][i]表示已经吃掉的食物集合是S且刚吃的是第i个食物的最大 ...

  4. codeforces 580D Kefa and Dishes(状压dp)

    题意:给定n个菜,每个菜都有一个价值,给定k个规则,每个规则描述吃菜的顺序:i j w,按照先吃i接着吃j,可以多增加w的价值.问如果吃m个菜,最大价值是多大.其中n<=18 思路:一看n这么小 ...

  5. Codeforces Round #321 (Div. 2) D. Kefa and Dishes 状压dp

    题目链接: 题目 D. Kefa and Dishes time limit per test:2 seconds memory limit per test:256 megabytes 问题描述 W ...

  6. 「日常训练」Kefa and Dishes(Codeforces Round #321 Div. 2 D)

    题意与分析(CodeForces 580D) 一个人有\(n\)道菜,然后要点\(m\)道菜,每道菜有一个美味程度:然后给你了很多个关系,表示如果\(x\)刚好在\(y\)前面做的话,他的美味程度就会 ...

  7. [Codeforces 580D]Fizzy Search(FFT)

    [Codeforces 580D]Fizzy Search(FFT) 题面 给定母串和模式串,字符集大小为4,给定k,模式串在某个位置匹配当且仅当任意位置模式串的这个字符所对应的母串的位置的左右k个字 ...

  8. codeforces 580D:Kefa and Dishes

    Description When Kefa came to the restaurant and sat at a table, the waiter immediately brought him ...

  9. Codeforces Round #321 (Div. 2) D. Kefa and Dishes(状压dp)

    http://codeforces.com/contest/580/problem/D 题意: 有个人去餐厅吃饭,现在有n个菜,但是他只需要m个菜,每个菜只吃一份,每份菜都有一个欢乐值.除此之外,还有 ...

随机推荐

  1. 按装parallels tool的失败之路

    这是一篇对于其他人来说没什么意义的博客.单纯的可以被看作是日记. 首先,我想安装parallels tool. 但是照着网上很多教程(如www.cnblogs.com/artwalker/p/1323 ...

  2. SQL注入-流程

    一般注入分类: 时间,布尔,报错,堆,联合 有关函数介绍: current_user() 当前用户名 session_user() 链接数据库的用户名 @@basedir mysql安装路径 @@da ...

  3. Python+Docker+Flask+pyecharts实现数据可视化

    1.数据加工pyecharts图实现: 数据源:本地CSV文件 ps:由于是跟生产环境做交互,生产环境指标由HSQL加工,使用存储过程挂后台定时运行,后使用python实现导出及定时分发,本地pyth ...

  4. 【高级排序算法】2、归并排序法的实现-Merge Sort

    简单记录 - bobo老师的玩转算法系列–玩转算法 -高级排序算法 Merge Sort 归并排序 Java实现归并排序 SortTestHelper 排序测试辅助类 package algo; im ...

  5. 【Linux】如何查看命令来源于哪个包

    Debian:(Ubuntu等) 先安装apt-file sudo apt-get install -y apt-file apt-file update 查询命令:(已查询ifconfig为例) r ...

  6. 【Oracle】Oracle中chr()的含义

    oracle中chr含义 CHR(10)和 CHR(13)--在oracle都为换行 chr(32)--表示空格 DECLARE v_a VARCHAR2(255); v_b VARCHAR2(255 ...

  7. 【ORA】ORA-01756: quoted string not properly terminated

    出现ORA-01756: quoted string not properly terminated 后,查看SQL是否有中文符号 修改为英文的符号,运行正常

  8. ASP.NET Core错误处理中间件[3]: 异常处理器

    DeveloperExceptionPageMiddleware中间件错误页面可以呈现抛出的异常和当前请求上下文的详细信息,以辅助开发人员更好地进行纠错诊断工作.ExceptionHandlerMid ...

  9. 爬虫+django,打造个性化API接口

    简述 今天也是同事在做微信小程序的开发,需要音乐接口的测试,可是用网易云的开放接口比较麻烦,也不能进行测试,这里也是和我说了一下,所以就用爬虫写了个简单网易云歌曲URL的爬虫,把数据存入mysql数据 ...

  10. ABP vNext 实现租户Id自动赋值插入

    背景 在使用ABP vNext过程中,因为我们的用户体系庞大,所以一直与其他业务同时开发,在开发其他业务模块时,我们一直存在着误区:认为ABP vNext 自动处理了数据新增时的租户Id(Tenant ...