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这么小 ...
随机推荐
- HTML5商城开发二 通过位移实现拖动效果
1.效果 在该区域内,手按住拖动,该模块可上下滑动,至最顶或最底部,滑动出现空白区域将自动缩回
- How do I list the files in a directory?
原文地址:How do I list the files in a directory? You want a list of all the files, or all the files matc ...
- spring mvc4的日期/数字格式化、枚举转换
日期.数字格式化显示,是web开发中的常见需求,spring mvc采用XXXFormatter来处理,先看一个最基本的单元测试: package com.cnblogs.yjmyzz.test; i ...
- Alpha版本测试报告
请根据团队项目中软件的需求文档.功能规格说明书和技术规格说明书,写出软件的测试计划.测试过程和测试结果,并回答下述问题. 1. 在测试过程中发现了多少Bug? 2. 你是怎么进行场景测试(scenar ...
- CUDA2.3-原理之任意长度的矢量求和与用事件来测量性能
__global__ void add( int *a, int *b, int *c) { <span style="white-space:pre"> </s ...
- GWT-Dev-Plugin(即google web toolkit developer plugin)for Chrome的安装方法
如果你想要在Chrome中进行GWT调试,需要安装“gwt developer plugin for chrome”,但是普通安装模式下,会提示: This application is not su ...
- SQLite剖析之功能特性
SQLite是遵守ACID的轻型数据库引擎,它包含在一个相对较小的C库中.它是D.RichardHipp创建的公有领域项目.不像常见的客户端/服务器结构范例,SQLite引擎不是一个与程序通信的独立进 ...
- DateTime.Now.ToString() 用法
//2008年4月24日 System.DateTime.Now.ToString("D"); //2008-4-24 System.DateTime.Now.ToString(& ...
- 【瞎想】TDD与汉字;FDD与英语字母
我觉得TDD与汉字;FDD与英语字母他们之间有相似性. FDD的上行和下行用频率的不同来区分,TDD的上行和下行用相同的频率然后在同一时刻相差半个波长(对称频率).如果用维度数描述,FDD是1维的话, ...
- jQuery自定义插件
jQuery自定义插件 jQuery自定义插件按照功能分类,可以分为三类, 1>封装对象方法的插件,(也就是基于某个DOM元素的jQuery对象,局部的) 2>封装全局函数的插件, ( ...