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 namedLL 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 exactWi milligrams in a medicine, or not use it. Each selected substance will addTi 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 toMi 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) andQ (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!
题意:
给你一些材料来配置药品,每种材料有一定的重量和药效,有q个询问,问重量为m,药效为s的药能否配出来。
思路:
一看就觉得是二维费用的背包,但是数据量太大,接受不了,所以要针对问题找另外的解决办法,考虑到重量都在50之内,想到状压。
dp[i][j]表示处理完前i个物品,药效为j时能达到的重量的状态,第一维可以优化。
有点麻烦的是记录路径,因为400*200000的数组开不下,所以得找额外的办法,因为一条路径最多只有50的长度,所以可以用pre[j][k]记录到达药效为j时重量为k时放的是哪一个物品,根据第i个物品递推时会增加的一些状态来记录。
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set> #include <queue> #define maxn 401 #define MAXN 200005 #define INF 0x3f3f3f3f #define mod 1000000007 #define pi acos(-1.0) #define eps 1e-6 typedef unsigned long long ull; using namespace std; int n,m,u,v,ma; int w[maxn],t[maxn]; short pre[200001][51]; ull dp[200001]; void solve() { int i,j; memset(pre,0,sizeof(pre)); memset(dp,0,sizeof(dp)); dp[0]=1; ma=200000; ull tmp,k,tot=(1ULL<<51)-1; for(i=1;i<=n;i++) { for(j=ma;j>=t[i];j--) { if(dp[j-t[i]]==0) continue ; tmp=dp[j]; dp[j]|=(dp[j-t[i]]<<w[i])&tot; for(k=tmp^dp[j];k;k=(k-1)&k) // 新增加的状态 一位一位取出 { pre[j][__builtin_ctzll(k)]=i; // builtin_ctzll-末尾有几个0 } } } } int main() { int i,j,test,tu; scanf("%d",&test); while(test--) { scanf("%d%d",&n,&m); for(i=1;i<=n;i++) { scanf("%d%d",&w[i],&t[i]); } solve(); while(m--) { scanf("%d%d",&u,&v); if(dp[v]&(1ULL<<u)) { printf("%d",pre[v][u]); tu=u; u-=w[pre[v][u]]; v-=t[pre[v][tu]]; while(u) { printf(" %d",pre[v][u]); tu=u; u-=w[pre[v][u]]; v-=t[pre[v][tu]]; } puts(""); } else printf("No solution!\n"); } } return 0; }