(hdu step 6.1.2)Eddy's picture(在只給出二維座標點的情況下,求讓n個點連通的最小費用),hdu6.1.2
題目:
| Eddy's picture |
| Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) |
| Total Submission(s): 172 Accepted Submission(s): 126 |
| |
Problem DescriptionEddy 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?
|
InputThe 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.
|
OutputYour 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
31.0 1.02.0 2.02.0 4.0 |
Sample Output
3.41 |
| Authoreddy |
| |
| RecommendJGShining |
題目分析:
使用kruscal來求最小產生樹,簡單題。這道題和上一道題比較的特點就在於:
1)有一維座標點變成二維座標點。
struct Point{
double x;
double y;
}points[maxn];
2)點與點之間的距離不再直接給出,而需要你自己算。
edges[cnt++].weight = get_distance(points[i],points[j]);//這是點與點之間的距離不再是直接給出而是需要你自己算
代碼如下:
/* * b.cpp * * Created on: 2015年3月9日 * Author: Administrator */#include <iostream>#include <cstdio>#include <cmath>#include <algorithm>using namespace std;const int maxn = 101;struct Point{double x;double y;}points[maxn];struct Edge{int begin;int end;double weight;}edges[maxn*maxn];int father[maxn];int find(int a){if(a == father[a]){return a;}return father[a] = find(father[a]);}double kruscal(int count){int i;for(i = 1 ; i < maxn ; ++i){father[i] = i;}double sum = 0;for(i = 1 ; i <= count ; ++i){int fx = find(edges[i].begin);int fy = find(edges[i].end);if(fx != fy){father[fx] = fy;sum += edges[i].weight;}}return sum;}double get_distance(Point a,Point b){return sqrt(pow(b.x-a.x,2)+pow(b.y-a.y,2));}bool cmp(Edge a,Edge b){return a.weight < b.weight;}int main(){int n;while(scanf("%d",&n)!=EOF){int i;for(i = 1 ; i <= n ; ++i){scanf("%lf%lf",&points[i].x,&points[i].y);}int cnt = 1;int j;for(i = 1 ; i <= n ; ++i){//枚舉所有能形成邊的情況for(j = i+1 ; j <= n ; ++j){edges[cnt].begin = i;edges[cnt].end = j;edges[cnt++].weight = get_distance(points[i],points[j]);//這是點與點之間的距離不再是直接給出而是需要你自己算}}cnt -= 1;sort(edges+1,edges+1+cnt,cmp);printf("%.2lf\n",kruscal(cnt));}return 0;}