Problem Description
Eddy begins to like painting pictures recently ,he is sure of himself to become a painter.Every day Eddy draws pictures in his small room, and he usually puts out his newest pictures to let his friends appreciate. but the result it can be imagined, the friends are not interested in his picture.Eddy feels very puzzled,in order to change all friends 's view to his technical of painting pictures ,so Eddy creates a problem for the his friends of you.
Problem descriptions as follows: Given you some coordinates pionts on a drawing paper, every point links with the ink with the straight line, causes all points finally to link in the same place. How many distants does your duty discover the shortest length which the ink draws?
Input
The first line contains 0 < n <= 100, the number of point. For each point, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the point.
Input contains multiple test cases. Process to the end of file.
Output
Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the points.
Sample Input
3
1.0 1.0
2.0 2.0
2.0 4.0
Sample Output
3.41
/*
题意:给N个点的坐标,找N-1条边使N个点连通且N-1条边的权值和最小。
题解:求最小生成树(普利姆算法).
*/
//标程:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
using namespace std;
struct node{
int index;
double value;
};
struct cmp{
bool operator () (node &s, node &s1){
if(s.value != s1.value) return s.value > s1.value;
return s.index > s1.index;
}
};
priority_queue<node,vector<node>,cmp> q;
vector<int> v;
int n, a[110], vis[110];
double x[110], y[110], map[110][110];
double f(int i, int j){
return sqrt(pow(x[i]-x[j],2)+pow(y[i]-y[j],2));
}
int main(){
// freopen("a.txt","r",stdin);
while(cin >> n)
{
v.clear();
int i, j, k;
for(i = 1; i <= n; ++ i)
cin >> x[i] >> y[i];
memset(map,0,sizeof(map));
for(i = 1; i <= n; ++ i){
for(j = i + 1; j <= n; ++ j){
map[i][j] = map[j][i] = f(i,j);
}
}
double distance(0);
memset(vis,0,sizeof(vis));
vis[1] = 1;
v.push_back(1);
for(i = 1; i < n; ++ i)
{
while(!q.empty()) q.pop();
for(j = 0; j < v.size(); ++ j){
for(k = 1; k <= n; ++ k){
if(!vis[k]){
node temp;
temp.index = k;
temp.value = map[v[j]][k];
q.push(temp);
}
}
}
node tmp = q.top();
v.push_back(tmp.index);
vis[tmp.index] = 1;
distance += tmp.value;
}
printf("%.2lf\n",distance);
}
return 0;
}
Eddy's picture
最新推荐文章于 2020-07-26 14:26:18 发布