zoj3812 We Need Medicine 背包+位优化

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
1
3 3
2 10
1 12
1 5
3 15
4 27
3 17
Sample Output
1 3
3 2 1

No solution!

背包dp[i][j][k]表示第i个,第一属性为j,第二属性为k。考虑最大只有50,可以用二进制表示能达到的状态,即i位为1表示第一属性可以达到i,滚动数组省略i这一维,二进制省略j这一维,dp[k]为第二属性为k是第一属性能达到的状态。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<ostream>
#include<istream>
#include<algorithm>
#include<queue>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<stack>
#include<vector>
#define fi first
#define se second
#define ll long long
#define pii pair<int,int>
#define inf (1<<30)
#define eps 1e-8
#define pb push_back
using namespace std;
const int maxn=110005;
const ll mod=1000000007;
int n,q;
int w[500],t[500];
int ans[55][200005];
ll dp[200005];
map<ll,int>mp;
int main()
{
    for(int i=0;i<=50;i++) {
        mp[1ll<<i]=i;
    }
    int m,s;
    int T;
    scanf("%d",&T);
    while(T--) {
        scanf("%d%d",&n,&q);
        for(int i=1;i<=n;i++)
            scanf("%d%d",&w[i],&t[i]);
        memset(dp,0,sizeof(dp));
        memset(ans,0,sizeof(ans));
        dp[0]=1;
        for(int i=1;i<=n;i++) {
            for(int j=200000;j>=t[i];j--) {
                if(dp[j-t[i]]==0) continue;
                ll tmp=dp[j];
                dp[j]|=(dp[j-t[i]]<<w[i]);
                dp[j]&=((1ll<<51)-1);
                for(ll k=tmp^dp[j];k;k&=(k-1)) {
                    ll x=k&(-k);
                    ans[mp[x]][j]=i;
                }
            }
        }
        while(q--) {
            scanf("%d%d",&m,&s);
            if(ans[m][s]==0) printf("No solution!\n");
            else {
                printf("%d",ans[m][s]);
                int x=ans[m][s];
                m-=w[x]; s-=t[x];
                while(true) {
                    if(m==0 && s==0)
                        break;
                    x=ans[m][s];
                    printf(" %d",x);
                    m-=w[x]; s-=t[x];
                }
                puts("");
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值