HDU 1162 Eddy ' s picture (graph theory-minimal spanning tree)

Source: Internet
Author: User

The topics are as follows:

Eddy ' s pictureTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 7267 Accepted Submission (s): 3676


Problem Descriptioneddy begins to like painting pictures recently, he's sure of himself to become a painter. Every day Eddy draws pictures in his small, and he usually puts off his newest pictures to let his friends appreciate . But the result of it can be imagined, the friends is not a interested in its picture. Eddy feels very puzzled,in order to change all friends's view to he technical of painting pictures, so Eddy creates a PR Oblem for the He Friends of you.
Problem descriptions as Follows:given 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 <=, the number of a point. For each point, a line follows; Each following line contains and 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 both decimal places:the minimum total length of ink lines that can Conne CT all the points.

Sample Input
31.0 1.02.0 2.02.0 4.0

Sample Output
3.41


Main topic:

Given the coordinates of N points (two-dimensional x, y), you can connect any two points using a straight line (no direction) to connect all the points together to form a whole (so that any two points can be reached), the minimum distance of the line.

A simple minimum spanning tree problem, where the right side to be based on the coordinates of the input points to find out, at the time of the input storage point array subscript can be considered the number of the point, through the complexity of O (n^2) to find any two different points between the distance as the right, the edge of the two vertices is the distance between the two points. Then there is the simple minimum spanning tree algorithm, which can be used with Kruskal or prim.

Kruskal algorithm Implementation

#include <cstdio> #include <vector> #include <algorithm> #include <cmath>using namespace std; const int N = 105;struct edge{int x, y;double w;}; struct point{double x;double y;}; int pre[n]; Point Point[n]; Edge edges[n * N/2];int i_p, i_e, cnt;double res;int root (int x) {if (x! = Pre[x]) {pre[x] = root (Pre[x]);} return pre[x];} bool Merge (int x, int y) {int fx = root (x); int fy = root (y); bool ret = FALSE;IF (FX! = FY) {Pre[fx] = Pre[fy];ret = True;--c NT;} return ret;} void init (int n) {cnt = N;res = 0;for (int i = 0; I <= N; ++i) {pre[i] = i;}} BOOL CMP (const edge &a, const edge &b) {return A.W < B.W;} int main () {int n;double dx, dy;while (scanf ("%d", &n)! = EOF) {init (n); i_e = i_p = 0;for (int i = 0; i < n; ++i) {SCA NF ("%lf%lf", &AMP;DX, &dy);p oint[i_p].x = Dx;point[i_p].y = dy;++i_p;} for (int i = 0, i < n; ++i) {for (int j = i + 1; j < n; ++j) {edges[i_e].x = I;edges[i_e].y = j;double dd = (Point[i]. x-point[j].x) * (point[i].x-point[j].x);dd + = (pOINT[I].Y-POINT[J].Y) * (POINT[I].Y-POINT[J].Y); edges[i_e].w = sqrt (dd); ++i_e;}} Sort (edges, edges + i_e, CMP);//the cnt = = 1 indicates that the mixnum spanning tree is builded sucessfully.for (int i = 0 ; I < i_e && cnt! = 1; ++i) {if (merge (edges[i].x, edges[i].y)) res + = EDGES[I].W; printf ("%.2lf\n", res);} return 0;}



HDU 1162 Eddy ' s picture (graph theory-minimal spanning tree)

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.