The Head Elder
of the tropical island of Lagrishan has a problem. A burst of foreign aid money
was spent on extra roads between villages some years ago. But the jungle
overtakes roads relentlessly, so the large road network is 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 in use now and the cost in aacms per
month to maintain them. Of course there needs to be some way to get between all
the villages on maintained roads, even if the route is not as short as before.
The Chief Elder would like to tell the Council of Elders what would be the
smallest amount they could spend in aacms per month to maintain roads that would
connect all the villages. The villages are labeled A through I in the maps
above. The map on the right shows the roads that could be maintained most
cheaply, for 216 aacms per month. Your task is to write a program that will
solve such problems.
The input consists of one to 100 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 < 27,
and the villages are labeled with the first n letters of the alphabet,
capitalized. Each data set is completed with n-1 lines that start with 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 is the village label for the other end of the road followed by the monthly
maintenance cost in aacms for the road. Maintenance costs will be positive
integers less than 100. All data fields in the row are separated by single
blanks. The road network will always allow travel between all the villages. The
network will never have more than 75 roads. No village will have more than 15
roads going to other villages (before or after in the alphabet). In the sample
input below, the first data set goes with the map above.
The output
is one integer per line for each data set: the minimum cost in aacms per month
to maintain a road system that connect all the villages. Caution: A brute force
solution that examines every possible set of roads will not finish within the
one minute time limit.
Eddy | We have
carefully selected several similar problems for you:
1213 1198 1116 1269 1054
1 //0MS 324K 1232 B G++
2 #include<iostream>
3 #include<map>
4 struct node{
5 int u,v,d;
6 }p[1005];
7 using namespace std;
8 int set[105],n,cnt;
9 int cmp(const void*a,const void*b)
10 {
11 return (*(node*)a).d-(*(node*)b).d;
12 }
13 int find(int x)
14 {
15 if(x!=set[x]) set[x]=find(set[x]);
16 return set[x];
17 }
18 int merge(int a,int b)
19 {
20 int x=find(a);
21 int y=find(b);
22 if(x!=y){
23 set[x]=y;
24 return 1;
25 }
26 return 0;
27 }
28 int kruskal()
29 {
30 int ans=0;
31 for(int i=0;i<cnt;i++)
32 if(merge(p[i].u,p[i].v))
33 ans+=p[i].d;
34 return ans;
35 }
36 int main(void)
37 {
38 int m,d;
39 char c[2],s[2];
40 while(scanf("%d",&n)!=EOF && n)
41 {
42 map<char,int>M;
43 M.clear();
44 for(int i=0;i<=n;i++) set[i]=i;
45 int id=1;
46 cnt=0;
47 for(int i=1;i<n;i++){
48 scanf("%s %d",c,&m);
49 if(M[c[0]]==0) M[c[0]]=id++;
50 while(m--){
51 scanf("%s %d",s,&d);
52 if(M[s[0]]==0) M[s[0]]=id++;
53 p[cnt].u=M[c[0]];
54 p[cnt].v=M[s[0]];
55 p[cnt++].d=d;
56 }
57 }
58 qsort(p,cnt,sizeof(p[0]),cmp);
59 printf("%d\n",kruskal());
60 }
61 return 0;
62 }