zoj 3812 We Need Medicine (dp 状压)
先贴一下转载的思路和代码,,,: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 状压)的更多相关文章
- 【HDU】4352 XHXJ's LIS(数位dp+状压)
题目 传送门:QWQ 分析 数位dp 状压一下现在的$ O(nlogn) $的$ LIS $的二分数组 数据小,所以更新时直接暴力不用二分了. 代码 #include <bits/stdc++. ...
- 【BZOJ】1076 [SCOI2008]奖励关 期望DP+状压DP
[题意]n种宝物,k关游戏,每关游戏给出一种宝物,可捡可不捡.每种宝物有一个价值(有负数).每个宝物有前提宝物列表,必须在前面的关卡取得列表宝物才能捡起这个宝物,求期望收益.k<=100,n&l ...
- CCF 201312-4 有趣的数 (数位DP, 状压DP, 组合数学+暴力枚举, 推公式, 矩阵快速幂)
问题描述 我们把一个数称为有趣的,当且仅当: 1. 它的数字只包含0, 1, 2, 3,且这四个数字都出现过至少一次. 2. 所有的0都出现在所有的1之前,而所有的2都出现在所有的3之前. 3. 最高 ...
- HDU5731 Solid Dominoes Tilings 状压dp+状压容斥
题意:给定n,m的矩阵,就是求稳定的骨牌完美覆盖,也就是相邻的两行或者两列都至少有一个骨牌 分析:第一步: 如果是单单求骨牌完美覆盖,请先去学基础的插头dp(其实也是基础的状压dp)骨牌覆盖 hiho ...
- POJ 2404 Jogging Trails [DP 状压 一般图最小权完美匹配]
传送门 题意:找一个经过所有边权值最小的回路,$n \le 15$ 所有点度数为偶则存在欧拉回路,直接输出权值和 否则考虑度数为奇的点,连着奇数条边,奇点之间走已经走过的路移动再走没走过的路 然后大体 ...
- BZOJ 2595: [Wc2008]游览计划 [DP 状压 斯坦纳树 spfa]【学习笔记】
传送门 题意:略 论文 <SPFA算法的优化及应用> http://www.cnblogs.com/lazycal/p/bzoj-2595.html 本题的核心就是求斯坦纳树: Stein ...
- BZOJ 2734: [HNOI2012]集合选数 [DP 状压 转化]
传送门 题意:对于任意一个正整数 n≤100000,如何求出{1, 2,..., n} 的满足若 x 在该子集中,则 2x 和 3x 不能在该子集中的子集的个数(只需输出对 1,000,000,001 ...
- hdu 4352 "XHXJ's LIS"(数位DP+状压DP+LIS)
传送门 参考博文: [1]:http://www.voidcn.com/article/p-ehojgauy-ot.html 题解: 将数字num字符串化: 求[L,R]区间最长上升子序列长度为 K ...
- HDU.4352.XHXJ's LIS(数位DP 状压 LIS)
题目链接 \(Description\) 求\([l,r]\)中有多少个数,满足把这个数的每一位从高位到低位写下来,其LIS长度为\(k\). \(Solution\) 数位DP. 至于怎么求LIS, ...
随机推荐
- XDU——受教了
存在的问题还是很多的 GG 突然觉得刷题的目的并不是追求A.我们应该在那个过程中提高代码能力和建立模型解题能力 会的算法会巧妙应用才是王道 吐槽自己两句,写高数了
- WPF中窗体调用窗体
在WPF中有时候我们需要在一个窗体中去调用另外的一个窗体,下面给出调用方法. 下面实现在MainWindow中通过点击一个按钮调用另外的一个窗口. 首先创建你要调用的另外一个窗口:点击最上面的项目 ...
- QT5:先导篇 全局定义
一.简介 <QtGlobal>头文件包含了Qt类库的一些全局定义,包含基本数据类型 函数和宏 二.全局变量定义 <QtGlobal>定义的数据类型: Qt数据类型 ...
- 相机 感光度iso,焦距,光圈,ccd 和 噪点, 景深关系表格
表格 矩阵 感官度iso: 越低曝光速度越慢,所谓慢工出细活,成像质量会好,如果形成的话. 但是因为慢,所以要更多的光量,才能画完. 就需要更慢的快门 (但是太慢手抖的话就糊掉,或者动的物体形成轨迹. ...
- 大数据学习系列之Hadoop、Spark学习线路(想入门大数据的童鞋,强烈推荐!)
申明:本文出自:http://www.cnblogs.com/zlslch/p/5448857.html(该博客干货较多) 1 Java基础: 视频方面: 推荐<毕向东JAVA ...
- HDU-1312-Black and Red
这题其实和POJ的1979是同一道题,当时POJ使用cin写的,所以读入的时候,就很正确. 这次用scanf读入的时候,就出现了问题,我们在读完宽高之后,要用getchar吸收掉回车,然后每行末尾的回 ...
- layer的iframe层的传参和回参
从父窗口传参给iframe,参考://https://yq.aliyun.com/ziliao/133150 从iframe回参给父窗口,参考:https://www.cnblogs.com/jiqi ...
- PHP+Mysql实现分页
我们在项目开发的过程中避免不了使用分页功能,拿php来说,现在市面上有很多大大小小的php框架,当然了分页这种小功能这些框架中都是拿来直接可以用的. 这些框架的分页功能使用都很方便,配置一下分页所需参 ...
- Verilog之delay的两种用法(inter/intra)
verilog语言中有两种延迟方式:inter-delay和intra-delay,关于inter和intra.这两个英文前缀都有“内部,之间”的意思,但又有所不同.inter表达不同事物之间,int ...
- C语言内存函数
http://see.xidian.edu.cn/cpp/u/hs3/ 函数 说明 calloc() 分配内存空间 free() 释放内存空间 getpagesize() 取得内存分页大小 mallo ...