HDU 1301 Jungle Roads

Source: Internet
Author: User

The meaning of this question is to give you n related points, represented by A-I, and then given n-1 rows, the I-th line represents the relevant information from vertex I to other points.
Based on the given map, we need to select an appropriate route so that all given points can reach any other point. The problem is of small scale and the direct matrix is used.
Storage, using the prim algorithm. 1 # include <stdio. h>
2 # include <stdlib. h>
3 # include <string. h>
4 # include <iostream>
5 using namespace std;
6
7 const int MAX = 0x7ffffff;
8 int map [27] [27], MIN, v [27], n, dis, m, sum, flag;
9 // map [I] [j] indicates the distance from point I to j.
10 char cx, cy;
11
12 void Reset (int n)
13 {
14 map [n] [n];
15 memset (map, 0, sizeof (map ));
16 for (int I = 0; I <n-1; I ++)
17 {
18 cin> cx> m;
19 for (int j = 0; j <m; j ++)
20 {
21 cin> cy> dis;
22 map [cx-'a'] [cy-'a'] = map [cy-'a'] [cx-'a'] = dis;
23 // note the conversion between subscripts and characters
24}
25}
26
27 for (int I = 0; I <n; I ++)
28 {
29 for (int j = 0; j <n; j ++)
30 {
31 if (map [I] [j] = 0) map [I] [j] = MAX;
32}
33}
34
35}
36
37 int MinTree (int n)
38 {// prim algorithm
39 v [n];
40 memset (v, 0, sizeof (v ));
41 v [0] = 1;
42 sum = 0;
43 for (int I = 1; I <n; I ++)
44 {
45 MIN = MAX;
46 for (int j = 0; j <n; j ++)
47 {
48 if (! V [j] & map [0] [j] <MIN)
49 {
50 MIN = map [0] [j];
51 flag = j;
52}
53}
54 sum + = MIN;
55 v [flag] = 1;
56 for (int j = 0; j <n; j ++)
57 {
58 if (! V [j] & map [0] [j]> map [flag] [j])
59 {
60 map [0] [j] = map [flag] [j];
61}
62}
63}
64 printf ("% d \ n", sum );
65}
66
67 int main ()
68 {
69 while (scanf ("% d", & n), n)
70 {
71 Reset (n );
72 MinTree (n );
73}
74 return 0;
75}
76

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.