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!
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.
In the single line of the output print the maximum satisfaction that Kefa can get from going to the restaurant.
2 2 1
1 1
2 1 1
3
4 3 2
1 2 3 4
2 1 5
3 4 2
12
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
//It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
const int inf = (<<);
const int MAXN = ;
const int MAXS = (<<);
int n,m,k;
LL a[MAXN],ans;
LL f[MAXN][MAXS];//f[i][S]表示刚吃完i后状态为S的最大值
LL w[MAXN][MAXN]; inline int getint()
{
int w=,q=; char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar(); if(c=='-') q=,c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar(); return q ? -w : w;
} inline int count(int x){ int total=; while(x) total+=x&,x>>=; return total; } inline void work(){
n=getint(); m=getint(); k=getint(); int x,y; for(int i=;i<=n;i++) a[i]=getint();
for(int i=;i<=k;i++) x=getint(),y=getint(),w[x][y]=getint(); int end=(<<n)-;
for(int i=;i<=n;i++) f[i][(<<(i-))]=a[i];
for(int i=;i<=end;i++) {
for(int j=;j<=n;j++) {
if(( i|(<<(j-)) )!=i) continue;
for(int to=;to<=n;to++) {
if(to==j) continue; if(( i|(<<(to-)) )==i) continue;
f[to][i|(<<(to-))]=max(f[to][i|(<<(to-))],f[j][i]+a[to]+w[j][to]);
}
}
}
for(int i=;i<=end;i++) if(count(i)==m) for(int j=;j<=n;j++) ans=max(ans,f[j][i]);
printf("%lld",ans);
} int main()
{
work();
return ;
}
codeforces 580D:Kefa and Dishes的更多相关文章
- Codeforces 580B: Kefa and Company(前缀和)
http://codeforces.com/problemset/problem/580/B 题意:Kefa有n个朋友,要和这n个朋友中的一些出去,这些朋友有一些钱,并且和Kefa有一定的友谊值,要求 ...
- codeforces#580 D. Kefa and Dishes(状压dp)
题意:有n个菜,每个菜有个兴奋值,并且如果吃饭第i个菜立即吃第j个菜,那么兴奋值加ma[i][j],求吃m个菜的最大兴奋值,(n<=18) 分析:定义dp[status][last],statu ...
- dp + 状态压缩 - Codeforces 580D Kefa and Dishes
Kefa and Dishes Problem's Link Mean: 菜单上有n道菜,需要点m道.每道菜的美味值为ai. 有k个规则,每个规则:在吃完第xi道菜后接着吃yi可以多获得vi的美味值. ...
- 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 ...
- 「日常训练」Kefa and Dishes(Codeforces Round #321 Div. 2 D)
题意与分析(CodeForces 580D) 一个人有\(n\)道菜,然后要点\(m\)道菜,每道菜有一个美味程度:然后给你了很多个关系,表示如果\(x\)刚好在\(y\)前面做的话,他的美味程度就会 ...
- [Codeforces 580D]Fizzy Search(FFT)
[Codeforces 580D]Fizzy Search(FFT) 题面 给定母串和模式串,字符集大小为4,给定k,模式串在某个位置匹配当且仅当任意位置模式串的这个字符所对应的母串的位置的左右k个字 ...
- Codeforces 580D Kefa and Dishes(状态压缩DP)
题目链接:http://codeforces.com/problemset/problem/580/D 题目大意:有n盘菜每个菜都有一个满意度,k个规则,每个规则由x y c组成,表示如果再y之前吃x ...
- codeforces 580D. Kefa and Dishes
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- codeforces 580D Kefa and Dishes(状压dp)
题意:给定n个菜,每个菜都有一个价值,给定k个规则,每个规则描述吃菜的顺序:i j w,按照先吃i接着吃j,可以多增加w的价值.问如果吃m个菜,最大价值是多大.其中n<=18 思路:一看n这么小 ...
随机推荐
- 3098: Hash Killer II
3098: Hash Killer II Time Limit: 5 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 1219 Solved: ...
- CoffeeScript的类继承的工具函数extends
__hasProp = {}.hasOwnProperty, __extends = function(child, parent) { // 派生类时,如果基类的类属性值是对象,那么子类的类属性只是 ...
- 使用mysqldump进行mysql数据库备份还原
mysqldump是mysql自带的备份还原工具,默认在安装目录的bin下 可通过cmd命令行启动,然后运行: 还原一个数据库: mysql -h 主机 -u 用户名 -p密码 数据库名 < 指 ...
- 优秀开源代码解读之JS与iOS Native Code互调的优雅实现方案
简介 本篇为大家介绍一个优秀的开源小项目:WebViewJavascriptBridge. 它优雅地实现了在使用UIWebView时JS与ios 的ObjC nativecode之间的互调,支持消息发 ...
- 生成短链(网址) ShortUrlLink
建表 CREATE TABLE [dbo].[ShortUrl]( [Id] [,) NOT NULL, [LongUrl] [nvarchar]() NOT NULL, [BaseUri] [int ...
- Windows去除快捷箭头
美化桌面 bat代码: reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shel ...
- The superclass javax.servlet.http.HttpServlet was not found on the Java Build Path。
项目上右键-->Build Path-->Configuration Build Path -->Add Library -->Server Runtime 选择tomcat
- Bete冲刺第一阶段
Bete冲刺第一阶段 今日工作: github团队协作流程 web:调整dao层设计,增加新的dao组件 客户端:之前遗留的界面跳转的BUG 目前所遇问题: 第一,COCOAPODS的安装上还是有点问 ...
- HIbernate的对象状态
*临时状态对象: session中没有缓存,且在数据库中没有对应数据. User user1=new User(null,"c50",18); *持久化状态对象: session中 ...
- SPI协议及工作原理分析
说明.文章摘自:SPI协议及其工作原理分析 http://blog.csdn.net/skyflying2012/article/details/11710801 一.概述. SPI, Serial ...