HDU 1162 Eddy's picture

Source: Internet
Author: User

This topic is also a typical minimum spanning tree algorithm, which is similar to the previous one. That is to say, it gives you n two-dimensional plane points,
You can add proper edges so that all vertices are on the same Unicom branch, that is, there are paths between all vertices.
The problem is not large. You can use the prim algorithm to store data in a matrix. The "weight" between any two points is
The distance between two points. 1 # include <stdio. h>
2 # include <stdlib. h>
3 # include <math. h>
4 # include <string. h>
5 const double MAX = 1000000000.0;
6 struct Point
7 {
8 double x, y;
9} point [2, 101];
10
11 double map [0, 101] [101];
12 int v [101], n;
13
14 double Dis (Point a, Point B)
15 {
16 return sqrt (. x-B. x) * (. x-B. x) + (. y-B. y) * (. y-B. y ));
17}
18
19 void Build ()
20 {
21 memset (map, 0, sizeof (map ));
22 for (int I = 0; I <n; I ++)
23 {
24 for (int j = I; j <n; j ++)
25 {
26 if (I = j) map [I] [j] = MAX;
27 else
28 {
29 map [j] [I] = map [I] [j] = Dis (point [I], point [j]);
30}
31}
32}
33}
34
35 void MinTree ()
36 {
37 double sum = 0.0, min;
38 memset (v, 0, sizeof (v ));
39 v [0] = 1;
40 int flag;
41 for (int I = 1; I <n; I ++)
42 {
43 min = MAX;
44 for (int j = 0; j <n; j ++)
45 {
46 if (! V [j] & map [0] [j] <min)
47 {
48 min = map [0] [j];
49 flag = j;
50}
51}
52 sum + = min;
53 v [flag] = 1;
54 for (int j = 0; j <n; j ++)
55 {
56 if (! V [j] & map [0] [j]> map [flag] [j])
57 {
58 map [0] [j] = map [flag] [j];
59}
60}
61}
62 printf ("%. 2lf \ n", sum );
63}
64 int main ()
65 {
66 while (scanf ("% d", & n )! = EOF)
67 {
68 map [n] [n];
69 point [n];
70 for (int I = 0; I <n; I ++)
71 {
72 scanf ("% lf", & point [I]. x, & point [I]. y );
73}
74 Build ();
75 MinTree ();
76}
77 return 0;
78}
79

Related Article

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.