北大ACM2485 - Highways(最小生成树)

这是一道非常简单的关于最小生成树的问题。我们通过该题,完成对朴素prim、prim+heap、kruskal算法 的理解。

朴素Prim算法

设共有n个顶点,顶点之间没有必然的顺序,开始将其中一个顶点的权值设为0,其他设为最大(无穷大),花销是o(n)。每次从其中选择一个未处理过的权值最小的顶点,花销是O(n);然后更新和该顶点相连的顶点的权值,花销是o(n);共需要进行n次,花销是o(n),以上三个步骤是嵌套关系,所以花销是o(n3)。所以总花销是o(n)+o(n3)=o(n3)。

prim处理的对象是顶点,所以,数据结构是struct vertex。

运行结果是 760K 172MS。
代码如下:
/*
 *
 * Introduction : ACM of pku
 * ID : 2485
 * alg : MST.Prim
 * Author : Gykimo
 * Date : 20130115
 * 
 */

#include <stdlib.h>
#include <stdio.h>
#include <queue>

using namespace std;

struct vertex
{
	int minDist;	//the min distance to the adjcent nodes
	bool inMST;	//is walked
};

int case_num = 0;
int village_num = 0;

int edge[500][500];
vertex* vertices = NULL;

int max_lenth = 0;

void init(){
	vertices = (vertex*)malloc(village_num * sizeof(vertex));
	
	for(int i=1; i<village_num; i++){
		vertices[i].minDist = 65536;
		vertices[i].inMST = false;
	}

	vertices[0].minDist = 0;
	vertices[0].inMST = false;
}

/*获得未处理的权值最小的顶点*/
int getVertexOfMinDist(){
	int min = 65536;
	int minIndex = -1;
	for(int i=0; i<village_num; i++){
		if(!vertices[i].inMST && vertices[i].minDist < min){
			min = vertices[i].minDist;
			minIndex = i;
		}
	}
	return minIndex;
}

/*更新和index顶点相邻的顶点的权值*/
void updateMinDist(int index){
	for(int i=0; i<village_num; i++){
		if(!vertices[i].inMST && (vertices[i].minDist > edge[index][i])){
			vertices[i].minDist = edge[index][i];
		}
	}
}

void prim(){
	vertex* v = NULL;
	int minIndex = 0;

	while((minIndex = getVertexOfMinDist()) >= 0){
		v = vertices + minIndex;
		v->inMST = true;
		
		if(max_lenth < vertices[minIndex].minDist){
			max_lenth = vertices[minIndex].minDist;
		}

		updateMinDist(minIndex);
	}

	printf("%d\n", max_lenth);
}

void readLine(){
	scanf("%d", &case_num);
	for(int i=0; i<case_num; i++){
		scanf("%d", &village_num);
		for(int j=0; j<village_num; j++){
			for(int k=0; k<village_num; k++){
				scanf("%d", &edge[j][k]);
			}
		}

		init();
		prim();
		max_lenth = 0;
		free(vertices);
	}
}

int main(){
	readLine();
	return 0;
}

Prim + heap算法


Kruskal算法

由于Kruskal算法是对边进行处理的一个算法,而边除了有边的权值之外,还需要边的两个端点,所以共定义了两个数据结构
class edge和class vertex。

运行结果:8504K 516MS

注意:
如果将函数deinit()中注释掉的代码放开的话,运行结果是超时。

/*
 *
 * Introduction : ACM of pku
 * ID : 2485
 * alg : MST.Kruskal
 * Author : Gykimo
 * Date : 20130115
 * 
 */

#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <vector>

using namespace std;

class vertex
{
	public:
		vertex(){
			make_set();
		}
		
		vertex * find_set(){
			if(mParent != this){
				mParent = mParent->find_set();
			}
			
			return mParent;
		}

		void union_set(vertex * v){
			link(v->find_set());
		}
		
	private:
		vertex * mParent;
		int mRank;

		void make_set(){
			mParent = this;
			mRank = 0;
		}
		
		void link(vertex * v){
			if(mRank < v->mRank){
				mParent = v->find_set();
			}else{
				v->mParent = this;
				if(mRank = v->mRank){
					mRank++;
				}
			}
		}
};

class edge
{
	public:
		edge(vertex& u, vertex& v, int w){
			v1 = &u;
			v2 = &v;
			weight = w;
		}

		bool kruskal_check(){
			if(v1->find_set() != v2->find_set()){
				add_to_mst();
				return true;
			}
			return false;
		}

		int weight;

	private:
		vertex * v1;
		vertex * v2;
		
		void add_to_mst(){
			v1->union_set(v2);
		}
};

int case_num = 0;
int village_num = 0;
vector<edge*> edges;
vertex * vertices;
int max_lenth = 0;

void init(){
	vertices = new vertex[village_num];
}

void deinit(){
	delete [] vertices;
	/*
	for(vector<edge*>::iterator iter=edges.begin(); iter !=edges.end(); ++iter){
		delete (*iter);
	}*/
	edges.clear();
}

bool cmp(edge* e1, edge* e2){
	if(e1->weight < e2->weight)
		return true;
	return false;
}

void kruskal(){
	int loop_num = village_num;
	sort(edges.begin(), edges.end(), cmp);

	for(vector<edge*>::iterator iter=edges.begin(); iter !=edges.end(); ++iter){
		if((*iter)->kruskal_check()){
			loop_num--;
			max_lenth = (*iter)->weight;
		}
		if(loop_num == 0)
			break;
	}

	printf("%d\n", max_lenth);
}

void readLine(){
	int tmp;
	edge* e;
	
	scanf("%d", &case_num);
	for(int i=0; i<case_num; i++){
		scanf("%d", &village_num);
		init();
		
		for(int j=0; j<village_num; j++){
			for(int k=0; k<village_num; k++){
				scanf("%d", &tmp);
				if(k>j){
					e = new edge(vertices[j], vertices[k], tmp);
					edges.push_back(e);
				}
			}
		}
		kruskal();
		deinit();
	}
}

int main(){
	readLine();
	return 0;
}

结论



你可能感兴趣的:(北大ACM2485 - Highways(最小生成树))