「日常训练」Kefa and Dishes(Codeforces Round #321 Div. 2 D)
题意与分析(CodeForces 580D)
一个人有\(n\)道菜,然后要点\(m\)道菜,每道菜有一个美味程度;然后给你了很多个关系,表示如果\(x\)刚好在\(y\)前面做的话,他的美味程度就会增加\(c\)。求最大的美味程度。
这种题目一看就是状压dp,\(n \le 15\)啊。定义\(dp[i][stat]\)等于最后一道菜为第i个菜,吃完第i道菜后的状态是stat(第i位为是否吃过,二进制位数的和是吃过菜的总数)的最大美味程度。那么$$dp[i][stat]=max{dp[j][stat-(1<<(i-1))]+extra[j][i]+taste[i]}$$其中i、j必须在stat中有。然后记忆化搜索或者递推去解都可以。可以说是状压dp的板子题了。注意一下边界条件和起始求值即可。
代码
#include <bits/stdc++.h>
#define MP make_pair
#define PB emplace_back
#define fi first
#define se second
#define ZERO(x) memset((x), 0, sizeof(x))
#define ALL(x) (x).begin(),(x).end()
#define rep(i, a, b) for (repType i = (a); i <= (b); ++i)
#define per(i, a, b) for (repType i = (a); i >= (b); --i)
#define MS(x,y) memset(x,y,sizeof(x))
#define QUICKIO \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
using namespace std;
using ll=long long;
using repType=ll;
ll dp[25][1000005];
ll taste[25];
ll extra[25][25];
int n,m,k;
ll solve(int last, int stat) // stat after eating up last
{
//cout<<last<<" "<<stat<<endl;
if(dp[last][stat]!=-1) return dp[last][stat];
else
{
ll ans=0;
int val=(stat-(1<<(last-1)));
rep(i,1,n)
{
if(val&(1<<(i-1)))
{
ans=max(ans,solve(i,val)+extra[i][last]+taste[last]);
}
}
//cout<<" "<<last<<": "<<stat<<" "<<ans<<endl;
return dp[last][stat]=ans;
}
}
int main()
{
MS(dp,-1);
cin>>n>>m>>k;
rep(i,1,n)
{
cin>>taste[i];
dp[i][1<<(i-1)]=taste[i];
}
/*
rep(i,1,n)
{
rep(j,0,((1<<n)-1))
cout<<dp[i][j]<<" ";
cout<<endl;
}
*/
rep(i,1,k)
{
int x,y,c;
cin>>x>>y>>c;
extra[x][y]=c;
}
ll ans=0;
rep(i,1,n) dp[i][(1<<n)-1]=solve(i,(1<<n)-1);
rep(i,1,n)
{
rep(j,0,((1<<n)-1))
{
if(__builtin_popcount(j)==m)
{
ans=max(ans,dp[i][j]);
}
//cout<<dp[i][j]<<" ";
}
//cout<<endl;
}
cout<<ans<<endl;
}
「日常训练」Kefa and Dishes(Codeforces Round #321 Div. 2 D)的更多相关文章
- 「日常训练」Watering Flowers(Codeforces Round #340 Div.2 C)
题意与分析 (CodeForces 617C) 题意是这样的:一个花圃中有若干花和两个喷泉,你可以调节水的压力使得两个喷泉各自分别以\(r_1\)和\(r_2\)为最远距离向外喷水.你需要调整\(r_ ...
- 「日常训练」Alternative Thinking(Codeforces Round #334 Div.2 C)
题意与分析 (CodeForces - 603A) 这题真的做的我头疼的不得了,各种构造样例去分析性质... 题意是这样的:给出01字符串.可以在这个字符串中选择一个起点和一个终点使得这个连续区间内所 ...
- 「日常训练」More Cowbell(Codeforces Round #334 Div.2 B)
题意与分析(CodeForces 604B) 题意是这样的:\(n\)个数字,\(k\)个盒子,把\(n\)个数放入\(k\)个盒子中,每个盒子最多只能放两个数字,问盒子容量的最小值是多少(水题) 不 ...
- 「日常训练」Ice Cave(Codeforces Round 301 Div.2 C)
题意与分析(CodeForces 540C) 这题坑惨了我....我和一道经典的bfs题混淆了,这题比那题简单. 那题大概是这样的,一个冰塔,第一次踩某块会碎,第二次踩碎的会掉落.然后求可行解. 但是 ...
- 「日常训练」School Marks(Codeforces Round 301 Div.2 B)
题意与分析(CodeForces 540B) 题意大概是这样的,有一个考试鬼才能够随心所欲的控制自己的考试分数,但是有两个限制,第一总分不能超过一个数,不然就会被班里学生群嘲:第二分数的中位数(科目数 ...
- 「日常训练」The Intriguing Obsession(CodeForces Round #439 Div.2 C)
2018年11月30日更新,补充了一些思考. 题意(CodeForces 869C) 三堆点,每堆一种颜色:连接的要求是同色不能相邻或距离必须至少3.问对整个图有几种连接方法,对一个数取模. 解析 要 ...
- 「日常训练」Regular Bridge(Codeforces Round 306 Div.2 D)
题意与分析 图论基础+思维题. 代码 #include <bits/stdc++.h> #define MP make_pair #define PB emplace_back #defi ...
- 「日常训练」Two Substrings(Codeforces Round 306 Div.2 A)
题意与分析 一道非常坑的水题.分析醒了补. 代码 #include <bits/stdc++.h> #define MP make_pair #define PB emplace_back ...
- 「专题训练」Hard problem(Codeforces Round #367 Div. 2 C)
题意与分析 题意:给出\(n\)个字符串,可以反转任意串,反转每个串都有其对应的花费\(c_i\).经过操作后是否能满足字符串\(\forall i \in [1,n] \text{且} i \in ...
随机推荐
- 17、配置嵌入式servlet容器(1)
SpringBoot默认使用Tomcat作为嵌入式的Servlet容器 1).如何定制和修改Servlet容器的相关配置 1.修改和server有关的配置 (Se ...
- springmvc小结(上)
1.springmvc的整体结构以及流程 ①.前端控制器:只需要在web.xml文件中配置即可 作用:接受请求,处理响应结果,转发器,中央处理器 ②.处理器映射器:根据请求的url找到相应的Handl ...
- Fiddler学习基础(一)
Fiddler官方网站及下载地址:http://www.telerik.com/fiddler 1. Fiddler原理: 作为系统代理,所有的来自微软互联网服务(WinInet)的http请求再到达 ...
- Spring(十七)之表单处理
表单处理在实际开发中,非常常见,比如登录.注册或者新增.修改等等. 希望本示例对于初学者有一定的提升和帮助 该表单实例,主要说明MVC,相当于前台表单提交,提交相当于一个Http请求,这个请求通过Co ...
- ThreadPoolExecutor异常处理
java.util.concurrent包中的ThreadPoolExecutor,提供了java语言的线程池,你可以提交一个返回结果的任务(submit(Callable),返回Future),或者 ...
- Many-to-many relationships in EF Core 2.0 – Part 2: Hiding as IEnumerable
In the previous post we looked at how many-to-many relationships can be mapped using a join entity. ...
- 关于SQL优化这些你了解吗?
目录树 背景 优化点 前提必备知识 优化之一 - 从数据库设计方面考虑 优化之二 - 从SQL语句优化方面考虑 优化之三 - 读写分离与分库分表 背景 在当今这个互联网的时代无非要解决两大难题,其一是 ...
- Context initialization failed org.springframework.beans.factory.BeanCreationException
严重: Context initialization failed org.springframework.beans.factory.BeanCreationException: Error cre ...
- UIButtonType各个类型的解释:
UIButtonType各个类型的解释: typedef NS_ENUM(NSInteger, UIButtonType) { UIButtonTypeCustom = , UIButtonTypeS ...
- 浅谈React和VDom关系
组件化 组件的封装 组件的复用 组件的封装 视图 数据 视图和数据之间的变化逻辑 import React, {Component} from 'react'; export default clas ...