Description
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.
Input
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.
题意很简单,求一个最小生成树,求出树的最短距离和,最小生成树有两种算法,一种是prim算法,一种是kruskal算法,先来看prim,基本过程为:1.将图中n个点分为两个集合。2.U集合为已经在生成树上的顶点集。3.V集合为尚未落在生成树上的顶点集。4.不断选取U中顶点与V中顶点权值最小的边。结束。
再来看这次用到的Kruskal算法,将每条边的权值排序,每次选择最小的边加入生成树中,如果它的添加不使树产生回路那么这条边就加入生成树中,我们将边排序,然后根据最小的边来不断的构造树,其中判断回路比较难,这里使用了一种数据结构需要学习一下,并查集。能够快速的判断树中是否有环,并且合并不相交的集合,代码虽短,很实用效率也很高,这里需要注意一下并查集的应用。
#include<algorithm>
#include<stdio.h>
#include<queue>
#include<string.h>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
struct node
{
int l;
int r;
int len;
bool operator <(const node &x)const
{
if(len<x.len)
return true;
else
return false;
}
}a[1000];
int F[30]; //i点的父节点
int findfa(int x) //查找最先的父节点,既是根节点
{
if(x!=F[x])
return F[x]=findfa(F[x]);
return F[x];
}
int main()
{
int n,nn,len,nown;
char s[5];
while(scanf("%d",&n)!=EOF)
{
if(n==0)
break;
for(int i=1;i<=40;i++)
F[i]=i;
node now,next;
int Len=0;
for(int i=1;i<n;i++)
{
scanf("%s%d",s,&nn);
now.l=s[0]-'A'+1;
while(nn--)
{
scanf("%s%d",s,&len);
now.r=s[0]-'A'+1;
now.len=len;
a[Len++]=now;
}
}
sort(a,a+Len);
int ans=0;
for(int i=0;i<Len;i++)
{
now=a[i];
int dx=findfa(now.l);
int dy=findfa(now.r);
if(dx!=dy) //如果dx=dy,说明有相同的一条边的两个端点有相同的父节点,会出现环,则不加这条边
{
F[dx]=dy; //合并两个子集合
ans+=now.len;
}
}
printf("%d\n",ans);
}
return 0;
}