求鏈接二維空間中的點 最短的距離 (hdu 1162) kru

Eddy's picture

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10049    Accepted Submission(s): 5072


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. 
#include <cstdio>  
#include <cstring>  
#include <algorithm>  
#include <iostream>  
#include <cmath>
#define MaxV 100005  
#define MaxE 200005  
using namespace std;  
struct edge{  
    int u,v;  
    double cost;  
}e[MaxE];  
struct Node{
	double x,y;
}node[105];
int fa[MaxV];  
bool cmp(edge a,edge b)  
{  
    return a.cost<b.cost;  
}  
int findfa(int x)  
{  
    int a=x;  
    while(x!=fa[x])  
    {  
        x=fa[x];  
    }  
    while(a!=fa[a])   //路徑壓縮   
    {  
        int z=a;  
        a=fa[a];  
        fa[z]=x;  
    }  
    return x;  
}  
  
double kru(int n,int m)  
{  
    double ans1=0;  
    int num_edge=0;  
    for(int i=0;i<=n;i++)  
    {  
        fa[i]=i;  
    }  
    sort(e,e+m,cmp);  
  
    for(int i=0;i<m;i++)  
    {  
        int fau=findfa(e[i].u);  
        int fav=findfa(e[i].v);  
          
        if(fau!=fav)  
        {  
            fa[fau]=fav;  
            ans1=ans1+e[i].cost;  
            num_edge++;  
            if(num_edge==n-1) break;       
         }   
             
    }  
    return ans1;  
}  
int main()  
{  
    int n,m,x;    
    while(~scanf("%d",&n))
    {
	    m=(n*(n-1))/2;
	    for(int i=1;i<=n;i++)  
	    {  
	        scanf("%lf %lf",&node[i].x,&node[i].y);  
	        
	    }
	    int k=0;
	    for(int i=1;i<n;i++)
	    {
	    	for(int j=i+1;j<=n;j++)
	    	{
	    		e[k].u=i;
	    		e[k].v=j;
	    		e[k].cost=sqrt((node[i].x-node[j].x)*(node[i].x-node[j].x)+(node[i].y-node[j].y)*(node[i].y-node[j].y));
	    		k++;
			}
		}   
	    double ans=kru(n,m);
	    printf("%.2lf\n",ans);
	}
	  
    return 0;  
}


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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章