HDU 1233 or smooth Engineering

Source: Internet
Author: User

The question is to give you a graph with n vertices and give n * (n-1)/2 edge information, including the edge endpoint and edge length.
Select the shortest path to build all nodes on the same connected branch. Typical Minimum Spanning Tree Algorithms
If the scale is not large, the direct matrix can be competent. 1 # include <stdlib. h>
2 # include <stdio. h>
3 # include <string. h>
4
5 const int MAX = 100000000;
6 int map [101] [101], MIN, sum;
7 // map [I] [j] records the distance from point I to Point j!
8
9 int main ()
10 {
11 int n, x, y, m, v [101], flag, dis;
12 while (scanf ("% d", & n), n)
13 {
14 map [n] [n];
15 memset (map, 0, sizeof (map); // clear the Array
16 m = (n * (n-1)/2;
17 for (int I = 0; I <m; I ++)
18 {// enter and process edge information
19 scanf ("% d", & x, & y, & dis );
20 map [x-1] [Y-1] = map [Y-1] [x-1] = dis;
21}
22 for (int I = 0; I <n; I ++) map [I] [I] = MAX;
23 // graph creation is complete!
24 v [n];
25 memset (v, 0, sizeof (v ));
26 v [0] = 1;
27 sum = 0;
28 for (int I = 1; I <n; I ++)
29 {// obtain the minimum spanning tree using the prim algorithm
30 MIN = 10000000;
31 for (int j = 0; j <n; j ++)
32 {
33 if (! V [j] & map [0] [j] <MIN)
34 {
35 MIN = map [0] [j];
36 flag = j;
37}
38}
39 sum + = MIN;
40 v [flag] = 1;
41 for (int j = 0; j <n; j ++)
42 {
43 if (! V [j] & map [0] [j]> map [flag] [j])
44 {
45 map [0] [j] = map [flag] [j];
46}
47}
48}
49 printf ("% d \ n", sum );
50}
51 return 0;
52}
53
 

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.