hdu5188 01 背包

这题说的是给了n道题每道题用时ti分钟,需要在Li 时间或者之后完成,得分为vi, 我们 首先必须要能过完成,尽量让L-t小的放在前面。

然后采用背包去做

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <algorithm>
using namespace std;
const int maxn =30+5;
typedef long long LL;
struct point{
  int t,v,L,x;
  bool operator <(const point &rhs)const{
      if(x!=rhs.x) return x<rhs.x;
      return L<rhs.L;
  }
}P[maxn];
LL dp[400005];
int main()
{
    int n,w;
    while(scanf("%d%d",&n,&w)==2){
         LL sum1=0;
         int sumt=0,ub=0;
         for(int i =0; i<n; i++){
             scanf("%d%d%d",&P[i].t,&P[i].v,&P[i].L);
              ub = max(P[i].L,ub);
              sum1+=P[i].v;
              sumt+=P[i].t;
              P[i].x=P[i].L-P[i].t;
         }
         if(sum1<w){
             printf("zhx is naive!
");continue;
         }
         sort(P,P+n);
          ub = sumt+ub;
         memset(dp,0,sizeof(dp));
         for(int i=0; i<n; ++i)
            for(int j=ub; j>=P[i].L&&j>=P[i].t; j--)
             dp[j] = max(dp[j],dp[j-P[i].t]+P[i].v);
         int ans=ub+1;
         for(int i =0; i<=ub; i++)
         if(dp[i]>=w){
             ans=i; break;
         }
         if(ans == ub+1){
             printf("zhx is naive!
");continue;
         }else printf("%d
",ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/Opaser/p/4351004.html