hdoj 3535 AreYouBusy(混合揹包)



AreYouBusy

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4572    Accepted Submission(s): 1862


Problem Description
Happy New Term!
As having become a junior, xiaoA recognizes that there is not much time for her to AC problems, because there are some other things for her to do, which makes her nearly mad.
What's more, her boss tells her that for some sets of duties, she must choose at least one job to do, but for some sets of things, she can only choose at most one to do, which is meaningless to the boss. And for others, she can do of her will. We just define the things that she can choose as "jobs". A job takes time , and gives xiaoA some points of happiness (which means that she is always willing to do the jobs).So can you choose the best sets of them to give her the maximum points of happiness and also to be a good junior(which means that she should follow the boss's advice)?
 

Input
There are several test cases, each test case begins with two integers n and T (0<=n,T<=100) , n sets of jobs for you to choose and T minutes for her to do them. Follows are n sets of description, each of which starts with two integers m and s (0<m<=100), there are m jobs in this set , and the set type is s, (0 stands for the sets that should choose at least 1 job to do, 1 for the sets that should choose at most 1 , and 2 for the one you can choose freely).then m pairs of integers ci,gi follows (0<=ci,gi<=100), means the ith job cost ci minutes to finish and gi points of happiness can be gained by finishing it. One job can be done only once.
 

Output
One line for each test case contains the maximum points of happiness we can choose from all jobs .if she can’t finish what her boss want, just output -1 .



解題思路:

經典混合揹包

     題目給了很多類別的物品。用數組dp[i][j],表示第i組,時間爲j時的快樂值。每得到一組工作就進行一次DP,所以dp[i]爲第i組的結果。

  第一類(01揹包坑點多),至少選一項,即必須要選,那麼在開始時,對於這一組的dp的初值,應該全部賦爲負無窮,這樣才能保證不會出現都不選的情況。

狀態轉移方程:dp[i][j]=max(dp[i][j],max(dp[i][j-w[x]]+p[x],dp[i-1][j-w[x]]+p[x]));

dp[i][j]: 是不選擇當前工作;

dp[i-1][j-w[x]]+p[x]: 第一次在本組中選物品,由於開始將該組dp賦爲了負無窮,所以第一次取時,必須由上一組的結果推知,這樣才能保證得到全局最優解;

dp[i][j-w[x]]+p[x]:表示選擇當前工作,並且不是第一次取;

  第二類(分組揹包),最多選一項,即要麼不選,一旦選,只能是第一次選。

狀態轉移方程:dp[i][j]=max(dp[i][j],dp[i-1][j-w[x]]+p[x]);

由於要保證得到全局最優解,所以在該組DP開始以前,應該將上一組的DP結果先複製到這一組的dp[i]數組裏,因爲當前組的數據是在上一組數據的基礎上進行更新的。

     第三類(01揹包),任意選,即不論選不選,選幾個都可以。

狀態轉移方程爲:dp[i][j]=max(dp[i][j],dp[i][j-w[x]]+p[x]);

同樣要保證爲得到全局最優解,先複製上一組解,數據在當前組更新。


#include<bits/stdc++.h>
using namespace std;

const int INF = 0x3f3f3f;
int w[110],p[110];
int dp[110][110];

int main()
{
    int i,j,k,m,s,n,T;
    while(~scanf("%d%d",&n,&T))
    {
        memset(dp,0,sizeof(dp));
        for(i=1;i<=n;i++)
        {
            scanf("%d%d",&m,&s);
            for(j=1;j<=m;j++)
                scanf("%d%d",&w[j],&p[j]);
            if(s==0)
            {
                for(k=0;k<=T;k++)
                    dp[i][k]=-INF;
                for(k=1;k<=m;k++)
                    for(j=T;j>=w[k];j--)
                    {
                        dp[i][j]=max(dp[i][j],dp[i][j-w[k]]+p[k]);
                        dp[i][j]=max(dp[i][j],dp[i-1][j-w[k]]+p[k]);
                    }
            }
            else if(s==1)
            {
                for(k=0;k<=T;k++)
                    dp[i][k]=dp[i-1][k];
                for(k=1;k<=m;k++)
                    for(j=T;j>=w[k];j--)
                        dp[i][j]=max(dp[i][j],dp[i-1][j-w[k]]+p[k]);
            }
            else if(s==2)
            {
                for(k=0;k<=T;k++)
                    dp[i][k]=dp[i-1][k];
                for(k=1;k<=m;k++)
                    for(j=T;j>=w[k];j--)
                        dp[i][j]=max(dp[i][j],dp[i][j-w[k]]+p[k]);
            }
        }
        dp[n][T]=max(dp[n][T],-1);
        printf("%d\n",dp[n][T]);
    }
    return 0;
}




發佈了50 篇原創文章 · 獲贊 3 · 訪問量 8748
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章