hdu 1863

畅通工程

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 14103    Accepted Submission(s): 5824

Problem Description

省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。经过调查评估,得到的统计表中列出了有可能建设公路的若干条道路的成本。现请你编写程序,计算出全省畅通需要的最低成本。

Input

测试输入包含若干测试用例。每个测试用例的第1行给出评估的道路条数 N、村庄数目M ( < 100 );随后的 N
行对应村庄间道路的成本,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间道路的成本(也是正整数)。为简单起见,村庄从1到M编号。当N为0时,全部输入结束,相应的结果不要输出。

Output

对每个测试用例,在1行里输出全省畅通需要的最低成本。若统计数据不足以保证畅通,则输出“?”。

Sample Input

3 3 1 2 1 1 3 2 2 3 4 1 3 2 3 2 0 100

Sample Output

3 ?

Source

浙大计算机研究生复试上机考试-2007年

题解:显然就是求解最小生成树,用prim算法实现。

#include<stdlib.h> 
#include<stdio.h> 
#include<string.h> 
#define INF 10000001 
#define maxn 101 
int map[maxn][maxn]; 
int cost[maxn]; 
int chosed[maxn]; // 
int count; //统计点的个数 
long total_cost; //总费用 
void init_map(int n){ //初始化二阶矩阵 
	int i,j; 
	for(i=1;i<=n;i++) 
		for(j=1;j<=n;j++) 
			map[i][j]=(i==j?0:INF); 
} 
void init(int n){ 
	int i; 
	memset( chosed , 0 , sizeof(chosed)); 
	chosed[1]=1; 21: count=1; 
	for( i = 1 ; i <= n ; i++ ) 
		cost[i] = map[1][i]; 24: } 

void prim(int n){ 
	int min,min_idx; 
	int i; 
	total_cost=0; 
	while( count <=n ){ 
		min = INF; 
		for( i = 2 ; i <= n ; i++ ){ 
			if( !chosed[i] && cost[i] < min){ //选择尚未choosed的node中的最小的权重的边 
				min_idx=i; 
				min=cost[i]; 
			} 
		} 
		if( min == INF ) 
			return; 
		chosed[min_idx]=1; 
		total_cost+=min; 
		count++; 
		for( i = 1 ; i <= n ; i++) //按照这个node更新cost[] 
			if(!chosed[i]&& map[min_idx][i]<cost[i]) 
				cost[i] = map[min_idx][i]; 
	} 
} 

int main(){ 
	int road,node,i; 
	while(scanf("%d %d",&road,&node)!=EOF&& road ){ 
		init_map(node); 
		count=0; 
		while(road—){ 
			int a,b,c; 
			scanf("%d %d %d",&a,&b,&c); 
			map[a][b]=c; 
			map[b][a]=c; 
		} 
		init(node); 
		prim(node); 
		if(count==node) 
			printf("%ld
",total_cost); 
		else 
			printf("?
"); 
	} 
}

另一版本:kruskal算法(边从小到大排序)

#include<stdlib.h> 
#include<stdio.h> 
#include<string.h> 
#define maxn 10000 
#define N 101
struct node{ 
	int u; 
	int v; 
	int w; 
}edges[maxn]; 
int total_cost; 
int id[N]; 
int choosed[N]; 
int comp(const void*p,const void *q){ 
	struct node a=*(struct node *)p; 
	struct node b=*(struct node *)q; 
	return a.w-b.w; 
} 
int find_root(int idx){ 

	if(id[idx]==-1) 
		return idx; 
	return id[idx]=find_root(id[idx]); 
} 

void init(int n,int m){ 
	int i; 
	memset(choosed,0,sizeof(choosed)); 
	qsort(edges,n,sizeof(struct node),comp); 

	for(i=0;i<=m;i++) 
		id[i]=-1; 
	total_cost=0; 
} 
void kruskal(int n){ 
	int i,x,y; 
	for(i=0;i<n;i++){ 
		x=find_root(edges[i].u); 
		y=find_root(edges[i].v); 
		if(x!=y){ 
			id[y]=x; 
			total_cost+=edges[i].w; 
			choosed[edges[i].u]=1; 
			choosed[edges[i].v]=1; 
		} 
	} 
} 
int main(){ 
	int n,m,i,count; 
	while(scanf("%d %d",&n,&m)!=EOF&&n){ 
		for(i=0;i<n;i++) 
			scanf("%d %d %d",&edges[i].u,&edges[i].v,&edges[i].w);
		init(n,m); 
		kruskal(n); 
		count=0; 
		for(i=1;i<=m;i++){ 

			if(id[i]==-1) 
				count++; 
		} 
		if(count==1)//注意,不能通过统计choosed[]的数量,即使全部被选择,也未必是一棵连通的树。 
			printf("%d
",total_cost); 
		else 
			printf("?
"); 
	} 
}
原文地址:https://www.cnblogs.com/ZJUT-jiangnan/p/3588715.html