先贴一下转载的思路和代码,,,:http://blog.csdn.net/qian99/article/details/39138329

状压dp博大精深啊,以后看到n<=50都可以往状压上想,orz

We Need Medicine


Time Limit: 10 Seconds                                     Memory Limit: 65536 KB                                                     Special Judge                            

A terrible disease broke out! The disease was caused by a new type of virus, which will lead to lethal lymphoedema symptom. For convenience, it was named LL virus.

After several weeks of research, the scientists found the LL virus highly lethal and infectious. But more importantly, it has a long incubation period. Many victims were unaware of being infected until everything was too late. To prevent from the apocalypse, we need medicine!

Fortunately, after another several weeks of research, the scientists have finished the analysis of the LL virus. You need write a program to help them to produce the medicine.

The scientists provide you N kinds of chemical substances. For each substance, you can either use it exact Wi milligrams in a medicine, or not use it. Each selected substance will add Ti points of therapeutic effect value (TEV) to the medicine.

The LL virus has Q different variants. For each variant, you need design a medicine whose total weight equals to Mi milligrams and total TEV equals to Si points. Since the LL virus is spreading rapidly, you should start to solve this problem as soon as possible!

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first line contains two integers N (1 <= N <= 400) and Q (1 <= Q <= 400).

For the next N lines, each line contains two integers Wi (1 <= Wi <= 50) and Ti (1 <= Ti <= 200000).

Then followed by Q lines, each line contains two integers Mi (1 <= Mi <= 50) and Si (1 <= Si <= 200000).

Output

For each test case, output Q lines. For the i-th line, output the indexes (1-based) of chemical substances in the i-th medicine, separated by a space. If there are multiple solutions, output any one. If there is no solution, output "No solution!" instead.

Sample Input

1
3 3
2 10
1 12
1 5
3 15
4 27
3 17

Sample Output

1 3
3 2 1
No solution!

Author: JIANG, Kai                                         Source: The 2014 ACM-ICPC Asia Mudanjiang Regional First Round

转自:http://blog.csdn.net/qian99/article/details/39138329

题意:给出n个物品,每个物品有两种属性Wi,Ti,有q组查询,每组查询要求在n个物品中选出一些,并使得两个属性的和为Mi,Si。

思路:刚开始看感觉是神题,后来仔细想了想,其实本质上就是个背包。最裸着写的话,那么就是dp[i][j][k]表示使用前i个物品,是否可以凑出第一个属性j,第二个属性k,要输出方案的话记录一下路径就可以了。一开始这么写了一发,加了一些乱七八糟的优化,还是会T。虽然这题时限还算宽,但这么写复杂度还是太高了。考虑到第一个属性最多只有50,那么可以用一个二进制数来表示是否能凑出第一个属性的情况,即:第i位为1表示可以凑出i。使用这种方法的好处是对于物品i可以直接算出第一种属性的组合情况,枚举一下新增的位,更新一下结果就行了。

 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map> #define N 410
#define M 200010
#define mod 6
#define mod2 100000000
#define ll long long
#define ull unsigned long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int T;
int n,q;
int w[N],t[N];
int m,s;
ull f[M];
int ans[M][];
map<ull,int>mt; void ini1()
{
int i;
for(i=;i<=;i++){
mt[ (1ll<<(i-1ll)) ]=i;
}
} void ini()
{
int i,j;
ull k,x;
ull v;
scanf("%d%d",&n,&q);
memset(ans,,sizeof(ans));
memset(f,,sizeof(f));
for(i=;i<=n;i++){
scanf("%d%d",&w[i],&t[i]);
}
f[]=;
for(i=;i<=n;i++){
for(j=;j>=t[i];j--){
v=f[j];
f[j] |= (f[j-t[i]]<<w[i]) & ( (1ll<<)- );
for(k=v ^ f[j];k>;k &= (k-) )
{
x=(k ^(k-))&k;
ans[j][ mt[x]- ]=i;
}
}
}
} void solve()
{
int te;
while(q--)
{
scanf("%d%d",&m,&s);
if(ans[s][m]==){
printf("No solution!\n");
continue;
}
else{
printf("%d",ans[s][m]);
te=ans[s][m];
m-=w[te];
s-=t[te];
while(m!=)
{
printf(" %d",ans[s][m]);
te=ans[s][m];
m-=w[te];
s-=t[te];
}
printf("\n");
}
}
} void out()
{
//printf("%lld\n",ans);
//cout<<ans<<endl;
} int main()
{
ini1();
// freopen("data.in","r",stdin);
scanf("%d",&T);
for(int cnt=;cnt<=T;cnt++)
// while(T--)
//while(scanf("%I64d%I64d%I64d",&a,&b,&c)!=EOF)
{
ini();
solve();
//out();
} return ;
}

