背包 ZOJ 3812 We Need Medicine

来源:互联网 发布:caffe 数据增强 编辑:程序博客网 时间:2024/06/05 19:40

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 Tipoints 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

13 32 101 121 53 154 273 17

Sample Output

1 33 2 1No solution!

题意:有N个物品,有两个价值,一个在(1~50) ,一个在(1~200000), 然后询问怎么样组成两个价值和分别恰好是和M,S。 


思路:直接做的话是二维背包是吧。。。但是复杂度显然会爆的。看到M的范围如此小,我们能想到这个一定是一个突破口,稍微想想,你能想到用一个51bit 就能标识每个取值了(0,1,2...50)51种可能,这里可以用long long来压缩状态。那么我们用dp[i] 表示S价值为i,M可能的取值的一个long long的数。转移的时候对应一个mi,我们只需要把dp[i] << mi,就是转移到的新状态了。所以转移方程是dp[j+S[i]] |= dp[j] << M[i];  然后直接用背包的方法做就行了。  当时没做出来,看来对于这种64以内的东西还是不够敏感啊。


代码:

#include <iostream>#include <cstdio>#include <cstring>#include <cassert>#include <queue>#include <vector>#include <map>#include <set>#include <algorithm>using namespace std;#define rep(i,a,b) for(int i=(a);i<(int)(b);++i)#define rrep(i,b,a) for(int i=(b);i>=(int)(a);--i)#define eps 1e-9#define clr(a,x) memset(a,x,sizeof(a))#define LL long longconst int maxS = 200000+5;const int maxn = 400+5;const LL mask = (1LL<<50)-1;LL dp[maxS];int M[maxn],S[maxn];int id[maxn];int N,Q;bool cmp(int x,int y) { return S[x] < S[y]; }void input(){    rep(i,1,N+1) {        scanf("%d%d",M+i,S+i);        id[i] = i;    }    sort(id+1,id+1+N,cmp);    clr(dp,0);    dp[0] = 1;    int sum = 0;    rep(z,1,N+1) {        int i = id[z];        sum += S[i];        if (sum >= maxS-5) sum = maxS-5;        rrep(j,sum-S[i],0)            dp[j+S[i]] |= dp[j] << M[i];    }}int ans[maxn],c;bool dfs(int cur,int m,int s){    if (cur == 0) return s == 0 && m == 0;    int x = id[cur];    if (!(dp[s] & (1LL<<m))) return false;    if (M[x] <= m && S[x] <= s && (dp[s] & (1LL<<m))) {        ans[c++] = x;        if (dfs(cur-1,m-M[x],s-S[x])) return true;        --c;    }    return dfs(cur-1,m,s);}void solve(){    while (Q--) {        int m, s; scanf("%d%d",&m,&s);        c = 0;        if (!dfs(N,m,s)) puts("No solution!");        else {            printf("%d",ans[0]);            rep(i,1,c) printf(" %d",ans[i]);            puts("");        }    }}void Getinput(){    freopen("in.txt","w",stdout);    int T = 13;    printf("%d\n",T);    while (T--) {        N = Q = 400;        printf("%d %d\n",N,Q);        rep(i,0,N) printf("%d %d\n",rand()%50+1,rand()%200000+1);        rep(i,0,Q) printf("%d %d\n",rand()%50+1,rand()%200000+1);    }}int main(){//    Getinput(); return 0;//    freopen("in.txt","r",stdin);    int T; cin >> T;    int cas = 0;    while (T--) {        ++cas;        scanf("%d%d",&N,&Q);        input();        solve();    }}


0 0
原创粉丝点击