1162 Eddy's picture

来源:互联网 发布:js中的深拷贝和浅拷贝 编辑:程序博客网 时间:2024/05/21 05:20

Eddy's picture

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


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
31.0 1.02.0 2.02.0 4.0
 

Sample Output
3.41
 

Author
eddy
 
做题一看这一题就知道是最小生成树的问题。但是一直纠结与点。想想每一个点与另一个点之间相连接,要五千多个数,然后就惧怕了,再后来做到按距离排序的时候不知道怎么排序了,反正就是没有坚持下来。这道题不难,典型的最小生成树问题




#include<iostream>#include<cstring>#include<algorithm>#include<cmath>using namespace std;struct point{    int x,y;    double l;}p[5600];int set[5600];int cmp(point x,point y){    return x.l<y.l;}int find(int x){    int r=x;    while(r!=set[r])    {        r=set[r];    }    int j=x,i;    while(set[j]!=r)    {        i=set[j];        set[j]=r;        j=i;    }    return r;}int join(point k){    int fx=find(k.x);        int fy=find(k.y);    if(fx!=fy)    {        set[fx]=fy;        return 1;    }    return 0;}int main(){  int n;    while( cin>>n )   {     double a[102][2];         double countn=0;      for(int i=0;i<n;i++)           cin>>a[i][0]>>a[i][1];    int k=0;        for(int i=0;i<n-1;i++)         for(int j=i+1;j<n;j++)          {              p[k].x=i;              p[k].y=j;              p[k].l=sqrt((a[i][0]-a[j][0])*(a[i][0]-a[j][0])+(a[i][1]-a[j][1])*(a[i][1]-a[j][1]));              k++;            }             sort(p,p+k,cmp);             for(int i=0;i<(n+1)*n/2;i++)             set[i]=i;           int num=0;            for(int i=0;i<k;i++)           {               if(join(p[i]))           {            countn+=p[i].l;                  num++;           }             if(num==(n-1))             break;           }            printf("%0.2lf\n",countn);   }        return 0;}


0 0
原创粉丝点击