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, ...
随机推荐
- “流”的思维—Workflowy
3.“流”的思维—Workflowy是我最喜欢的”流“的工具(WorkFlowy - Organize your brain.)我觉得,让发散性的思维更具实施性,必须分步操作,必须有先后,必须单线程. ...
- 结合浅层高层特征的paper总结
1.ION:在conv3.conv4.conv5和context features上分别进行roi_pooling,在channel那一维进行concat 2.Hypernet:在较浅层max_poo ...
- 爬虫_python3_urllib
urlib库为python3的HTTP内置请求库 urilib的四个模块: urllib.request:用于获取网页的响应内容 urllib.error:异常处理模块,用于处理异常的模块 urlli ...
- Codeforces Round #271 (Div. 2)-A. Keyboard
http://codeforces.com/problemset/problem/474/A A. Keyboard time limit per test 2 seconds memory limi ...
- linux 常用命令(持续更新)
查看IP地址 ifconfig 查看TCP端口 netstat -ntlp vi 文本编辑 (1)进入vi编辑模式 在vi的默认模式中,直接在界面中输入: i 在光标所在位置开始编辑: a 在光标所在 ...
- HTML 显示和隐藏浏览器滚动条
滚动条和overflow有关 显示: overflow-x:auto; overflow-y:auto; overflow-x:scroll; overflow-y:scroll; 隐藏: overf ...
- 118. Pascal's Triangle@python
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. Example: Inpu ...
- perl学习之:匹配修饰符/s /m
m 是将字符串作为多行处理,s是将字符串作为单行处理,如果是s在字符串中出现的\n就相当于普通字符. 6.6. Matching Within Multiple Lines6.6.1. Problem ...
- 模块导入及使用,关键字,模块搜索路径,python文件的两种用途
06.05自我总结 一.模块导入及使用 1.模块导入的两种方式 我们拿time模块并使用其中的time功能进行举例 a)第一种 import time print(time.time) import首 ...
- python--管道, 事件, 信号量, 进程池
一 . 管道 (了解) from multiprocessing import Process, Pipe def f1(conn): # 管道的recv 里面不用写数字 from_main_proc ...