Shirlies
宁静专注认真的程序媛~

#include "stdio.h"
#include "string.h"

int dp[1002][32];

int main()
{
    int N,V,K,T;
    int val[102],vol[102];
    int fa[35],fb[35];
    int i,j,k,ai,bi;
   
    scanf("%d",&T);
    while(T--)
    {
              scanf("%d%d%d",&N,&V,&K);
              for(i=0;i<N;i++)
              {
              scanf("%d",&val[i]);
              }
              for(i=0;i<N;i++)
              {
              scanf("%d",&vol[i]);
              }
              memset(dp,0,sizeof(dp));
              memset(fa,0,sizeof(fa));
              memset(fb,0,sizeof(fb));
             
              for(i=0;i<N;i++)
              {
                  for(j=V;j>=vol[i];j--)
                  {
                       for(k=1;k<=K;k++)
                       {
                       fa[k]=dp[j][k];
                       fb[k]=dp[j-vol[i]][k]+val[i];
                       }
                      
                       k=1;
                       ai=1;
                       bi=1;
                      
                       while(k<=K&&(ai<=K||bi<=K))//这里ai<=K||bi<=K必须得是或,因为要比较完,如果是&&可能没有储存K个值。
                       {
                       if(fa[ai]>=fb[bi])//这个地方得加等号,不然答案是错误的,因为fa[]在前边是dp[j][k],当fa[]和fb[]相等时,应该选dp[j][k]。(将策略不同但权值相同的两个方案是看作同一个解还是不同的解。如果是前者,则维护有序队列时要保证队列里的数没有重复的。)
                       {
                            dp[j][k]=fa[ai];
                            ai++;
                            }
                       else
                       {
                            dp[j][k]=fb[bi];
                            bi++;
                            }
                            if(dp[j][k]!=dp[j][k-1])
                            k++;
                            }
                            }
                            }
                           
                  printf("%d\n",dp[V][K]);
                  }
                  }
                          
    个人心得:看过别人的解题报告,才知道自己对dP的了解太不深刻了,这一题要建立在对DP的深刻理解上,我个人对DP还只是知道一点皮毛,对其深刻的思想还不太了解,唉,我要好好做dP了,我个人认为做背包的题目还是得先好好搞一通DP啊~~~   

posted on 2011-11-30 20:00  Shirlies  阅读(1637)  评论(0编辑  收藏  举报