zoj 3812 We Need Medicine (dp 位最佳化 巧妙記錄路徑)

來源:互聯網
上載者:User

標籤:des   style   color   io   os   ar   strong   for   資料   

We Need MedicineTime 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 toSi 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) andTi (1 <= Ti <= 200000).

Then followed by Q lines, each line contains two integers Mi (1 <=Mi <= 50) andSi (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 3 3 2 1No 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-6typedef 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;}


zoj 3812 We Need Medicine (dp 位最佳化 巧妙記錄路徑)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.