模拟退火的时候Float Exception RE是什么鬼啊啊啊啊
代码粘下面:
#include<bits/stdc++.h>
using namespace std;
const long double temp=1e4;
const long double drop=0.92;
const long double stop=1e-8;
int t,n,p1,p2,l[31],a[16],b[16];
long long ans=0x3f3f3f3f;
long long energy(){
int sum1=0,sum2=0;
for(int i=0;i<p1;i++) sum1+=a[i];
for(int j=0;j<p2;j++) sum2+=b[j];
return abs(sum1-sum2);
}
void solve(){
double nowTemp=temp;
long long endE;
while(nowTemp>stop){
nowTemp*=drop;
int d1=rand()%p1,d2=rand()%p2;
swap(a[d1],b[d2]);
endE=energy();
if(endE==0||ans==0){
ans=0; return;
}
if(ans>=endE) {ans=endE;continue;}
else{
double display=nowTemp/(endE-ans);
double accept=rand()/RAND_MAX;
if(display>25){
ans=endE; continue;
}
else if(exp(display)>accept){
ans=endE; continue;
}
else{
swap(a[d1],b[d2]);
}
}
}
}
int main(){
srand(time(NULL));
cin>>t;
while(t--){
cin>>n; ans=0x3f3f3f3f;
memset(l,0,256); p1=p2=0;
for(int i=0;i<n;i++) cin>>l[i];
if(n==1){
cout<<l[0]<<endl;
continue;
}
for(int i=0;i<n;i++){
if(i%2){
a[p1++]=l[i];
}
else b[p2++]=l[i];
}
for(int j=0;j<3;j++) solve();
cout<<ans<<endl;
}
}