传送门:http://codeforces.com/problemset/problem/580/d

思路:状压DP,f[i][j]表示最后一个为i,已选取的菜的状态为j。

#include<cstdio>
#include<cstring>
#include<algorithm>
const int maxt=540000;
using namespace std;
typedef long long ll;
int n,m,k,g[20][20],a[20],pow[20];ll f[20][maxt],ans;int main(){pow[0]=1;for (int i=1;i<=19;i++) pow[i]=pow[i-1]<<1;scanf("%d%d%d",&n,&m,&k);for (int i=0;i<n;i++) scanf("%d",&a[i]);for (int i=1,x,y,z;i<=k;i++) scanf("%d%d%d",&x,&y,&z),x--,y--,g[x][y]=z;for (int i=0;i<n;i++) f[i][pow[i]]=a[i];for (int i=1;i<m;i++)for (int s=0;s<pow[n];s++){int cnt=0;for (int j=0;j<n;j++) if (s&pow[j]) cnt++;if (cnt!=i) continue;for (int j=0;j<n;j++)if (!(s&pow[j])){for (int k=0;k<n;k++)if (s&pow[k])f[j][s|pow[j]]=max(f[j][s|pow[j]],f[k][s]+a[j]+g[k][j]);}}for (int s=0;s<pow[n];s++){int cnt=0;for (int j=0;j<n;j++) if (s&pow[j]) cnt++;if (cnt!=m) continue;for (int i=0;i<n;i++) ans=max(ans,f[i][s]);}printf("%I64d\n",ans);return 0;
}

转载于:https://www.cnblogs.com/thythy/p/5493527.html

codeforces 580D Kefa and Dishes相关推荐

  1. 【CodeForces - 580D】Kefa and Dishes (状压dp)

    题干: kefa进入了一家餐厅,这家餐厅中有n个菜(0<n≤18),kefa对第i个菜的满意度为ai(0≤ai≤10^9),并且对于这n个菜有k个规则,如果kefa在吃完第xi个菜之后吃了第yi ...

  2. Codeforces ----- Kefa and Dishes [状压dp]

    题目传送门:580D 题目大意:给你n道菜以及每道菜一个权值,k个条件,即第y道菜在第x道后马上吃有z的附加值,求从中取m道菜的最大权值 看到这道题,我们会想到去枚举,但是很显然这是会超时的,再一看数 ...

  3. Codeforces Round #321 (Div. 2) D Kefa and Dishes(dp)

    用spfa,和dp是一样的.转移只和最后一个吃的dish和吃了哪些有关. 把松弛改成变长.因为是DAG,所以一定没环.操作最多有84934656,514ms跑过,实际远远没这么多. 脑补过一下费用流, ...

  4. CodeForces 580A Kefa and First Steps

    Time limit 2000 ms Memory limit 262144 kB Kefa decided to make some money doing business on the Inte ...

  5. 线段树 + 字符串Hash - Codeforces 580E Kefa and Watch

    Kefa and Watch Problem's Link Mean: 给你一个长度为n的字符串s,有两种操作: 1 L R C : 把s[l,r]全部变为c; 2 L R d : 询问s[l,r]是 ...

  6. codeforces 580C Kefa and Park(DFS)

    题目链接:http://codeforces.com/contest/580/problem/C #include<cstdio> #include<vector> #incl ...

  7. Codeforces Round #321 (Div. 2)

    水 A - Kefa and First Steps /************************************************ * Author :Running_Time ...

  8. Codeforces Round #321 (Div. 2) C. Kefa and Park dfs

    C. Kefa and Park Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/probl ...

  9. Codeforces Round #321 (Div. 2) B. Kefa and Company 二分

    B. Kefa and Company Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/pr ...

最新文章

  1. A Comprehensive Analysis of Sequence Alignment Algorithms for LongRead Sequencing
  2. 1字符集 iso latin_ISO-8859-1 、Latin-1 西欧编码介绍及应用
  3. C++17中那些值得关注的特性(上)
  4. 一文读懂什么是P问题、NP问题和NPC问题
  5. python将图片原比例缩小_Python实现图片长宽比例缩放和填充
  6. 用Java打开一个网页
  7. java编译遇到的问题与解决_java web开发中遇到的问题及解决方案(个人学习日志,持续更新)...
  8. 百度大脑全面解析如何通过NLP、CV和ASR等技术将企业服务智能化
  9. Android 8 AudioPolicy 分析
  10. SpringMVC注解@RequestParam(转)
  11. python中元组和列表的区别_Python 序列:列表、元组
  12. IIS6同一IP部署多域名证书(部署指南)
  13. ios共享账号公众号_新增iOS游戏账号共享06
  14. 第五章 多元函数微分学
  15. 5G工业路由器5G工业网关的区别
  16. DOS命令导出文件夹内所有文件的名称和全路径
  17. 加密货币是货币,而非股权
  18. [C++]小根堆 插入/删除/初始化
  19. CIO必读:平常心走IT路 吴正宏经历的信息化三十年
  20. 推荐几款工具,提升十倍工作效率

热门文章

  1. Logistic回归解析
  2. oracle中merge into用法解析
  3. 十进制转换为16进制
  4. NLP论文 -《Distributed Representations of Sentences and Documents》-句子和文档的分布式表示学习(二)
  5. Python读取文本文档转化成列表
  6. 2-3树与2-3-4树【转载】
  7. 自适应关于帧场编码问题
  8. oracle数据库dblink创建语句_3场直播丨达梦DM8数据库安装部署初体验、新基建下的国产数据库应用和发展趋势、Oracle外部表创建与使用...
  9. 路由器snmp配置_S7503E V7 snmpv3典型组网配置案例(与IMC联动)
  10. 初学python需要安装哪些-致Python初学者 Anaconda入门使用指南完整版