sksksk @ 2018-10-05 20:01:17
看了2小时没发现有问题啊
using namespace std; int V,s; int zhu_w[65],zhu_ji[65],fu_w[65][2],fu_ji[65][2]; int f[33000]; int main() { scanf("%d%d",&V,&s); for(int i=1,a,b,c;i<=s;i++) { scanf("%d%d%d",&a,&b,&c); if(c&&!fu_w[c][0])fu_w[c][0]=a,fu_ji[c][0]=ba,i--,s--; else if(c&&fu_w[c][0])fu_w[c][1]=a,fu_ji[c][1]=ba,i--,s--; else zhu_w[i]=a,zhu_ji[i]=b*a; } for(int i=1;i<=s;i++) for(int j=V;j>=0;j--) { if(j>=zhu_w[i])f[j]=max(f[j],f[j-zhu_w[i]]+zhu_ji[i]); if(j>=zhu_w[i]+fu_w[i][0])f[j]=max(f[j],f[j-zhu_w[i]-fu_w[i][0]]+zhu_ji[i]+fu_ji[i][0]); if(j>=zhu_w[i]+fu_w[i][1])f[j]=max(f[j],f[j-zhu_w[i]-fu_w[i][1]]+zhu_ji[i]+fu_ji[i][1]); if(j>=zhu_w[i]+fu_w[i][0]+fu_w[i][1])f[j]=max(f[j],f[j-zhu_w[i]-fu_w[i][1]-fu_w[i][0]]+zhu_ji[i]+fu_ji[i][1]+fu_ji[i][0]); } printf("%d",f[V]); return 0; }
by 大壶 @ 2018-10-05 20:02:36
几分?
by 大壶 @ 2018-10-05 20:02:55
依赖背包
by sksksk @ 2018-10-05 20:02:58
#include<bits/stdc++.h>
using namespace std;
int V,s;
int zhu_w[65],zhu_ji[65],fu_w[65][2],fu_ji[65][2];
int f[33000];
int main()
{
scanf("%d%d",&V,&s);
for(int i=1,a,b,c;i<=s;i++)
{
scanf("%d%d%d",&a,&b,&c);
if(c&&!fu_w[c][0])fu_w[c][0]=a,fu_ji[c][0]=b*a,i--,s--;
else if(c&&fu_w[c][0])fu_w[c][1]=a,fu_ji[c][1]=b*a,i--,s--;
else zhu_w[i]=a,zhu_ji[i]=b*a;
}
for(int i=1;i<=s;i++)
for(int j=V;j>=0;j--)
{
if(j>=zhu_w[i])f[j]=max(f[j],f[j-zhu_w[i]]+zhu_ji[i]);
if(j>=zhu_w[i]+fu_w[i][0])f[j]=max(f[j],f[j-zhu_w[i]-fu_w[i][0]]+zhu_ji[i]+fu_ji[i][0]);
if(j>=zhu_w[i]+fu_w[i][1])f[j]=max(f[j],f[j-zhu_w[i]-fu_w[i][1]]+zhu_ji[i]+fu_ji[i][1]);
if(j>=zhu_w[i]+fu_w[i][0]+fu_w[i][1])f[j]=max(f[j],f[j-zhu_w[i]-fu_w[i][1]-fu_w[i][0]]+zhu_ji[i]+fu_ji[i][1]+fu_ji[i][0]);
}
printf("%d",f[V]);
return 0;
}
by sksksk @ 2018-10-05 20:03:15
@大壶 60
by 大壶 @ 2018-10-05 20:06:47
3个if就够了,你再仔细看一下