codeforces#580 D. Kefa and Dishes(状压dp)
题意:有n个菜,每个菜有个兴奋值,并且如果吃饭第i个菜立即吃第j个菜,那么兴奋值加ma[i][j],求吃m个菜的最大兴奋值,(n<=18)
分析:定义dp[status][last],status为每道菜的状态,1为吃,0为不吃,last为最后一个菜,dp[status][last],在status状态下最后吃last的最大兴奋值,然后枚举status去更新后面的状态,详细见代码
AC代码:
- #include <bits/stdc++.h>
- #define ll long long
- using namespace std;
- int n,m,k;
- ll w[20],ma[20][20],dp[(1<<18)+10][20],ans=0;
- int main()
- {
- ios::sync_with_stdio(false);
- cin>>n>>m>>k;
- for(int i=0;i<n;i++)
- cin>>w[i];
- for(int i=1;i<=k;i++)
- {
- int a,b;
- ll c;
- cin>>a>>b>>c;
- ma[a-1][b-1]=c;
- }
- for(int i=0;i<n;i++)
- dp[(1<<i)][i]=w[i];
- for(int i=0;i<(1<<n);i++)
- {
- int cnt=0;
- for(int j=0;j<n;j++)//当前状态可以转移到“当前状态+一个菜”
- {
- if((i&(1<<j))==0)continue;
- cnt++;
- // cout<<cnt<<endl;
- for(int k=0;k<n;k++)//找到一个当前状态不吃的菜,用当前状态去更新吃第k个菜的状态
- {
- if((i&(1<<k))==1)continue;
- dp[(i^(1<<k))][k]=max(dp[(i^(1<<k))][k],dp[i][j]+w[k]+ma[j][k]);
- }
- }
- if(cnt==m)
- for(int j=0;j<n;j++)ans=max(ans,dp[i][j]);
- }
- cout<<ans<<endl;
- return 0;
- }
codeforces#580 D. Kefa and Dishes(状压dp)的更多相关文章
- Codeforces Round #321 (Div. 2) D. Kefa and Dishes 状压dp
题目链接: 题目 D. Kefa and Dishes time limit per test:2 seconds memory limit per test:256 megabytes 问题描述 W ...
- Codeforces ----- Kefa and Dishes [状压dp]
题目传送门:580D 题目大意:给你n道菜以及每道菜一个权值,k个条件,即第y道菜在第x道后马上吃有z的附加值,求从中取m道菜的最大权值 看到这道题,我们会想到去枚举,但是很显然这是会超时的,再一看数 ...
- CF580D Kefa and Dishes 状压dp
When Kefa came to the restaurant and sat at a table, the waiter immediately brought him the menu. Th ...
- codeforces 580D Kefa and Dishes(状压dp)
题意:给定n个菜,每个菜都有一个价值,给定k个规则,每个规则描述吃菜的顺序:i j w,按照先吃i接着吃j,可以多增加w的价值.问如果吃m个菜,最大价值是多大.其中n<=18 思路:一看n这么小 ...
- Codeforces Gym 100015F Fighting for Triangles 状压DP
Fighting for Triangles 题目连接: http://codeforces.com/gym/100015/attachments Description Andy and Ralph ...
- codeforces 342D Xenia and Dominoes(状压dp+容斥)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud D. Xenia and Dominoes Xenia likes puzzles ...
- Codeforces 375C - Circling Round Treasures(状压 dp+最短路转移)
题面传送门 注意到这题中宝藏 \(+\) 炸弹个数最多只有 \(8\) 个,故考虑状压,设 \(dp[x][y][S]\) 表示当前坐标为 \((x,y)\),有且仅有 \(S\) 当中的物品被包围在 ...
- Codeforces Round #321 (Div. 2) Kefa and Dishes 状压+spfa
原题链接:http://codeforces.com/contest/580/problem/D 题意: 给你一些一个有向图,求不超过m步的情况下,能获得的最大权值和是多少,点不能重复走. 题解: 令 ...
- D. Kefa and Dishes(状压)
永久打开的传送门 \(这次总算没有写砸........\) \(设f[i][j]为上一次吃的i物品状态为j的最大收益\) \(那么我们就暴力枚举所有状态i,然后在当前状态找出一个没吃的食物j,再去找一 ...
随机推荐
- Python操作字典(dict)
一.字典定义 >>> dict={} 二.字典元素添加 >>> dict['性别']='男' >>> dict {'性别': '男'} >& ...
- Python平时代码的一些知识
os.walk的作用: def file_name(file_dir): for root, dirs, files in os.walk(file_dir): print(root) #当前目录 ...
- Win10 - MySQL-zip安装方法
Win10 - MySQL-zip安装方法 安装步骤 1.下载,到MySQL官网:https://dev.mysql.com/downloads/mysql/ 2.解压安装包 解压下载的安装包,放到你 ...
- DubboAdmin部署
1.软件下载 部署管理后台和监控中心需要以下软件 opensesame 下载地址:https://github.com/alibaba/opensesame Dubbo源码下载 https://g ...
- Ajax入门例子
在customer.php的文件中,代码如下: <html> <head> <script type="text/javascript"> fu ...
- JavaScript -- 时光流逝(九):Window 对象、Navigator 对象
JavaScript -- 知识点回顾篇(九):Window 对象.Navigator 对象 1. Window 对象 1.1 Window 对象的属性 (1) closed: 返回窗口是否已被关闭. ...
- leetcode刷题--两数之和(简单)
一.序言 第一次刷leetcode的题,之前从来没有刷题然后去面试的概念,直到临近秋招,或许是秋招结束的时候才有这个意识,原来面试是需要刷题的,面试问的问题都是千篇一律的,只要刷够了题就差不多了,当然 ...
- python 基本数据类型--字符串实例详解
字符串(str) :把字符连成串. 在python中⽤', ", ''', """引起来的内容被称为字符串 . 注意:python中没有单一字符说法,统一称叫字 ...
- F. Graph Without Long Directed Paths Codeforces Round #550 (Div. 3)
F. Graph Without Long Directed Paths time limit per test 2 seconds memory limit per test 256 megabyt ...
- Thread.interrupt()
作者:Intopass链接:https://www.zhihu.com/question/41048032/answer/89431513来源:知乎著作权归作者所有.商业转载请联系作者获得授权 ...