POJ 1251--jungle Roads (minimum spanning tree)

Source: Internet
Author: User

Jungle Roads
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 20154 Accepted: 9291

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

Source

POJ 1251--jungle Roads code: Prim algorithm to find the minimum spanning tree
1#include <iostream>2#include <cstdio>3#include <string>4#include <cstring>5 using namespacestd;6 7 intN;8 intdis[ -][ -];9 intmindis[ -];Ten intvis[ -]; One  A intPrim () { -     intI,j,sum =0; -memset (Vis,0,sizeof(Vis)); thememset (Mindis,0,sizeof(Mindis)); -      for(i =0; I < n; i++) -Mindis[i] = dis[0][i];//There is only one point in the starting set, and the distance to the set is the distance to the first point. -vis[0] =1; +     intnum=n-1, jilu=0; -      while(num--){ +         intMin = +; A          for(i =0; I < n; i++){ at             if(!vis[i]&&mindis[i] <min) { -min = mindis[i];//Update min -Jilu =i; -             } -         } -     //cout << "added point is:" <<jilu<< "sum+ min is:" << min << Endl; in         //if (min!=1000) -Sum + =min; toVis[jilu] =1;//new Jilu points into the collection +          for(j =0; J < N; J + +) {//the shortest distance from the update point to the collection -             if(Vis[j]) the                 Continue; *             Else if(mindis[j]>Dis[jilu][j]) $MINDIS[J] =Dis[jilu][j];Panax Notoginseng         } -     } the  +     returnsum; A } the  + intMain () - { $      while(Cin >> n&&n!=0){ $         intI, J, K, X, Y, W, M; -         CharA, B; -          for(i =0; I < n; i++) the              for(j =0; J < N; J + +) -DIS[I][J] = Dis[j][i] = +;Wuyi          for(i =0; I < n-1; i++){ theCin >> a >>m; -             if(M >0){ Wu                  for(j =0; J < M; J + +){ -CIN >> B >>W; Aboutx = A-'A'; $y = B-'A'; -Dis[x][y] = dis[y][x] =W; -                 } -             } A         } +cout << Prim () <<Endl; the     } -     return 0; $}

POJ 1251--jungle Roads (minimum spanning tree)

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.