POJ1861 Jungle Roads (Kruskal) (also set)

Source: Internet
Author: User

Jungle Roads
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 23882 Accepted: 11193

Description


The Head Elder of the tropical island of Lagrishan have a problem. A burst of foreign aid money is spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly and so the large road network are too expensive to maintain. The Council of elders must choose to stop maintaining some roads. The map above on the left shows all the roads with use now and the cost in aacms per month to maintain them. Of course there needs to be some-to-get between all the villages on maintained roads, even if the route was not as Shor T as before. The chief Elder would like to tell the Council of the elders what would is the smallest amount they could spend in aacms per m Onth to maintain roads this would connect all the villages. The villages is labeled A through I in the maps above. The map on the right shows the roads that could is maintained most cheaply, for 216 aacms per month. Your task is to write a program that would solve such problems.

input

The input consists of one to the data sets, followed by a final line containing only 0. Each data set starts with a line containing only a number n, which is the number of villages, 1 < N <, and the VI Llages is labeled with the first n letters of the alphabet, capitalized. Each data set was completed with n-1 lines this start with the village labels in alphabetical order. There is no line for the last village. Each line for a village starts with the village label followed by a number, K, of roads from this village to villages with Labels later in the alphabet. If K is greater than 0, the line continues with data for each of the K roads. The data for each road was the village label for the other end of the road followed by the monthly maintenance cost in AACM s for the road. Maintenance costs is positive integers less than 100. All data fields in the row is separated by single blanks. The road network would always be allow to travel between all the villages. The network would never have more than roads. No Village'll has more than roads going to other villages (before or after the alphabet). In the sample input below, the first data set goes with the map above.

Output

The output is an integer per line for each data set:the minimum cost of aacms per month to maintain a road system that C Onnect all the villages. Caution:a Brute force solution that examines every possible set of roads would not finish within the one minute 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
"Analysis" words do not say, is the smallest spanning tree, the direct set of templates.

#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<algorithm>#include<climits>#include<cstring>#include<string>#include<Set>#include<map>#include<queue>#include<stack>#include<vector>#include<list>#include<functional>#defineMoD 1000000007#defineINF 0x3f3f3f3f#definePi ACOs (-1.0)using namespaceStd;typedefLong Longll;Const intn=1005;Const intm=15005;structEDG {intV,u; intW;} EDG[M];BOOLCMP (EDG g,edg h) {returng.w<H.W;}intn,m,maxn,cnt;intParent[n];voidinit () { for(intI=0; i<n; i++) parent[i]=i;}voidBuild () {intw,k; Charu,v;  for(intI=0; i<n-1; i++) {cin>>u>>K;  while(k--) {cin>>v>>W; edg[++cnt].u=u-'A'; EDG[CNT].V=v-'A'; EDG[CNT].W=W; }} sort (Edg,edg+cnt+1, CMP);}intFind (intx) {if(parent[x]! = x) Parent[x] =Find (parent[x]); returnparent[x];}//finds and returns the root node of the collection to which node x belongsvoidUnion (intXinty) {x=Find (x); Y=Find (y); if(x = = y)return; Parent[y]=x;}//merging elements from two different collectionsvoidKruskal () {intsum=0; intnum=0; intu,v;  for(intI=0; i<=cnt; i++) {u=edg[i].u; V=edg[i].v; if(Find (u)! =Find (v)) {Sum+=EDG[I].W; Num++;        Union (U,V); }        //printf ("!!! %d%d\n ", num,sum);        if(num>=n-1) {printf ("%d\n", sum);  Break; }    }}intMain () { while(~SCANF ("%d", &n) &&N) {cnt=-1;        Init ();        Build ();    Kruskal (); }    return 0;}
View Code

POJ1861 Jungle Roads (Kruskal) (also set)

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.