码迷,mamicode.com
首页 > 其他好文 > 详细

P2871 手链

时间:2019-03-09 21:49:00      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:数组   nbsp   背包   max   ble   ring   cst   www.   tps   

传送

这个题的数据限制没有翻译出来,所以有可能产生爆内存现象

再此翻译一下:1<=n(物品个数)<=3402,1<=M(总重量)<=12880 (就这两个有点用)

显然这是一个0,1背包题

公式一般的代码如下:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,c[5001],w[5000],f[500000];//f数组要开大点,不然会RE,以及c数组,w数组不要太大,不然会爆内存
int main()
{cin>>n>>m;
  for(int i=1;i<=n;i++)
   cin>>c[i]>>w[i];
   for(int i=1;i<=n;i++)
   {
       for(int j=m;j>=c[i];j--)//这里是0,1背包的循环方式,
完全背包只需要改成 j=c[i];j<=m;j++即可 {f[j]
=max(f[j],f[j-c[i]]+w[i]); } } cout<<f[m]; }

 

P2871 手链

标签:数组   nbsp   背包   max   ble   ring   cst   www.   tps   

原文地址:https://www.cnblogs.com/lcez56jsy/p/10503059.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!