Exercise on a minimum spanning tree template question in HDU

Source: Internet
Author: User

Statistics

Eddy's picture

Time Limit: 2000/1000 ms (Java/Other) Memory Limit: 65536/32768 K (Java/Other)

Total Submission (s): 7 Accepted Submission (s): 4

Font: Times New Roman | Verdana | Georgia

Font Size: bytes →

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

 

 

Well, this question is a very simple problem of the minimal spanning tree. The general idea is to give you the coordinates of n points, so that you can connect these points and the path is shortest, obviously, this question can be applied using the template of the Prim algorithm, but the only thing to note is that this question gives you the coordinates of n points, you need to convert the coordinates one by one into the edge lengths of the n points and then perform the Prim algorithm. Therefore, you have to add a function before that to calculate the distance between two points, of course, it is much easier to store the coordinates of n points in the structure. Then, after the preliminary work is completed, you can input and output the coordinates according to the question... Of course, we accidentally wrote memset (vis, 0, sizeof (0) When initializing the vis array )) however, there is only one group in the example, so I have not managed the results.
After reading the example, I handed in two results and connected them to WA. Next time, this low-level error should be avoided as much as possible = The Code is as follows:

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespace std;double d[10005];double map[10005][10005];int vis[10005];int n;const double MAX=1000000.0;struct  Point {    double x;    double y; }point[10005];double  dis(int i,int j){ return (double)sqrt((point[i].x-point[j].x)*(point[i].x-point[j].x)+(point[i].y-point[j].y)*(point[i].y-point[j].y));}double   Prim(int n){    int i,j,id;    double mins,ans=0;    for(i=0;i<n;i++)    {        d[i]=map[0][i];    }     memset(vis,0,sizeof(vis));    for(j=0;j<n;j++)    {   mins=MAX;        id=-1;        for(i=0;i<n;i++)        {            if(vis[i]==0&&mins>d[i])            {                mins=d[i];                id=i;            }        }            if(mins==MAX)  return -1;            vis[id]=1;            ans+=mins;            for(i=0;i<n;i++)            {                if(vis[i]==0)                    d[i]=min(d[i],map[id][i]);            }    }   return ans;} int main() {     int i,j;     while(scanf("%d",&n)!=EOF)     {         for(i=0;i<n;i++)         {             cin>>point[i].x>>point[i].y;         }            for(i=0;i<n;i++)            {                 for(j=0;j<n;j++)                {                    map[i][j]=map[j][i]=dis(i,j);                }            }            printf("%.2lf\n",Prim(n));     }   return 0; }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.