这道题有人用遗传算法做吗

P1507 NASA的食物计划

有人帮蒟蒻改代码吗 ```cpp #include<cstdio> #include<cstdlib> #include<ctime> #include<cctype> inline double random(double from,double to){ double ans=rand()\*1.0/RAND\_MAX\*(to-from); ans+=from; return ans; } inline long long max(long long a,long long b){ return a>b?a:b; } inline long long read(){ long long num=0,f=1; char ch=getchar(); while(!isdigit(ch)){ if(ch=='-') f=-1; ch=getchar(); } while(isdigit(ch)){ num=num\*10+ch-'0'; ch=getchar(); } return num\*f; } int V,M,n; long long tot; int w[10000]; int d[10000]; int c[10000]; int size; long long answer; struct Biome{ bool DNA[55]; int score; inline int GetScore(){ int maxv=0,maxm=0; int ans=0; for(int i=1;i<=n;++i){ maxv+=d[i]\*DNA[i]; maxm+=w[i]\*DNA[i]; ans+=c[i]\*DNA[i]; if(maxv>V||maxm>M) return 0; } tot+=ans; answer=max(answer,ans); return ans; } }f[1000000]; inline int GetParent(){ long long sum=random(0,tot); long long ans=0; for(int i=1;i<=size;++i){ ans+=f[i].score; if(ans>=sum) return i; } } inline void multi(int fa,int mo){ size++; for(int i=1;i<=n;++i){ double rando=random(0,1); if(rando>0.5) f[size].DNA[i]=f[fa].DNA[i]; else f[size].DNA[i]=f[mo].DNA[i]; if(rando>=0.49&&rando<=0.51) f[size].DNA[i]=!f[size].DNA[i]; } } int main(){ srand(time(NULL)); V=read(),M=read(),n=read(); for(int i=1;i<=n;++i){ w[i]=read(); d[i]=read(); c[i]=read(); } for(int i=1;i<=1000;++i){ size++; for(int j=1;j<=n;++j){ double rando=random(0,1); if(rando>0.5) f[i].DNA[j]=1; else f[i].DNA[j]=0; } f[i].score=f[i].GetScore(); } for(int i=1;i<=10000;++i){ int father=GetParent(),mother=GetParent(); multi(father,mother); f[size].score=f[size].GetScore(); } printf("%lld",answer); }
by 封癫 @ 2017-05-26 19:00:20


蒟蒻遗传已炸
by 封癫 @ 2017-05-26 19:00:35


遗传是最不高深的(胡说)
by zht467 @ 2017-05-26 19:06:55


%%%大佬
by 子谦。 @ 2017-05-31 18:46:56


%%%dalao
by angleの奏 @ 2017-07-18 18:56:03


%%%dalao
by yzhang @ 2017-07-25 11:01:01


#大佬
by 无妹的妹控 @ 2017-07-27 18:49:55


******大佬******
by 吴海啸 @ 2017-07-31 12:07:19


\_**dalao**\_
by 吴海啸 @ 2017-07-31 12:07:49


# 大佬 ## %%%%%
by M_sea @ 2017-08-01 19:53:06


| 下一页