#图 # #最小生成树 # #kruskal #-----The way in the jungle

Source: Internet
Author: User

Minimum spanning tree

The spanning tree of a connected graph with n nodes is a minimal connected sub-graph of the original, and contains all n nodes in the original image, and has the least number of edges to keep the graph connected. The minimum spanning tree can be calculated using the Kruskal (Kruskal) algorithm or the prim (PRIMM) algorithm.

in a given graph G = (V, E), (U, v) represents the edge of the connection vertex u and Vertex V (that is), and W (U, v) represents the weight of this edge, if there is a subset of E (that is) and is a non-circular graph, so that W (t) is the smallest, then this T is the minimum spanning tree of G.          The minimum spanning tree is actually the abbreviation for the minimum weight spanning tree. Kruskal Algorithmthe algorithm for finding the minimum spanning tree of weighted connected graphs. The Kruskal algorithm selects a total of n-1 edges, and the greedy rule (n points) used is to select from the remaining edges a collection of the least expensive edges that do not produce loops to join the selected edges. It is not possible to form a spanning tree if the selected edges produce loops. The Kruskal algorithm is divided into e-steps, where E is the number of edges in the network. Consider this e-edge in an incremental order, with one edge at a time. When an edge is considered, if a loop is added to the set of selected edges, it is discarded, otherwise it is selected. Openjudge 253: The road in the jungleTotal time limit: 1000msmemory limit: 65536kB
Describe


The head of the tropical island Lagrishan is facing a problem: a few years ago, a group of foreign aid funds had been used to maintain the roads between villages, but the growing jungle had ruthlessly eroded the villagers ' roads, resulting in huge road maintenance costs and the elders had to abandon part of the road maintenance. The diagram on the left shows the schematic of the road being used and the cost per month of repair (in aacms) for each road. Now the Presbyterian Church needs to propose a plan to ensure that the villages can reach each other and keep the cost of road maintenance to a minimum each month. The village is numbered from a to I. The scheme on the right shows minimum maintenance cost of 216 aacms per month.

Input
the input contains 1~100 datasets, the last behavior 0. The first behavior of each data set village number N, 1 < n < 27, followed by the first n letters of the alphabet are marked. Then there are n-1 lines, and the first data in each row is the alphabetical village number (excluding the last village). The data at the back of each village represents the number of roads that the village leads to the village numbered behind it, such as a 2 B 25, which represents a village with 2 numbered villages after a and connected to it. If k is greater than 0,k, the number of the K villages is given in turn, along with the cost of road maintenance to the starting village, such as a 2 B I 25, which represents a 12 road maintenance cost between A and B, The cost of road maintenance between a and I is 25 (a positive integer with a maintenance cost of not more than 100). The total number of roads is not more than 75, and each village will not have more than 15 roads (including numbers before and after them) to other villages.
Output
Each dataset has one output: the small cost of repairing the road per month for the solution.
hint: Brute force algorithm can find a solution, but it will exceed the time limit.
Sample input
9A 2 B I 25B 3 c ten H i 8C 2 D 55D 1 E 44E 2 F g 38F 0G 1 H 35H 1 I 353A 2 B ten C 40B 1 C 200
Sample output
21630
1#include <stdio.h>2#include <string.h>3#include <algorithm>4 using namespacestd;5 6 structnode{7     intu,v;8     intW;9     BOOL operator< (ConstNode & A)Const{Ten     returnw<A.W; One     } A}edge[ the]; - intfa[ -]; - intN, CNT; the  - intGETFA (intx) { -     returnFa[x]=fa[x]==x?X:GETFA (fa[x]); - } +  - voidAddintXintYintW) { +edge[++cnt].u=x; Aedge[cnt].v=y; atedge[cnt].w=W; - } -  - intKruskal () { -     intans=0, cnt1=0; -Sort (edge+1, edge+cnt+1); in  -      for(intI=1; i<=cnt;i++){ to         intU=GETFA (edge[i].u), v=GETFA (EDGE[I].V); +         if(u!=v) { -ans+=EDGE[I].W; theFA[GETFA (U)]=GETFA (v); *             if(++cnt1==n-1) Break;//all points are fully connected $         }Panax Notoginseng     } -     returnans; the } +  A intMain () { the      while(SCANF ("%d", &n) &&n!=0){ +Cnt=0; -memset (FA,0,sizeof(FA)); $Memset (Edge,0,sizeof(Edge)); $          for(intI=1; i<=n;++i) fa[i]=i; -           for(intI=1; i<n;++i) { -            Charc[3];intx; thescanf"%s%d",&c,&x); -            if(x>0)Wuyi               for(intj=1; j<=x;++j) { the                  intYChara[3]; -scanf"%s%d",&a,&y); WuAdd (c[0]- -, a[0]- -, y); -              } About          } $printf"%d\n", Kruskal ()); -     } -     return 0; -}

#图 # #最小生成树 # #kruskal #-----The way in the jungle

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.