Codeforces 580D Kefa and Dishes(状态压缩DP)
题目链接:http://codeforces.com/problemset/problem/580/D
题目大意:
有n盘菜每个菜都有一个满意度,k个规则,每个规则由x y c组成,表示如果再y之前吃x那么满意度会额外增加c,
现在凯迪想吃m盘菜,并且满意度最大,请求出满意度。
解题思路:
状压DP,设dp[i][j]表示在状态i并且最后一道菜放在位置j时的最大满意度。
注意要处理好一道菜时的情况,以及注意二进制表示中1的个数超过m的情况。
代码:
#include<bits/stdc++.h>
#define lc(a) (a<<1)
#define rc(a) (a<<1|1)
#define MID(a,b) ((a+b)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define clr(arr,val) memset(arr,val,sizeof(arr))
#define _for(i,start,end) for(int i=start;i<=end;i++)
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef long long LL;
const int N=;
const int INF=0x3f3f3f3f;
const double eps=1e-; LL ans,n,m,p;
LL val[N],mp[N][N],dp[<<][N];//dp[i][j]表示i的状态下最后一个盘子选择第j个的最优解 int main(){
FAST_IO;
cin>>n>>m>>p;
for(int i=;i<n;i++){
cin>>val[i];
}
for(int i=;i<=p;i++){
int x,y,c;
cin>>x>>y>>c;
mp[x-][y-]=c;
}
memset(dp,-,sizeof(dp));
ans=;
int lim=(<<n);
for(int i=;i<lim;i++){
int cnt=;
for(int j=;j<n;j++){
int tmp=(<<j);
if(tmp&i)
cnt++;
}
//点菜数不能>m
if(cnt>m) continue;
for(int j=;j<n;j++){
int tmp=(<<j);
if(tmp&i){
int pre=i-tmp;
//单个菜的时候没有前一个菜所以直接赋值,否则会被0 x类型的rule影响
if(cnt==)
dp[i][j]=val[j];
else{
for(int k=;k<n;k++){
if(dp[pre][k]==-) continue;
dp[i][j]=max(dp[i][j],dp[pre][k]+mp[k][j]+val[j]);
}
}
ans=max(dp[i][j],ans);
}
}
}
cout<<ans<<endl;
return ;
}
Codeforces 580D Kefa and Dishes(状态压缩DP)的更多相关文章
- codeforces 580D Kefa and Dishes(状压dp)
题意:给定n个菜,每个菜都有一个价值,给定k个规则,每个规则描述吃菜的顺序:i j w,按照先吃i接着吃j,可以多增加w的价值.问如果吃m个菜,最大价值是多大.其中n<=18 思路:一看n这么小 ...
- dp + 状态压缩 - Codeforces 580D Kefa and Dishes
Kefa and Dishes Problem's Link Mean: 菜单上有n道菜,需要点m道.每道菜的美味值为ai. 有k个规则,每个规则:在吃完第xi道菜后接着吃yi可以多获得vi的美味值. ...
- Codeforces 580D Kefa and Dishes(状压DP)
题目大概说要吃掉n个食物里m个,吃掉各个食物都会得到一定的满意度,有些食物如果在某些食物之后吃还会增加满意度,问怎么吃满意度最高. dp[S][i]表示已经吃掉的食物集合是S且刚吃的是第i个食物的最大 ...
- codeforces 580D. Kefa and Dishes
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- Kefa and Dishes(CodeForces580D)【状态压缩DP】
状态压缩DP裸题,比赛的时候没反应过来,进行了n次枚举起点的solve,导致超时. #include<cstdio> #include<iostream> #include&l ...
- Codeforces C. A Simple Task(状态压缩dp)
题目描述: A Simple Task time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces 4538 (状态压缩dp)Little Pony and Harmony Chest
Little Pony and Harmony Chest 经典状态压缩dp #include <cstdio> #include <cstring> #include < ...
- hoj2662 状态压缩dp
Pieces Assignment My Tags (Edit) Source : zhouguyue Time limit : 1 sec Memory limit : 64 M S ...
- POJ 3254 Corn Fields(状态压缩DP)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4739 Accepted: 2506 Descr ...
随机推荐
- Ubuntu中Android SDK Manager无法更新解决办法
Ubuntu中Android SDK Manager无法更新解决办法http://hi.baidu.com/petercao2008/item/d7a64441f04668e81e19bc1a
- 解题:POI 2011 Strongbox
首先洛谷的题面十分的劝退(至少对我这个菜鸡来说是这样),我来解释一下(原来的英文题面): 有一个有若干个密码(每个密码都可以开箱子)的密码箱,密码是在$0$到$n-1$的数中的,且所有的密码都满足一个 ...
- 2017 3 8 练习赛 t3 路径规划
题目大意是让你在一棵树上找到一条路径使得(路径边权和*路径最小值) 最大. 这道题有两种方法. 1.点分治,考虑过重心的每条路径是否可能成为答案,枚举从根出发的每一条路径中的最小值等于总路径的最小值, ...
- python【数据类型:字典】
字典的定义 infos = {'name':'张晓红','sex':'女','address':'上海','age':18} stus = {"name":"张三&quo ...
- np.diff函数
np.diff函数 觉得有用的话,欢迎一起讨论相互学习~Follow Me 数组中a[n]-a[n-1] import numpy as np a=np.array([1, 6, 7, 8, 12]) ...
- 转:实现OC与JS的简易交互
oc-->js stringByEvaluatingJavaScriptFromString,其参数是一NSString 字符串内容是js代码(这又可以是一个js函数.一句js代码或他们的组合) ...
- vue 脚手架使用
1. npm指令 vue init 模板类型 项目名称 如: vue init webpack-simple mydemo 2.进入刚才生产的 文件夹 mydemo cd mydemo 3.初始化 ...
- CF&&CC百套计划1 Codeforces Round #449 B. Ithea Plays With Chtholly
http://codeforces.com/contest/896/problem/B 题意: 交互题 n张卡片填m个1到c之间的数,1<=n*ceil(c/2)<=m 最后填出一个单调非 ...
- Nginx模块Lua-Nginx-Module学习笔记(二)Lua指令详解(Directives)
源码地址:https://github.com/Tinywan/Lua-Nginx-Redis Nginx与Lua编写脚本的基本构建块是指令. 指令用于指定何时运行用户Lua代码以及如何使用结果. 下 ...
- 高维数据降维 国家自然科学基金项目 2009-2013 NSFC Dimensionality Reduction
2013 基于数据降维和压缩感知的图像哈希理论与方法 唐振军 广西师范大学 多元时间序列数据挖掘中的特征表示和相似性度量方法研究 李海林 华侨大学 基于标签和多特征融合的图像语义空间学习技 ...