题目链接: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 dumpsys命令详细使用

    一.dumpsys命令介绍 1.命令说明 Dumpsys用户系统诊断,它运行在设备上,并提供系统服务状态信息 命令格式: adb shell dumpsys [system serbices] 2.系 ...

  2. Python【logging】模块

    # 1.负责往控制台里面输出日志信息的 # 2.往日志文件里面写日志的,按天生成日志,清理日志 import logging from logging import handlers logger = ...

  3. Go_12:Go命令行处理

    概述 常用的命令行参数解析有 2 种方式,一种是不带参数标签直接接上参数值,另外一种就是带有标签的参数解析.第一种我们可以直接通过 os 包提供的原始方法获取,第二种我们需要通过 flag 包来解析获 ...

  4. IOS方形头像如何变成圆形

    方法一:直接使用UIView对应图层的cornerRadius self.layer.cornerRadius     = CGRectGetWidth(self.bounds)/2.f; self. ...

  5. UIViewController的初始化

    UIViewController的初始化一般有两个: -(instancetype)init; -(instancetype)initWithNibName:(NSString *)nibNameOr ...

  6. Tomcat假死排查方案

    使用Tomcat作为Web服务器的时候偶尔会遇到Tomcat停止响应的情况,通过netstat查看端口情况会发现tomcat的端口出现大量的CLOSE_WAIT,此时Tomcat会停止响应前端请求,同 ...

  7. 防止jquery ajax 重复提交

    var requestSent = false; jQuery("#buttonID").click(function() { if(!requestSent) { request ...

  8. 快速搭建Spring Boot项目

    Spring boot是Spring推出的一个轻量化web框架,主要解决了Spring对于小型项目饱受诟病的配置和开发速度问题. Spring Boot 包含的特性如下: 创建可以独立运行的 Spri ...

  9. [HTML5和Flash视频播放器]Video.js 学习笔记(一 ) HLS库:videojs-contrib-hls

    DEMO地址:https://github.com/Tinywan/PHP_Experience https://github.com/videojs/videojs-contrib-hls 下载JS ...

  10. TPS低,CPU高--记一次storm压测问题排查过程

    一.业务背景+系统架构 本次场景为kafka+storm+redis+hbase,通过kafka的数据,进入storm的spout组件接收,转由storm的Bolt节点进行业务逻辑处理,最后再推送进k ...