题目链接: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. luoguP2123 皇后游戏——微扰法的应用与排序传递性的证明

    题目背景 还记得 NOIP 2012 提高组 Day1 的国王游戏吗?时光飞逝,光阴荏苒,两年 过去了.国王游戏早已过时,如今已被皇后游戏取代,请你来解决类似于国王游 戏的另一个问题. 题目描述 皇后 ...

  2. (转)Tomcat version 7.0 only supports J2EE 1.2, 1.3, 1.4, and Java EE 5 and 6 Web mod

    背景:在sts中导入web项目,却不能通过sts中的tomcat来加载该服务,并报出如下错误. “Tomcat version 7.0 only supports J2EE 1.2, 1.3, 1.4 ...

  3. 【堆的启发式合并】【P5290】[十二省联考2019]春节十二响

    Description 给定一棵 \(n\) 个节点的树,点有点权,将树的节点划分成多个集合,满足集合的并集是树的点集,最小化每个集合最大点权之和. Limitation \(1~\leq~n~\le ...

  4. poi excel导入纯数字单元格显示科学计数法的处理

    POI读取Excel文件时,对纯数字单元格的处理   用POI读取Excel文件的时候,可能会遇到这样的问题:Excel文件中某一单元格中的数据为数字,例如12345678910123. 正常读取的话 ...

  5. Hystrix之Dashboard的常见问题

    Hystrix Dashboard (断路器:Hystrix 仪表盘)只监控一个实例,而Turbine监控多个实例,要使用Turbine必须使用Hystrix,因为Turbine是为了监控断路器的状态 ...

  6. R安装时出现辑程包不存在,解决方法

    R > -------------------------------------------------[解决方案]1.使用命令单独安装caret,安装的时间很长.install.packag ...

  7. 【Asp.net入门4-01】基本开发工具

  8. 初学SQL语句练习2

    -- 子查询 --单行子查询-- 查询出所有比雇员姓名为“CLARK ”工资高的员工的信息 SELECT * FROM EMP WHERE SAL > (SELECT SAL FROM EMP ...

  9. python【文件操作:读写文件】

    文件读写模式

  10. shell的父子进程

    2017年1月11日, 星期三 shell的父子进程   启动/执行方式: 当前shell:               #!/bin/bash 必须行首                        ...