#include<stdio.h>
#include<math.h>
#include<string.h>
#define MAX 10
#define INF 100000
double cpx[MAX],cpy[MAX],min;
int b[MAX];
int visit[MAX];
void dfs(int *a,int cur,int n,double s){
int i;
if(cur==n){
if(min>s){
min=s;
for(i=0;i<n;i++){
b[i]=a[i];
}
}
}
else{
for(i=0;i<n;i++){
double temp=16+sqrt((cpx[i]-cpx[a[cur-1]])*(cpx[i]-cpx[a[cur-1]])+(cpy[i]-cpy[a[cur-1]])*(cpy[i]-cpy[a[cur-1]]));
if(visit[i]==0&&min>s+temp){
visit[i]=1;
a[cur]=i;
dfs(a,cur+1,n,s+temp);
visit[i]=0;
}
}
}
}
int main(){
int n,cas=0;
while(scanf("%d",&n)!=EOF&&n!=0){
cas++;
memset(visit,0,sizeof(visit));
int i;
for(i=0;i<n;i++){
scanf("%lf%lf",&cpx[i],&cpy[i]);
}
min=INF;
int a[MAX];
double s;
for(i=0;i<n;i++){
s=0;
visit[i]=1;
a[0]=i;
dfs(a,1,n,s);
visit[i]=0;
}
printf("**********************************************************\n");
printf("Network #%d\n",cas);
for(i=1;i<n;i++)
printf("Cable requirement to connect (%.0lf,%.0lf) to (%.0lf,%.0lf) is %.2lf feet.\n",cpx[b[i-1]],cpy[b[i-1]],cpx[b[i]],cpy[b[i]],16+sqrt((cpx[b[i]]-cpx[b[i-1]])*(cpx[b[i]]-cpx[b[i-1]])+(cpy[b[i]]-cpy[b[i-1]])*(cpy[b[i]]-cpy[b[i-1]])));
printf("Number of feet of cable required is %.2lf.\n",min);
}
return 0;
}
uva216(Getting in Line )-简单回溯
最新推荐文章于 2018-03-28 15:44:44 发布