悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 hdu2191

http://acm.hdu.edu.cn/showproblem.php?pid=2191

// 一开始以为可以用贪心的,可是wrong 后来一想肯定是不行的。
//原来这是一个多重背包的问题,后来去查看了相关的资料,不过还不是很懂,特别是动态方程的转移,总是不很熟练的
//for(i = 1..n )
// for(j = v..0 )
//  f[v] = max(f[v], f[v - c[i]]+w[i]);
//问题的关键是转换成0 1背包
#include<iostream>//2249765 2010-03-26 11:50:49 Accepted 2191 0MS 240K 564 B C++ 悔惜晟
#include<cstdio>
#include<algorithm>
#include<string>
using namespace std;
int f[101];

 struct stu
 {
  int p;
  int h;
  int c;
 }df[110];

int main()
{
 int t,n, m, i,j, k;
 scanf("%d", &t);
 while(t--)
 {
  scanf("%d%d", &n, &m);
  for(i = 0 ;i < m ;i++)
   scanf("%d%d%d", &df[i].p, &df[i].h, &df[i].c);
  memset(f,0,sizeof(f));
  for(i = 0 ;i < m ;i++)
   for(j = 1; j <= df[i].c; j++)
    for(k = n; k >= df[i].p; k--)
     f[k] = max(f[k], f[k - df[i].p] + df[i].h );
  printf("%d/n", f[n]);
 }
}

//把问题转换成完全0 1 的背包问题
//把物体的种类分开来处理
#include<iostream>//2249808 2010-03-26 12:11:22 Accepted 2191 15MS 308K 721 B C++ 悔惜晟 
#include<cstdio>
#include<string>
#include<algorithm>
using namespace std;

 struct stu
 {
  int p;
  int h;
  int c;
 }df[101];
 
 struct stu1
 {
  int p1;
  int h1;
  //int c1;
 }df1[2005];

int f[2005];
int main()
{
 int t, n, m, i, j, count;;
 cin>>t;
 while(t--)
 {
  cin>>n>>m;
  count = 0;
  for(i = 0; i < m; i++)
  {
   cin>>df[i].p>>df[i].h>>df[i].c;
   for(j = 0; j < df[i].c ; j++)
   {
    df1[count].p1 = df[i].p;
    df1[count].h1 = df[i].h;
    count++;
   }
  }
  memset(f, 0 ,sizeof(f));
  for(i = 0; i < count; i++)
   for(j = n; j >= df1[i].p1; j--)
   f[j] = max(f[j], f[j - df1[i].p1] + df1[i].h1);
  
  printf("%d/n", f[n]);
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值