Pagini recente » Cod sursa (job #2115991) | Cod sursa (job #2852213) | Cod sursa (job #1009330) | Cod sursa (job #2470346) | Cod sursa (job #573893)
Cod sursa(job #573893)
#include <stdio.h>
#include <string.h>
#define Nmax 17
#define INF 2147000000
int a[1<<Nmax][Nmax];
int x[2][Nmax+1],y[2][Nmax+1],val[2][Nmax+1],d[Nmax+1][Nmax+1];
int N,st;
inline int Minim(int x,int y){ return x<y ? x:y; }
inline int D(int x1,int x2,int y1,int y2){
return (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
}
inline void work(){
int i,j,dist,sol,conf;
memset(val[st],0,sizeof(val[st]));
for(i=1;i<=N;++i)
for(j=1;j<=N;++j)
if(i!=j)
d[i][j]=D(x[st][i],x[st][j],y[st][i],y[st][j]);
for(i=0;i<N;++i){
dist=INF;
for(j=1;j<=val[st^1][0];++j)
dist = Minim(dist, val[st^1][j] + D(x[st][i+1],x[st^1][j],y[st][i+1],y[st^1][j]));
a[1<<i][i]=dist;
}
for( conf=1; conf<(1<<N); ++conf )
if( conf&(conf-1) )
for(i=0; (1<<i) <= conf;++i)
if( conf&(1<<i) ){
a[conf][i]=INF;
for(j=0; (1<<j) <= conf; ++j)
if( (conf&(1<<j)) && j!=i )
a[conf][i]=Minim(a[conf][i],a[conf^(1<<i)][j] + d[i+1][j+1]);
}
sol=INF;
for(i=0;i<N;++i){
val[st][i+1]=a[(1<<N)-1][i];
sol=Minim(sol,val[st][i+1]);
}
val[st][0]=N;
printf("%d\n",sol);
}
int main(){
int i,tip;
freopen("bibel.in","r",stdin);
freopen("bibel.out","w",stdout);
val[1][val[1][0]=1]=0;
st=0; N=0;
do{
scanf("%d",&tip);
if( !tip ) ++N,scanf("%d%d",&x[st][N],&y[st][N]);
else{
if( tip==1 ){
work();
N=0;
st ^=1;
}
}
} while( tip != 2 );
fclose(stdin); fclose(stdout);
return 0;
}