题目的意思就是给出这么多点,找出一种连接方式,是距离总和最小。(注意两个点间的距离孙万要加16,题目要求,不用深究)
做法就是就是枚举点的全排列,找出距离最小的。。
AC代码:
#include<iostream>
#include<algorithm>
#include<cmath>
#include<stdio.h>
using namespace std;
const int N = 200;
struct point {
double x;
double y;
};
int t;
int temp[N];
point p[N];
int fin[N];
double k[N];
double k2[N];
double res;
int T = 1;
double cul () {
double f = 0;
for (int i = 0 ; i < t - 1 ;i++) {
k[i] = sqrt(pow(p[temp[i]].x - p[temp[i + 1]].x , 2) + pow(p[temp[i]].y - p[temp[i + 1]].y , 2)) + 16;
}
for (int i = 0 ; i < t - 1; i++) {
f += k[i];
}
return f;
}
int main () {
while (cin >> t && t) {
for (int i = 0 ; i < t ; i++) {
cin >> p[i].x >> p[i].y;
temp[i] = i;
}
double m = 100000000000;
do {
double flag = cul();
if (flag < m) {
m = flag;
for (int i = 0 ; i < t ;i++) {
fin[i] = temp[i];
if (i != t - 1)
k2[i] = k[i];
}
}
}while(next_permutation(temp , temp + t));
cout <<"**********************************************************" <<endl;
cout <<"Network #"<<T++<<endl;
for (int i = 0 ; i < t - 1 ;i++) {
printf("Cable requirement to connect (%.0lf,%.0lf) to (%.0lf,%.0lf) is %.2lf feet.\n",p[fin[i]].x, p[fin[i]].y, p[fin[i + 1]].x, p[fin[i + 1]].y ,k2[i] );
}
printf("Number of feet of cable required is %.2lf.\n",m);
}
return 0 ;
}