Pagini recente » Cod sursa (job #2249793) | Cod sursa (job #3123756) | Cod sursa (job #2333667) | Cod sursa (job #1641865) | Cod sursa (job #533159)
Cod sursa(job #533159)
#include <stdio.h>
#define maxn 1<<17
#define inf 1000000000
struct nrt{
int x,y;
};
int pit(nrt a,nrt b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
};
int p1,cc[2],a,b,d[18][18],sll[maxn][17],dpr[18][18],dpp[18],i,j,k;
nrt v[2][17];
int main()
{
freopen("bibel.in","r",stdin);
freopen("bibel.out","w",stdout);
scanf("%d",&p1);
cc[1]=1;
v[1][0].x=0;
v[1][0].y=0;
int ii=0;
while(p1!=2){
if(p1==0){
scanf("%d%d",&v[ii][cc[ii]].x,&v[ii][cc[ii]].y);
cc[ii]++;
}
if(p1==1){
for(i=0;i<(1<<cc[ii]);i++)
for(j=0;j<cc[ii];j++)
sll[i][j]=inf;
for(i=0;i<cc[ii];i++)
for(j=0;j<cc[ii];j++)
dpr[i][j]=pit(v[ii][i],v[ii][j]);
for(i=0;i<cc[ii];i++)
for(j=0;j<cc[ii^1];j++)
if(dpp[j]+pit(v[ii][i],v[ii^1][j])<sll[1<<i][i])
sll[1<<i][i]=dpp[j]+pit(v[ii][i],v[ii^1][j]);
for(i=0;i<(1<<cc[ii]);i++)
for(j=0;j<cc[ii];j++)
if((1<<j)&i)
for(k=0;k<cc[ii];k++)
if(((1<<k)&i)==0)
if(sll[i][j]+dpr[k][j]<sll[i^(1<<k)][k]){
sll[i+(1<<k)][k]=sll[i][j]+dpr[k][j];
}
int sol=dpp[0]=sll[(1<<cc[ii])-1][0];
for(i=1;i<cc[ii];i++){
dpp[i]=sll[(1<<cc[ii])-1][i];
if(sol>sll[(1<<cc[ii])-1][i]){
sol=sll[(1<<cc[ii])-1][i];
}
}
printf("%d\n",sol);
ii=ii^1;
cc[ii]=0;
}
if(p1!=2){
scanf("%d",&p1);
}
}
return 0;
}