Jungle RoadsTime
limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 6737 Accepted Submission (s): 4893
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
Sourcemid-central USA 2002
Original title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1301
http://poj.org/problem?id=1251
Test instructions: Enter N, representing the number of attractions, next n-1 line, each line of the first letter represents the sights, the number behind the number of points connected to it, followed by n pairs of letters and numbers, representing the sights and distances connected to the attraction.
The Prime Algorithm AC code: 0MS.
#include <cstdio>//prime#include <iostream>using namespace std;const int Inf=0x3f3f3f3f;int dis[30];int a[ 30][30];bool vis[30];int n,m;int Prime () {for (int i=0; i<n; i++) {dis[i]=a[0][i]; Vis[i]=false; } dis[0]=0; Vis[0]=true; int ans=0; for (int i=1; i<n; i++) {int p=-1; int minn=inf; for (int j=0; j<n; J + +) {if (!vis[j]&&dis[j]<minn) minn=dis[p=j]; } Ans+=minn; Vis[p]=true; for (int j=1; j<n; J + +) {if (!vis[j]&&dis[j]>a[p][j]) dis[j]=a[p][j]; }} return ans; int main () {while (cin>>n,n) {for (int i=0; i<n; i++) {for (int j=0; j<n; j + +) if (i==j) a[i][j]=0; else A[i][j]=inf; } char Ch1,ch2; int x, y; for (int i=1; i<n; i++) {cin>>ch1>>x;while (x--) {cin>>ch2>>y; if (a[ch1-' a '][ch2-' a ']>y) a[ch1-' A '][ch2-' a ']=a[ch2-' a '][ch1-' a ']=y; }} printf ("%d\n", Prime ()); } return 0;}
Kruskal Algorithm AC code: 15MS
#include <iostream> #include <algorithm>using namespace std;const int inf=0x3f3f3f3f;struct node{int s,e,w; }a[500];int fa[30];int n,m;int Find (int x) {if (x==fa[x]) return x; Return Fa[x]=find (Fa[x]);} BOOL CMP (node A,node b) {return A.W<B.W;} int Kruskal () {sort (a,a+m,cmp); int ans=0; for (int i=0;i<m;i++) {int fx=find (A[I].S); int Fy=find (A[I].E); if (fx!=fy) {ANS+=A[I].W; Fa[fx]=fy; }} return ans; int main () {while (cin>>n,n) {int x, y; Char Ch1,ch2; for (int i=0;i<n;i++) fa[i]=i; m=0; for (int i=1;i<n;i++) {cin>>ch1>>x; while (x--) {cin>>ch2>>y; A[m].s=ch1-' A '; a[m].e=ch2-' A '; A[m++].w=y; }} Cout<<kruskal () <<endl; } return 0;}
HDU 1301 &poj 1215 Jungle Roads "minimum spanning tree, prime algorithm +kruskal algorithm"