再贴一份自己加过注释的,状压dp博大精深啊:

 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map> #define N 410
#define M 200010
#define mod 6
#define mod2 100000000
#define ll long long
#define ull unsigned long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int T;
int n,q;
int w[N],t[N];
int m,s;
ull f[M];
int ans[M][];
map<ull,int>mt; void ini1()
{
int i;
for(i=;i<=;i++){
mt[ (1ll<<(i-1ll)) ]=i;
//printf(" i=%d mt=%I64d\n",i,(1ll<<(i-1ll)));
}
} void ini()
{
int i,j;
ull k,x;
ull v,te;
scanf("%d%d",&n,&q);
memset(ans,,sizeof(ans));
memset(f,,sizeof(f));
for(i=;i<=n;i++){
scanf("%d%d",&w[i],&t[i]);
}
f[]=;
for(i=;i<=n;i++){
//printf(" i=%d\n",i);
//for(j=200000;j>=t[i];j--){
for(j=;j>=t[i];j--){
v=f[j]; //j原来存的值
te=(f[j-t[i]]<<w[i]);
f[j] |= (f[j-t[i]]<<w[i]) ; //j原来存的值+转移后存的值
// printf(" j=%d v=%I64d f=%I64d\n",j,v,f[j]);
for(k=v ^ f[j];k>;k &= (k-) ) //k初始化为新增出来的值,然后不断将k最右边的1减掉
// for(k=te;k>0;k &= (k-1) )
{
x=(k ^(k-))&k; //取k最右边的1
ans[j][ mt[x]- ]=i;
// printf(" k=%I64d x=%I64d mtx-1=%d ans=%d\n",k,x,mt[x]-1,ans[j][ mt[x]-1 ]);
}
}
}
} void solve()
{
int te;
while(q--)
{
scanf("%d%d",&m,&s);
if(ans[s][m]==){
printf("No solution!\n");
continue;
}
else{
printf("%d",ans[s][m]);
te=ans[s][m];
m-=w[te];
s-=t[te];
while(m!=)
{
printf(" %d",ans[s][m]);
te=ans[s][m];
m-=w[te];
s-=t[te];
}
printf("\n");
}
}
} void out()
{
//printf("%lld\n",ans);
//cout<<ans<<endl;
} int main()
{
ini1();
// freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
scanf("%d",&T);
for(int cnt=;cnt<=T;cnt++)
// while(T--)
//while(scanf("%I64d%I64d%I64d",&a,&b,&c)!=EOF)
{
ini();
solve();
//out();
} return ;
}

