2014牡丹江网络预选赛D题(状压DP)zoj3812

来源:互联网 发布:nba2k14卡顿优化补丁 编辑:程序博客网 时间:2024/04/16 17:08
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

13 32 101 121 53 154 273 17

Sample Output

1 33 2 1No solution!

题意:给出n个药品,每个药品有重量和权值,然后有Q次查询,每次查询m,s,要求在那些药里面找一些出来使得总的重量为m,总的权值为s,药品最多使用一次

思路:很像01背包

            用DP[i][j][k]表示在前 i 个药品中总重量为 j 总权值为 k 的状态,存在=1,不存在=0

            但是这题物品数量太多,背包太大,显然空间和时间上都是受不了的

            可以用bitset表示最后一维,如果bitset的第k位存在,则存在权值总和为k的状态,开到200000即可

            然后用滚动数组可以将第一维省去,那么空间上就解决了

            转移方程:DP[j] = DP[j] | ( DP[ j-w[i] ] << v[i] )    ( j 始终是从大for到小,滚动数组原理 )

            预处理出所有的DP[j][k]以后就可以开始查询了

            如果单纯的在400个物品里爆搜是想都不用想会超时的

            所以可以将背包的状态按物品的代号划分出来

            然后每次按当前枚举到的物品,找到所对应的背包状态,直接判断即可

0 0