HDU 1301:jungle Roads "Prim"

Source: Internet
Author: User

Jungle RoadsTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 5516 Accepted Submission (s): 3981


Problem 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.

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.

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

Ac-code:

#include <cstdio> #include <cstring> #include <algorithm> #define INF 100000int s[30][30],n; Using namespace Std;void Prim () {int Vis[30],min,dis[30],sum,i,v,k;memset (vis,0,sizeof (VIS)), for (i=1;i<n;i++) dis [I]=s[0][i];d is[0]=0;vis[0]=1;sum=0;for (i=1;i<n;i++) {min=inf;for (v=0;v<n;v++) if (!vis[v]&&dis[v] <min) {min=dis[v];k=v;} Sum+=min;vis[k]=1;for (v=0;v<n;v++) if (!vis[v]&&dis[v]>s[k][v]) dis[v]=s[k][v];} printf ("%d\n", sum); return;} int main () {int I,j,m,a,v,b,dis;char zh,ch;while (scanf ("%d", &n), N) {GetChar (); for (i=0;i<n;i++) for (j=0;j<n ; j + +) S[i][j]=inf; for (v=1;v<n;v++) {scanf ("%c%d", &zh,&m), A=zh-65;for (i=0;i<m;i++) {scanf ("%c%d", &ch,&dis); b= Ch-65;s[a][b]=s[b][a]=dis;} GetChar ();} Prim ();} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 1301:jungle Roads "Prim"

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.