codeforces 580D Kefa and Dishes(状压dp)
题意:给定n个菜,每个菜都有一个价值,给定k个规则,每个规则描述吃菜的顺序:i j w,按照先吃i接着吃j,可以多增加w的价值。问如果吃m个菜,最大价值是多大。其中n<=18
思路:一看n这么小,除了暴力之外就得想想状态压缩dp了。因为每种菜正好两种状态(吃过与没吃过),正好可以使用二进制来表示每种状态,那么一共有(1<<n)位种可能,即从状态(000...0)到状态(111...1),所以定义状态dp[s][i],表示状态为s时,并且最后吃第i种菜的时候的最大值。那么转移方程就是
dp[s|(1<<j)] = max(dp[s|(1<<j)], dp[s][i] + ary[i] + ad[i][j];
其中ary表示每种菜的价值,ad[i][j]表示先吃i再吃j的价值。注意状态方程转移的条件:第i位已经选择过,第j位没有选择过。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = ( << ) + ;
long long dp[maxn][];
long long ary[];
long long ad[][];
int main()
{
int n, m, k, u, v, w;
scanf("%d%d%d", &n, &m, &k);
for (int i = ; i < n; i++) cin >> ary[i];
for (int i = ; i < k; i++)
{
scanf("%d%d%d", &u, &v, &w);
u--; v--;
ad[u][v] = w;
}
for (int i = ; i < n; i++)
dp[<<i][i] = ary[i];
long long ans = ;
int tot = << n;
for (int s = ; s < tot; s++)
{
int cnt = ;
for (int i = ; i < n; i++)
{
if ((s & ( << i)) != )
{
cnt++;
for (int j = ; j < n; j++)
{
if ((s & (<<j)) == ) //be careful the priority of operator
{
int ss = s | ( << j);//the next state
dp[ss][j] = max(dp[ss][j], dp[s][i] + ary[j] + ad[i][j]); } } }
}
if (cnt == m)
{
for (int i = ; i < n; i++)
if ((s & (<<i)) != ) ans = max(ans, dp[s][i]);
}
}
cout << ans << endl; return ;
}
codeforces 580D 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道菜的最大权值 看到这道题,我们会想到去枚举,但是很显然这是会超时的,再一看数 ...
- Codeforces 580D Kefa and Dishes(状态压缩DP)
题目链接:http://codeforces.com/problemset/problem/580/D 题目大意:有n盘菜每个菜都有一个满意度,k个规则,每个规则由x y c组成,表示如果再y之前吃x ...
- CF580D Kefa and Dishes 状压dp
When Kefa came to the restaurant and sat at a table, the waiter immediately brought him the menu. Th ...
- dp + 状态压缩 - Codeforces 580D Kefa and Dishes
Kefa and Dishes Problem's Link Mean: 菜单上有n道菜,需要点m道.每道菜的美味值为ai. 有k个规则,每个规则:在吃完第xi道菜后接着吃yi可以多获得vi的美味值. ...
- codeforces 580D. Kefa and Dishes
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- Codeforces Round #363 LRU(概率 状压DP)
状压DP: 先不考虑数量k, dp[i]表示状态为i的概率,状态转移方程为dp[i | (1 << j)] += dp[i],最后考虑k, 状态表示中1的数量为k的表示可行解. #incl ...
- codeforces 8C. Looking for Order 状压dp
题目链接 给n个物品的坐标, 和一个包裹的位置, 包裹不能移动. 每次最多可以拿两个物品, 然后将它们放到包里, 求将所有物品放到包里所需走的最小路程. 直接状压dp就好了. #include < ...
- Codeforces 429C Guess the Tree(状压DP+贪心)
吐槽:这道题真心坑...做了一整天,我太蒻了... 题意 构造一棵 $ n $ 个节点的树,要求满足以下条件: 每个非叶子节点至少包含2个儿子: 以节点 $ i $ 为根的子树中必须包含 $ c_i ...
随机推荐
- Android软件开发之发送短信与系统短信库解析
今天我和同学们讨论一下Android平台下如何调用系统方法发送短信.接收短信.系统的短信库相关的问题.进入正题,我们先使用Eclipse工具模拟给自己的模拟器发送一条短信.在Eclipse下打开DDM ...
- Android用户界面UI组件--AdapterView及其子类(四) GridView
GridView常用的XML属性: android:columnWidth 设置列的宽度. android:horizontalSpacing 两列之间的间距. android:numColum ...
- 用JAVA 查询 Active Directory(AD)
Required Details LDAP address (For e.g.: myjeeva.com or IP of the Domain Controller/Global Catalog[G ...
- MST(prim)+树形dp-hdu-4756-Install Air Conditioning
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4756 题目意思: n-1个宿舍,1个供电站,n个位置每两个位置都有边相连,其中有一条边不能连,求n个 ...
- 新建应用母版页的网页index.aspx,about.aspx,login.aspx
转:http://www.netfoucs.com/liu_111111/article/details/8433491 -----------index.aspx:----------------- ...
- DevExpress控件学习总结 z
1.Navigation & Layout 1.1 Bar Manager 如果想在窗体或用户控件(user control)上添加工具条(bars)或弹出菜单(popup menus),我们 ...
- 使用jenkins + python + selenium一步步搭建web自动化测试“框架”(1) - 各部件简介
原先搭建过一个自动化测试框架,迷迷糊糊不求甚解,算是搭建了一个仅限于能用的.根本算不上框架的玩意儿. 现在回过头来好好看看究竟每一步是怎么联系起来的,是怎么运行起来的. 本次博客先说说各部件都是干什么 ...
- HDOJ/HDU 2203 亲和串(简单的判断~Java的indexOf()方法秒)
Problem Description 人随着岁数的增长是越大越聪明还是越大越笨,这是一个值得全世界科学家思考的问题,同样的问题Eddy也一直在思考,因为他在很小的时候就知道亲和串如何判断了,但是发现 ...
- JavaScript高级程序设计61.pdf
JSON对象 早期的JSON解析器就是使用JavaScript的eval()函数,ECMAScript5对解析JSON的行为做出了规定,定义了全局对象JSON. JSON对象有2个方法:stringi ...
- JAVA网络编程基础知识
网络编程的目的就是指直接或间接地通过网络协议与其他计算机进行通讯.网络编程中有两个主要的问题,一个是如何准确的定位网络上一台或多台主机,另一个就是找到主机后如何可靠高效的进行数据传输.在TCP/IP协 ...