HDU 3217 Health(状压DP)
speaking, for each subset S of the N kinds of medicine (excluding the empty set), it has a health value v(S). If YY chooses to take a combination T of the medicines, the final effect to his illness is the sum of health values of all non-empty subsets of T.
YY wants to be healthy as quickly as possible, so the final effect of the medicines he takes should be as large as possible. Of course, YY may choose taking nothing to have a zero final effect, if he is too unlucky to achieve a positive one…
For each test case, the first line contains a positive integer N (N≤16), the number of different kinds of medicine YY received from LMY.
The second line contains a single integer M (0≤M≤2N).
M lines follow, representing a list of health values.
Each of the M lines contains 2 integers, s (1≤s<2N)and v (-10000≤v≤10000), indicating a subset of the N kinds of medicine and its health value. Write s in binary representation and add leading zeros if needed to make it exactly N binary digits. If the ith binary
digit of s is 1, then the subset it represents includes the ith kind of medicine; otherwise it does not.
It is guaranteed that no two lines of the list describe the same subset. All non-empty subsets that do not appear in the list have health value 0.
Input ends with N=0.
2
3
1 10
2 -1
3 100
0
109
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
typedef long long LL;
using namespace std;
int dp[17][1<<17];
int n,m; int main()
{
int s,u,v;
while(~scanf("%d",&n)&&n)
{
memset(dp,0,sizeof(dp));
scanf("%d",&m);
int s=(1<<n)-1;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&u,&v);
dp[0][u]=v;
}
for(int i=1;i<=n;i++)
{
for(int j=0;j<=s;j++)
{
if(j&(1<<(i-1)))
dp[i][j]=dp[i-1][j]+dp[i-1][j-(1<<(i-1))];
else
dp[i][j]=dp[i-1][j];
}
}
int ans=-INT_MAX;
for(int i=0;i<=s;i++)
ans=max(ans,dp[n][i]);
printf("%d\n",ans);
}
return 0;
}
HDU 3217 Health(状压DP)的更多相关文章
- HDU 4284Travel(状压DP)
HDU 4284 Travel 有N个城市,M条边和H个这个人(PP)必须要去的城市,在每个城市里他都必须要“打工”,打工需要花费Di,可以挣到Ci,每条边有一个花费,现在求PP可不可以从起点1 ...
- HDU 4336 容斥原理 || 状压DP
状压DP :F(S)=Sum*F(S)+p(x1)*F(S^(1<<x1))+p(x2)*F(S^(1<<x2))...+1; F(S)表示取状态为S的牌的期望次数,Sum表示 ...
- HDU 3001 Travelling ——状压DP
[题目分析] 赤裸裸的状压DP. 每个点可以经过两次,问经过所有点的最短路径. 然后写了一发四进制(真是好写) 然后就MLE了. 懒得写hash了. 改成三进制,顺利A掉,时间垫底. [代码] #in ...
- HDU - 5117 Fluorescent(状压dp+思维)
原题链接 题意 有N个灯和M个开关,每个开关控制着一些灯,如果按下某个开关,就会让对应的灯切换状态:问在每个开关按下与否的一共2^m情况下,每种状态下亮灯的个数的立方的和. 思路1.首先注意到N< ...
- hdu 4114(状压dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4114 思路:首先是floyd预处理出任意两点之间的最短距离.dp[state1][state2][u] ...
- HDU 3091 - Necklace - [状压DP]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3091 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- HDU 3811 Permutation 状压dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3811 Permutation Time Limit: 6000/3000 MS (Java/Othe ...
- HDU 5838 (状压DP+容斥)
Problem Mountain 题目大意 给定一张n*m的地图,由 . 和 X 组成.要求给每个点一个1~n*m的数字(每个点不同),使得编号为X的点小于其周围的点,编号为.的点至少大于一个其周围的 ...
- hdu 4628 Pieces 状压dp
题目链接 枚举所有状态, 1表示这个字符还在原来的串中, 0表示已经取出来了. 代码中j = (j+1)|i的用处是枚举所有包含i状态的状态. #include <iostream> #i ...
- HDU 5045 Contest(状压DP)
Problem Description In the ACM International Collegiate Programming Contest, each team consist of th ...
随机推荐
- Node-Webkit作者王文睿:桌面应用的全新开发方式
摘要:最近两年,Node.js技术越来越火,基于它所开发的应用也纷纷出现在大家面前,其中Node-Webkit就是这样的一个开源框架,它允许开发者使用Web技术开发桌面应用. Node-Webkit是 ...
- ubuntu14操作系统chrome标签和书签乱码解决
ubuntu操作系统更新后.发现chrome标签和书签中文都无法显示. 解决的方法: 打开配置文件 sudo vim /etc/fonts/conf.d/49-sansserif.conf <! ...
- 通过OpenSSL解码X509证书文件
在Windows平台下.假设要解析一个X509证书文件,最直接的办法是使用微软的CryptoAPI. 可是在非Windows平台下,就仅仅能使用强大的开源跨平台库OpenSSL了.一个X509证书通过 ...
- javascript (十四) dom
通过 HTML DOM,可访问 JavaScript HTML 文档的所有元素. HTML DOM (文档对象模型) 当网页被加载时,浏览器会创建页面的文档对象模型(Document Object M ...
- Delphi 获取网站验证码的图片
uses ActiveX,ComObj; procedure TfrmMain.FormCreate(Sender: TObject); begin OleInitialize(nil); end; ...
- [Android学习笔记]try-catch
private boolean test() { boolean result = true; String str = null; try { Log.d("test",&quo ...
- 微信公众平台应用开发框架sophia设计不足(1)
设计一个小框架考虑的东西真不少,每一样都不easy: 1.既要解决当前技术的不足: 2.又要方便他人使用(基本的目的). 3.同一时候又要设计得优雅.easy扩展. sophia一開始设计用来支持智能 ...
- 苹果新的编程语言 Swift 语言进阶(五)--控制流
Swift 语言支持C语言全部的控制语句.包含for 和while循环语句,if和switch条件语句,以及break和continue控制语句等. Swift 语言除了支持以上语句,还添加了一个f ...
- 函数指针玩得不熟,就不要自称为C语言高手(函数指针是解耦对象关系的最佳利器,还有signal)
记得刚开始工作时,一位高手告诉我说,longjmp和setjmp玩得不熟,就不要自称为C语言高手.当时我半信半疑,为了让自己向高手方向迈进,还是花了一点时间去学习longjmp和setjmp的用法.后 ...
- jsonp与cors跨域的一些理解(转)
CORS其实出现时间不短了,它在维基百科上的定义是:跨域资源共享(CORS )是一种网络浏览器的技术规范,它为Web服务器定义了一种方式,允许网页从不同的域访问其资源.而这种访问是被同源策略所禁止的. ...