题目链接: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. 调用Android系统设置项

    Intent mIntent = new Intent(); ComponentName comp = new ComponentName("com.android.settings&quo ...

  2. LGP4577【JSOI2018】战争

    题解: 求出$A$ 和$-B$ 的$Minkowsiki$和再$O(logn)$判断一个点是否在凸包内: $Minkowsiki$的求法比较容易忘,要多多温习才可以: #include<bits ...

  3. Python 爬虫入门(二)—— IP代理使用

    上一节,大概讲述了Python 爬虫的编写流程, 从这节开始主要解决如何突破在爬取的过程中限制.比如,IP.JS.验证码等.这节主要讲利用IP代理突破. 1.关于代理 简单的说,代理就是换个身份.网络 ...

  4. lumen 使用 dingo API 在 phpunit 中 404 的解决方法, 以及鉴权问题

    1. phpunit.xml 中添加 dingo 相关配置 <env name="API_STANDARDS_TREE" value="x"/> & ...

  5. Python模拟登录cnblogs

    Python利用requests.Session对象模拟浏览器登录cnblogs request.Session对行可以跨请求的保持cookie,非常方便的用于模拟登录. cnblogs登录页面分析: ...

  6. 队列,event,multiprocess

    队列:queue queue is especially useful in threaded programming when information must be exchanged safel ...

  7. oracle进阶之分析函数

    本博客是自己在学习和工作途中的积累与总结,纯属经验之谈,仅供自己参考,也欢迎大家转载,转载时请注明出处. http://www.cnblogs.com/king-xg/p/6797119.html 分 ...

  8. Ubuntu下配置支持Windows访问的Samba共享

    一.安装Ubuntu samba服务器 $ sudo apt-get install samba $ sudo apt-get install smbclient # Linux客户端测试用 二.创建 ...

  9. 翻译: 星球生成 II

    翻译: 星球生成 II 本文翻译自Planet Generation - Part II 译者: FreeBlues 以下为译文: 概述 在前一章 我解释了如何为星球创建一个几何球体. 在本文中, 我 ...

  10. C++的一些不错开源框架,可以学习和借鉴

    from https://www.cnblogs.com/charlesblc/p/5703557.html [本文系外部转贴,原文地址:http://coolshell.info/c/c++/201 ...