Pagini recente » iconcurs10 | Cod sursa (job #481105) | Cod sursa (job #48632) | Cod sursa (job #1353391) | Cod sursa (job #152275)
Cod sursa(job #152275)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define pow(x) (1u<<(x))
#define sqr(x) ((x)*(x))
int x[20],y[20],px[20],py[20],plus[20],n,np,sol[1<<17][17],dist[17][32],j,i;
inline void improve(unsigned m){
#define W(j) if (!(m & pow(j)) && sol[m][i]+dist[i][j]<sol[m+pow(j)][j])
sol[m+pow(j)][j] = sol[m][i]+dist[i][j];
for (unsigned i=0; i<17; i++)
if(m & pow(i)){
W(0) W(1) W(2) W(3) W(4) W(5) W(6) W(7) W(8) W(9) W(10)
W(10) W(11) W(12) W(13) W(14) W(15) W(16);
}
}
void solve(){
memset(sol,0x7f,(17*4)<<n);
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
dist[i][j]=(sqr(x[i]-x[j])+sqr(y[i]-y[j]));
for (int i=0;i<n;i++)
for (int j=0;j<np;j++)
if (sqr(x[i]-px[j])+sqr(y[i]-py[j])+plus[j]<sol[pow(i)][i])
sol[pow(i)][i]=sqr(x[i]-px[j])+sqr(y[i]-py[j])+plus[j];
int last=(1<<n)-1;
for(int m=1;m<last;m++)
improve(m);
np=n;
int min=sol[last][0];
for (int i=0;i<n;i++){
if (sol[last][i]<min)
min=sol[last][i];
px[i]=x[i],py[i]=y[i],plus[i]=sol[last][i];
}
printf("%d\n", min);
n=0;
}
int main(){
freopen("bibel.in", "r", stdin);
freopen("bibel.out", "w", stdout);
np=1;
while(1){
int op;
scanf("%d",&op);
if(op==0)
scanf("%d %d",x+n,y+n), n++;
else
if(op==1)
solve();
else{
fclose(stdin);
fclose(stdout);
return 0;
}
}
}