zoj 3812 We Need Medicine (dp 状压)的更多相关文章

  1. 【HDU】4352 XHXJ's LIS(数位dp+状压)

    题目 传送门:QWQ 分析 数位dp 状压一下现在的$ O(nlogn) $的$ LIS $的二分数组 数据小,所以更新时直接暴力不用二分了. 代码 #include <bits/stdc++. ...

  2. 【BZOJ】1076 [SCOI2008]奖励关 期望DP+状压DP

    [题意]n种宝物,k关游戏,每关游戏给出一种宝物,可捡可不捡.每种宝物有一个价值(有负数).每个宝物有前提宝物列表,必须在前面的关卡取得列表宝物才能捡起这个宝物,求期望收益.k<=100,n&l ...

  3. CCF 201312-4 有趣的数 (数位DP, 状压DP, 组合数学+暴力枚举, 推公式, 矩阵快速幂)

    问题描述 我们把一个数称为有趣的,当且仅当: 1. 它的数字只包含0, 1, 2, 3,且这四个数字都出现过至少一次. 2. 所有的0都出现在所有的1之前,而所有的2都出现在所有的3之前. 3. 最高 ...

  4. HDU5731 Solid Dominoes Tilings 状压dp+状压容斥

    题意:给定n,m的矩阵,就是求稳定的骨牌完美覆盖,也就是相邻的两行或者两列都至少有一个骨牌 分析:第一步: 如果是单单求骨牌完美覆盖,请先去学基础的插头dp(其实也是基础的状压dp)骨牌覆盖 hiho ...

  5. POJ 2404 Jogging Trails [DP 状压 一般图最小权完美匹配]

    传送门 题意:找一个经过所有边权值最小的回路,$n \le 15$ 所有点度数为偶则存在欧拉回路,直接输出权值和 否则考虑度数为奇的点,连着奇数条边,奇点之间走已经走过的路移动再走没走过的路 然后大体 ...

  6. BZOJ 2595: [Wc2008]游览计划 [DP 状压 斯坦纳树 spfa]【学习笔记】

    传送门 题意:略 论文 <SPFA算法的优化及应用> http://www.cnblogs.com/lazycal/p/bzoj-2595.html 本题的核心就是求斯坦纳树: Stein ...

  7. BZOJ 2734: [HNOI2012]集合选数 [DP 状压 转化]

    传送门 题意:对于任意一个正整数 n≤100000,如何求出{1, 2,..., n} 的满足若 x 在该子集中,则 2x 和 3x 不能在该子集中的子集的个数(只需输出对 1,000,000,001 ...

  8. hdu 4352 "XHXJ's LIS"(数位DP+状压DP+LIS)

    传送门 参考博文: [1]:http://www.voidcn.com/article/p-ehojgauy-ot.html 题解: 将数字num字符串化: 求[L,R]区间最长上升子序列长度为 K ...

  9. HDU.4352.XHXJ's LIS(数位DP 状压 LIS)

    题目链接 \(Description\) 求\([l,r]\)中有多少个数,满足把这个数的每一位从高位到低位写下来,其LIS长度为\(k\). \(Solution\) 数位DP. 至于怎么求LIS, ...

随机推荐

  1. 立个单调栈flag

    http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=687&pid=1002

  2. poj2312Battle City BFS

    题意: M行N列矩阵, 'Y'表示开始位置, 'T'表示目标位置, 从开始位置到目标位置至少需要走多少步,其中, 'S', 'R'表示不能走, 'B' 花费为2, 'E'花费为1. 思路:纯 BFS. ...

  3. MIPS程序设计实例

    第一题:用系统功能调用实现简单输入输出 题目要求 利用系统功能调用从键盘输入,转换后在屏幕上显示,具体要求如下: 1.如果输入的是字母(A~Z,区分大小写)或数字(0~9),则将其转换成对应的英文单词 ...

  4. CAD交互绘制矩形批注(网页版)

    js中实现代码说明: 动态拖放时的绘制事件: function DynWorldDrawComment2( pCustomEntity,pWorldDraw, curPt) { // 得到绘制参数. ...

  5. 模板类 vector

    概要 介绍一下模板类 vector 的常用操作,以及一个应用举例,顺时针打印矩阵.   基本定义 模板类 vector 是一种动态数组,它是使用 new 创建动态数组的替代品,实际上,vector 也 ...

  6. call和apply方法的异同

    基本作用:改变对象的执行上下文. this指向执行上下文.(执行环境) this指向的永远是调用该方法的对象 function func(){ this.a=1; console.log(this.a ...

  7. 清理数据库事务——SQL语句

    清除流程内部的所有相关数据 eg1: declare @procedureTemp table ( [ProcedureCode] varchar(10) ) declare @ProcedureCo ...

  8. _IO_FILE

    hctf2017的babyprintf解法是house of orange,深入学习了一下,牵扯出许多知识,这里先进行第一步:_IO_FILE结构 0x00 _IO_FILE glibc-2.2.1\ ...

  9. NoSQL 之 Morphia 操作 MongoDB

    上两篇文章:http://www.cnblogs.com/hoojo/archive/2011/06/01/2066426.html http://www.cnblogs.com/hoojo/arch ...

  10. [LUOGU] P2196 挖地雷

    题目背景 NOIp1996提高组第三题 题目描述 在一个地图上有N个地窖(N<=20),每个地窖中埋有一定数量的地雷.同时,给出地窖之间的连接路径.当地窖及其连接的数据给出之后,某人可以从任一处 ...