Jungle Roads (最小生成树)

题目链接:http://poj.org/problem?id=1251

 

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. 

Output

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. 

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
3
A 2 B 10 C 40
B 1 C 20
0

Sample Output

216
30

题目大意:

给出所有相连的道路,如样例中第二行:A有两个:b和i,分别距离是12和25,下面的一样;

让后输出最小的距离(给出了两个样例)

求最小生成树的算法有两种,1.prim和2.找老大(我已经忘记学名了。。反正我称之找老大)

两者的模板不同,复杂度也不同,

prim(O(n^2))时间复杂度;

找老大(O(log n))的复杂度;

下面我们一一讲述:

首先是我认为最简单的prim(毕竟时间复杂度那么高2333):

prim算法又分为两个方法:

1.链接表法(运用结构体储存数据)

2.链接矩阵(利用数组储存数据)

首先链接表法,其实他们两个的算法步骤是一样的了,只是比较的时候有点区别空间复杂度也不一样:

链接表的空间复杂度比链接矩阵的空间复杂度要低(其实也可能叫做领接矩阵。。。不知道我记错没。。)

链接矩阵:

这个比链接表要好理解一点(个人认为因为当时是先用邻接矩阵写的,内存超限时改用的邻接表):

ac代码:

 

#include
#include
#include

#include
#include
#include
#include
#include
using namespace std;

#define ll long long
#define da    0x3f3f3f3f
#define xiao -0x3f3f3f3f
#define clean(a,b) memset(a,b,sizeof(a))


int shuzu[30][30];										//邻接矩阵储存距离,对应的下标是村庄与村庄,如果距离是da就代表不想连 
bool biaoji[30];
int shuaxin[30];
int sum,n;

void prime(int x)										//前面输入的是多少,就是从第几个村庄找,这里是从第0个开始找 
{
	int i,j;
	sum=0;												//sum一开始是0 
	clean(biaoji,0);									//初始化都没链接(标记) 
	clean(shuaxin,da);									//初始化 都没路(因此是大的) 
	shuaxin[x]=0;										//第一个是自己,距离是0 
	for(i=0;ishuzu[can][j])//没加入生成树&&距离新的小于之前的距离 
				shuaxin[j]=shuzu[can][j];	//刷新距离,使各个点到生成树对的距离保持最小 
		}
	}
}

int main()
{
	while(~scanf("%d",&n))
	{
		if(n==0)
			break;
		int i,j;
		clean(shuzu,da);						//初始化没有路 
		for(i=0;i>ch>>x;							//一个村庄与几个村庄相连 
			for(j=0;j>ar>>st;					//相连的村庄与距离 
				int fang=ar-'A';				//用数字储存 
				shuzu[i][fang]=st;				//因为这是双向的连通图,因此都能相互走 
				shuzu[fang][i]=st;				//双向连通图,要链接两个村庄 
			}
		}
		prime(0);								//先找到一个村庄,也可以从第一个村庄开始找,也可以换成1什么的 
		printf("%d\n",sum);						//最后输出生成树的长度 
		
	}
}

 

 

然后是链接表:

 

 

已ac:

 

#include
#include
#include

#include
#include
#include
#include
#include
using namespace std;

#define ll long long
#define da    10000000
#define xiao -10000000
#define clean(a,b) memset(a,b,sizeof(a))

struct ac{						//结构体储存数据 
	int num;					//与之相连的村庄 
	int space;					//与之相连的村庄的长度 
};

vector home[30];			//vector可以看一下,这里就一个可变的结构体容器 
bool biaoji[30];				//标记 有没有加入生成树 
int shuaxin[30];				//刷新最短距离 
int sum,n;

void prime(int x)				//与邻接矩阵类似 
{
	int i,j;
	sum=0;
	clean(biaoji,0);			//都要初始化 
	for(i=0;i<30;++i)
		shuaxin[i]=da;
	shuaxin[x]=0;				//第一次是0 
	for(i=0;i>ch>>x;
			for(j=0;j>ar>>st;
				int fang=ar-'A';
				ac one,two;						//因为是结构体因此要用结构体储存 
				one.num=i;						//双向连通图 
				one.space=st;
				two.num=fang;					//第二个 
				two.space=st;
				home[i].push_back(two);			//可以互相走 
				home[fang].push_back(one);		//加进去 
			}
		}
		prime(0);								//从初始村庄开始(加入初始村庄进入生成树) 
		printf("%d\n",sum);						//生成树长度 
		for(i=0;i<30;++i)						//清空 
			home[i].clear();
	}
	
}

 

 

 

然后是找老大的。。我写完后才知道的。。。这里有个链接,讲的比较详细,我就写一下代码,便于理解(噢!!对了这叫并查集!!才想起来。。。找老大的学名叫:并查集。。。。):

大佬的博客链接:http://blog.csdn.net/niushuai666/article/details/6662911

 

  

实在不想写了来个链接。。:http://blog.csdn.net/yzllz001/article/details/51789281

#include  
#include  
using namespace std;  
int a[10002];  
struct st  
{  
    int x,y,len;  
}s[10001];  
int find(int x)  			//找老大 
{  
    while(x!=a[x])  
    {  
        x=a[x];  
    }  
    return x;  				//返回老大 
}  
int un(int x,int y)  		//混合老大 
{  
    x=find(x);  
    y=find(y);  
    if(x!=y)  
    {  
        a[x]=y;  
        return 1;  
    }  
    return 0;  
}  
int cmp(const void *a,const void *b)  	//排序 
{  
    st c=*(st *)a;  
    st d=*(st *)b;  
    return c.len-d.len;  
}  
int main()  
{  
    int i,j,k,n,m,l,t,kk;  
    char cc[2];  
    char dd[2];  
    while(~scanf("%d",&n))  
    {  
        if(n==0)  
        break;  
        for(i=1;i<=300;i++)  
        {  
            a[i]=i;  
        }  
        int k=0;  
        for(i=0;i

 

 

 

 

 

 

 

你可能感兴趣的:(图论)