#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
inline double dbrand(){return (double)(rand()%10000)/10000.0;}
int n,Au,Av,Bu,Bv,ans;
int mp[310][310],bel[310];
int atp,a[310],btp,b[310],au,av,bu,bv;
int calc()
{
atp=0,btp=0;
for(int i=1;i<=n;i++)
if(bel[i]==0)a[++atp]=i;
else b[++btp]=i;
int A=-1,B=-1;
for(int i=1;i<=atp;i++)
for(int j=i+1;j<=atp;j++)
if(mp[a[i]][a[j]]>A)A=mp[a[i]][a[j]],au=a[i],av=a[j];
for(int i=1;i<=btp;i++)
for(int j=i+1;j<=btp;j++)
if(mp[b[i]][b[j]]>B)B=mp[b[i]][b[j]],bu=b[i],bv=b[j];
if(A==-1)A=0;
if(B==-1)B=0;
if(A+B<ans)ans=A+B;
return A+B;
}
void annealing()
{
double T=1e5;
int num=calc();Au=au,Av=av,Bu=bu,Bv=bv;
while(T>1e-3)
{
for(int i=1;i<=1000;i++)
{
if(i%10==0)
{
bool bk=false; int cc=0;
for(int p=0;p<=3;p++)
{
int x;
if(p==0)x=Au;
else if(p==1)x=Av;
else if(p==2)x=Bu;
else if(p==3)x=Bv;
bel[x]^=1;
int ddd=calc();
if(ddd<num)
num=ddd,Au=au,Av=av,Bu=bu,Bv=bv;
else bel[x]^=1,cc++;
T*=0.97;
}
if(cc==4)break;
}
int p=rand()%4;
int x;
if(p==0)x=Au;
else if(p==1)x=Av;
else if(p==2)x=Bu;
else if(p==3)x=Bv;
bel[x]^=1;
int ddd=calc();
if(ddd<num)
num=ddd,Au=au,Av=av,Bu=bu,Bv=bv;
else bel[x]^=1;
T*=0.97;
}
for(int i=1;i<=100;i++)
{
int x=rand()%n+1;
bel[x]^=1;
int ddd=calc();
int delta=ddd-num;
if(ddd<num||exp(delta/T)>=dbrand())
num=ddd,Au=au,Av=av,Bu=bu,Bv=bv;
else bel[x]^=1;
T*=0.97;
}
}
for(int i=1;i<=100;i++)
{
int x=rand()%n+1;
bel[x]^=1;
int ddd=calc();
bel[x]^=1;
}
}
int main()
{
srand(2003);
while(scanf("%d",&n)!=EOF)
{
int mx=0,sx;
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++)
{
scanf("%d",&mp[i][j]),mp[j][i]=mp[i][j];
if(mx<mp[i][j])
{
sx=mx;
mx=mp[i][j];
}
else sx=max(sx,mp[i][j]);
}
ans=sx;
for(int i=1;i<=20;i++)
{
for(int j=1;j<=n;j++)bel[j]=rand()%2;
annealing();
}
printf("%d\n",ans);
}
return 0;
}
14670:佛罗里达
最新推荐文章于 2024-09-02 23:29:10 发布