You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
1.4 KiB

#include <iostream>
#include <stdio.h>
using namespace std;
typedef struct{
int v;
int w;
int no;
}goods;
int n,c;//<2F><>Ʒ<EFBFBD><C6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int *x;//<2F><>ǰ<EFBFBD><C7B0>
goods *a;//<2F><>Ʒ
int bv,*bx;//<2F><><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD>Ž<EFBFBD>
int cc,cw;//<2F><>ǰ<EFBFBD><C7B0>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>
int cmp(goods a,goods b)//Լ<><D4BC>sort <20><><EFBFBD><EFBFBD>cmp<6D><70><EFBFBD><EFBFBD>
{
return a.v*b.w>=a.w*b.v;
}
float Bound(int step){//̰<><CCB0><EFBFBD><EFBFBD><E3B7A8><EFBFBD>õ<EFBFBD>ǰ״̬<D7B4><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
int i,m=c-cw;
float b=cc;
for(i=step; i<=n&&a[i].w<=m;i++)
b+=a[i].v, m-=a[i].w;
if(i<=n)
b+=(m*1.0/a[i].w)*a[i].v;
return b;
}
void dfs(int step){
int i;
if(step>n){//Ҷ<>ӽ<EFBFBD><D3BD><EFBFBD>
if(cc>bv){
bv=cc;
for(i=1;i<=n;i++)bx[a[i].no]=x[i];//<2F><><EFBFBD>Ż<EFBFBD>ԭ
}
return;//<2F><>ͷ
}
if(cw+a[step].w<=c){//<2F><><EFBFBD><EFBFBD>Լ<EFBFBD><D4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
x[step]=1;
cc+=a[step].v;cw+=a[step].w;
dfs(step+1);
cc-=a[step].v;cw-=a[step].w;//<2F>ָ<EFBFBD><D6B8>ֳ<EFBFBD>
}
if(Bound(step+1)>bv){//<2F><><EFBFBD><EFBFBD><EFBFBD>Ͻ<EFBFBD><CFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
x[step]=0;
dfs(step+1);
return;
}
}
int main(){
int i;
cout<<"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʒ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>n<EFBFBD><EFBFBD>"<<endl;
cin>>n;
a=new goods[n+1];
bx=new int[n+1];
x=new int[n+1];
cc=0;
cw=0;
bv=0;
cout<<"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>c<EFBFBD><EFBFBD>"<<endl;
cin>>c;
for(i=1;i<=n;i++)a[i].no=i,x[i]=0;
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>%d<><64><EFBFBD><EFBFBD>Ʒ<EFBFBD>ļ<EFBFBD>ֵ:\n",n);
for(i=1;i<=n;i++)cin>>a[i].v;
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>%d<><64><EFBFBD><EFBFBD>Ʒ<EFBFBD><C6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:\n",n);
for(i=1;i<=n;i++)cin>>a[i].w;
dfs(1);
printf("<EFBFBD><EFBFBD><EFBFBD>ż<EFBFBD>ֵΪ<EFBFBD><EFBFBD>%d\n",bv);
cout<<"<EFBFBD><EFBFBD><EFBFBD>Ž<EFBFBD>Ϊ<EFBFBD><EFBFBD>"<<endl;
for(i=1;i<=n;i++)cout<<bx[i]<<'\t';
cout<<endl;
return 0;
}