P1757 通天之分组背包
背包中的经典问题,我竟然不知道。
分组背包
就是每个物品有一个所属的小组,小组内的物品会冲突。
就是把01背包中的两个for换一下位置
01:
for(i,1,kind)
for(j,v,w[i])
分组背包
for(j,v,w[i])
for(i,1,kind)
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<cstring>
#define inf 2147483647
#define For(i,a,b) for(register int i=a;i<=b;i++)
#define p(a) putchar(a)
#define g() getchar()
//by war
//2017.10.17
using namespace std;
int n,m,x,y,z;
struct bag
{
int cnt;
int v[];
int w[];
}a[];
int f[];
bool b[];
int kind;
int ans;
void in(int &x)
{
int y=;
char c=g();x=;
while(c<''||c>'')
{
if(c=='-')
y=-;
c=g();
}
while(c<=''&&c>='')x=x*+c-'',c=g();
x*=y;
}
void o(int x)
{
if(x<)
{
p('-');
x=-x;
}
if(x>)o(x/);
p(x%+'');
}
int main()
{
in(m),in(n);
For(i,,n)
{
in(x),in(y),in(z);
if(!b[z])
{
kind++;
b[z]=true;
}
a[z].cnt++;
a[z].v[a[z].cnt]=y;
a[z].w[a[z].cnt]=x;
}
For(i,,kind)
{
for(int t=m;t>=;t--)
For(j,,a[i].cnt)
if(t>=a[i].w[j])
f[t]=max(f[t],f[t-a[i].w[j]]+a[i].v[j]);
}
o(f[m]);
return ;
}