您的位置:首页 > 其它

最小生成树 Jungle Roads

2013-09-11 14:03 423 查看

Jungle Roads

Time Limit : 2000/1000ms(Java/Other)   MemoryLimit : 65536/32768K (Java/Other)Total Submission(s) :11   AcceptedSubmission(s) : 9

Font: Times NewRoman | Verdana | Georgia

Font Size: ← →

Problem Description

The Head Elder of the tropical island of Lagrishan has a problem. Aburst of foreign aid money was spent on extra roads betweenvillages some years ago. But the jungle overtakes roadsrelentlessly, so the large road network is too expensive tomaintain. The Council of Elders must choose to stop maintainingsome roads. The map above on the left shows all the roads in usenow and the cost in aacms per month to maintain them. Of coursethere needs to be some way to get between all the villages onmaintained roads, even if the route is not as short as before. TheChief Elder would like to tell the Council of Elders what would bethe smallest amount they could spend in aacms per month to maintainroads that would connect all the villages. The villages are labeledA through I in the maps above. The map on the right shows the roadsthat could be maintained most cheaply, for 216 aacms per month.Your task is to write a program that will solve suchproblems. The input consists of one to 100 data sets, followed by a finalline containing only 0. Each data set starts with a line containingonly a number n, which is the number of villages, 1< n < 27, and the villages arelabeled with the first n letters of the alphabet, capitalized. Eachdata set is completed with n-1 lines that start with village labelsin alphabetical order. There is no line for the last village. Eachline for a village starts with the village label followed by anumber, k, of roads from this village to villages with labels laterin the alphabet. If k is greater than 0, the line continues withdata for each of the k roads. The data for each road is the villagelabel for the other end of the road followed by the monthlymaintenance cost in aacms for the road. Maintenance costs will bepositive integers less than 100. All data fields in the row areseparated by single blanks. The road network will always allowtravel between all the villages. The network will never have morethan 75 roads. No village will have more than 15 roads going toother villages (before or after in the alphabet). In the sampleinput below, the first data set goes with the mapabove. The output is one integer per line for each data set: the minimumcost in aacms per month to maintain a road system that connect allthe villages. Caution: A brute force solution that examines everypossible set of roads will not finish within the one minute timelimit. 

Sample Input

9
A 2 B 12 I 25 
B 3 C 10 H 40 I 8 
C 2 D 18 G 55 
D 1 E 44 
E 2 F 60 G 38 
F 0 
G 1 H 35 
H 1 I 35 
A 2 B 10 C 40 
B 1 C 20 
0

Sample Output

216 
30
int x;
father[i]=j;
int n,i,p,q,j,m,g,k,ans;
char c1,c2;
while(scanf("%d",&n)!=EOF&&n)
{
k=0;
Makeset(N);
for(i=0;i<n-1;i++)
{
getchar();
scanf("%c%d",&c1,&m);
p=c1-'A'+1;
for(j=0;j<m;j++)
{
getchar();
scanf("%c%d",&c2,&g);
q=c2-'A'+1;
a[k].x=p;
a[k].y=q;
a[k].v=g;
k++;
}
}
qsort(a,k,sizeof(a[0]),cmp);
ans=0;
for(i=0;i<k;i++)
{
if(Merge(a[i].x,a[i].y))
{
ans+=a[i].v;
}
}
printf("%d\n",ans);
}
return 0;
int k[501];
int flag,i,j,min,sum=0;
for(i=1;i<=n;i++)
k[i]=map[1][i];
for(i=2;i<=n;i++)
{
min=MAX;
flag=0;
for(j=2;j<=n;j++)
{
if(k[j]<min&&k[j]!=0)
{
min=k[j];
flag=j;
}
}
sum+=min;
k[flag]=0;
for(j=2;j<=n;j++)
{
if(map[flag][j]<k[j])
{
k[j]=map[flag][j];
}
}
}
return sum;
int n,m,i,j,num,k,c;
char c1,c2;
k=n-1;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
map[i][j]=MAX;
}
while(k--)
{
getchar();//一定要加上,不然输入有误
scanf("%c%d",&c1,&m);
i=c1-'A'+1;
while(m--)
{
getchar();//一定要加上,不然输入有误
scanf("%c%d",&c2,&c);
j=c2-'A'+1;
map[i][j]=map[j][i]=c;
}
}
num=prim(map,n);
printf("%d\n",num);
return 0;

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: