先贴一下转载的思路和代码,,,: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. hdu 1181 深搜

    中文题 深搜 许久没写鸟,卡在输入问题上... #include <iostream> #include <string> using namespace std; bool ...

  2. 小白安装python软件

    首先下载:anaconda3.x          下载方式:百度搜索 清华镜像anaconda   https://mirrors.tuna.tsinghua.edu.cn/help/anacond ...

  3. Ukulele 调音

    正常的持琴姿势时,从上到下依次是:4,3,2,1弦,音从上往下是:G,C,E,A: 3弦 - C - Do - D - Re 2弦 - E - Mi - F - Fa 4弦 - G -So 1弦 - ...

  4. C语言运算符_03

    ·运算符的优先级:C语言中,运算符的优先级共分为15级.1级最高,15级最低.在表达式中,优先级较高的先于优先级较低的进行运算.而在同一个运算量两侧的运算符优先级相同时,则按运算符的结合性所规定的结合 ...

  5. Greenplum/Deepgreen(集群/分布式)安装文档

    Deepgreen分布式安装文档 环境准备 1.安装VMware虚拟机软件,然后在VMware安装三台Linux虚拟机(使用centos7版本) 2.使用的虚拟机如下: 192.168.136.155 ...

  6. 697. Degree of an Array@python

    Given a non-empty array of non-negative integers nums, the degree of this array is defined as the ma ...

  7. 学习笔记之30个常用的maven命令

    maven 命令的格式为 mvn [plugin-name]:[goal-name],可以接受的参数如下, -D 指定参数,如 -Dmaven.test.skip=true 跳过单元测试: -P 指定 ...

  8. UVa-156-反片语

    这题比较精妙的是,我们对于单词重排,实际上是进行了标准化的处理,即按照字典序排序. 这样的话,就很方便地处理了单词的重排问题,我们不需要使用全排列函数进行排列尝试,我们直接化简为一,然后进行比较就可以 ...

  9. [LUOGU] P2716 和谐的雪花

    https://www.luogu.org/problemnew/show/P2716 给出一个n*m的矩形,求里面边长最小的正方形,使得该正方形内最大值与最小值的差大于等于给定的K. 第一反应是二分 ...

  10. Linux poll机制

    1.用户空间调用(参考 poll(2) - Linux man page) int poll(struct pollfd *fds, nfds_t nfds, int timeout); it wai ...