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 ...
随机推荐
- Qt多工程多目录的编译案例
源地址:http://blog.csdn.net/libaineu2004/article/details/23625441 写这篇文章的目的是为了让Qt像VC++那样,支持一个工程包含多个项目.即1 ...
- Redis 与 Memcache
最近,我们看到许多使用Redis的案例,尤其是大型及先进的系统中应用的更多.事实上,我们正管理着一个新的规模庞大的Redis集群,设计该架构是为了每秒能处理上百万个事务.然而,Redis与Memcac ...
- Android NDK 简单介绍、工具安装、环境配置
NDK全称:Native Development Kit. 1.NDK是一系列工具的集合. * NDK提供了一系列的工具,帮助开发人员高速开发C(或C++)的动态库,并能自己主动将so和java应用一 ...
- JSP自定义标签——简单标签(1)
前面一篇博客介绍了自定义标签的传统标签使用方式,但是我们会发现,使用传统标签非常的麻烦,而且接口还多,现在传统标签基本都没用了,除了一些比较久的框架.Sun公司之后推出了一个新的标签使用方式,称之为简 ...
- Servlet的学习之Session(5)
在上一篇中我们介绍了如果使用Session来做一个简单的用户登录案例,在本篇中我们继续使用Session技术来做一个防止表单重复提交的案例. 这是一个很重要的知识点,在很多框架中都有防止表单重复提交的 ...
- (WinForm)文件夹状态监控,最小化到托盘,开机自启动
原文 (WinForm)文件夹状态监控,最小化到托盘,开机自启动 . 文件夾監控(監測文件夾中的文件動態): //MSDN上的例子 public class Watcher { public stat ...
- 基础知识(10)- 部署应用程序和applet
10.1 JAR文件 10.1.1 清单文件 10.1.2 可运行JAR文件 10.1.3 资源 10.1.4 密封 10.2 Java Web Start 10.2.1 沙箱 10.2. ...
- ajax后台处理返回json值
public ActionForward xsearch(ActionMapping mapping, ActionForm form, HttpServletRequest request, Htt ...
- MVC3和MVC4中CRUD操作
MVC3中EF实现的CRUD操作 public class HomeController : Controller { // // GET: /Home/ CarModelContainer db = ...
- 单片机C语言实现的采用DS18B20的温度检测装置
这几天老师布置了一个课程设计题目:采用51单片机控制的DS18B20温度检测系统.大概花了我一个礼拜的时间,幸好我的C语言学得还可以,最后还是让我搞出来了,真是高兴,我是采用STC-52单片机和DS1 ...