题目链接: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)的更多相关文章

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

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

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

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

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

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

  4. codeforces 580D. Kefa and Dishes

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  5. Kefa and Dishes(CodeForces580D)【状态压缩DP】

    状态压缩DP裸题,比赛的时候没反应过来,进行了n次枚举起点的solve,导致超时. #include<cstdio> #include<iostream> #include&l ...

  6. Codeforces C. A Simple Task(状态压缩dp)

    题目描述:  A Simple Task time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  7. Codeforces 4538 (状态压缩dp)Little Pony and Harmony Chest

    Little Pony and Harmony Chest 经典状态压缩dp #include <cstdio> #include <cstring> #include < ...

  8. hoj2662 状态压缩dp

    Pieces Assignment My Tags   (Edit)   Source : zhouguyue   Time limit : 1 sec   Memory limit : 64 M S ...

  9. POJ 3254 Corn Fields(状态压缩DP)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4739   Accepted: 2506 Descr ...

随机推荐

  1. LGP4173残缺的字符串

    题解 由于有通配符,所以$kmp$失效了: 将通配符看成0,其余字符看成互不相同的数字,$A,B$串对应得到$a,b$数组; 定义: $f(p) = \sum_{i=0}^{m-1} a_{i}b_{ ...

  2. [POI2008] BLO

    link 试题分析 分两种情况考虑. 当此点不是割点是,答案是$2\times (n-1)$. 当是割点时,我们发现这个点把树分成了若干个联通块,只要两两相乘即可. #include<iostr ...

  3. cpplint

    Cpplint是一个Python脚本,作为一款开源免费的代码静态检测工具,Google也使用它作为自己的C++代码检测工具,也就是说,只要你的代码遵从Google C++代码规范,那么Cpplint将 ...

  4. 利用Confluence搭建企业Wiki

    Confluence安装与部署 下载安装包及破解包 安装包下载地址:https://www.atlassian.com/software/confluence/download-archives 破解 ...

  5. gitlab的备份与恢复与迁移

    一.gitlab的备份1.1 创建备份目录,并授权 1 2 3 4 [root@linux-node1 ~]# mkdir /data/backups/gitlab -p [root@linux-no ...

  6. Struts2框架基础概念总结

    一.struts2框架 Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的 ...

  7. 3.fIddler的使用

    https://blog.csdn.net/chaoyu168/article/details/51065644 https://blog.csdn.net/u013474436/article/de ...

  8. Java时间格式转换工具类

    把当前时间修改成指定时间 //把当前时间修改成指定时间 public String dateUtil(Integer seconds, String dateFormatPattern){ Date ...

  9. [转载]memset()的效率

    http://blog.csdn.net/hackbuteer1/article/details/7343189 void *memset(void *s, int ch, size_t n); 作用 ...

  10. 【Linux】VMware及VirtualBox网络配置

    在VMware或VirtualBox中,安装完linux系统,不能连到win7 具体配置,如下. 如上.