杭电1233 and 杭电1162————最小生成树(MST之prime算法)

关于prime算法基础的讲解

http://blog.csdn.net/u013548531/article/details/38467183

还是畅通工程

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25849    Accepted Submission(s): 11509


Problem Description
某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离。省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路总长度为最小。请计算最小的公路总长度。
 

Input
测试输入包含若干测试用例。每个测试用例的第1行给出村庄数目N ( < 100 );随后的N(N-1)/2行对应村庄间的距离,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间的距离。为简单起见,村庄从1到N编号。
当N为0时,输入结束,该用例不被处理。
 

Output
对每个测试用例,在1行里输出最小的公路总长度。
 

Sample Input
3 1 2 1 1 3 2 2 3 4 4 1 2 1 1 3 4 1 4 1 2 3 3 2 4 2 3 4 5 0
 

Sample Output
3 5
<span style="font-family:Microsoft YaHei;font-size:14px;">#include <stdio.h>
#include <string.h>
#define  maxn 105
#define INF  100000

int G[maxn][maxn];
int intree[maxn];
int minlen[maxn];
int sum;/*公路最小长度*/ 
void initialize()
{
	memset(intree,0,sizeof(intree));
	for(int i = 1 ; i <= maxn -1 ; i++)
		for(int j = 1 ; j <= maxn - 1 ; j++)
			G[i][j] = INF;
	sum = 0;
}
void prime(int n)
{
	int templen;
	int node;
	
	for(int i = 1 ; i <= n ; i++)
		minlen[i] = G[1][i];
	intree[1] = 1;
	/*1号节点进入最小生成树*/
	for(int i = 2 ; i <= n ; i++)/*对剩下的n-1个点扫描*/ 
	{
		templen = INF;
		for(int j = 1 ; j <= n ; j ++)
		{
			if(!intree[j] && minlen[j] < templen)
			{
				templen = minlen[j];
				node = j;
			}
		}
		intree[node] = 1;/*找到了又一个加入最小生成树的点*/ 
		sum += templen;
		/*更新最小生成树的根节点到各点的距离*/ 
		for(int j = 1 ; j <= n ; j++)
			if(minlen[j] > G[node][j] && !intree[j])
				minlen[j] = G[node][j];
	} 
}
int main()
{
	int vertex1,vertex2,len;
	int N;
	while(scanf("%d",&N) != EOF && N)
	{
		initialize();
		for(int i = 1 ; i <= N*(N-1) / 2 ; i++)
		{
			scanf("%d%d%d",&vertex1,&vertex2,&len);
			G[vertex1][vertex2] = G[vertex2][vertex1] = len;
		}
		prime(N);
		printf("%d\n",sum);
	}
	return 0;
}</span>

Eddy's picture

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6799    Accepted Submission(s): 3429


Problem Description
Eddy begins to like painting pictures recently ,he is sure of himself to become a painter.Every day Eddy draws pictures in his small room, and he usually puts out his newest pictures to let his friends appreciate. but the result it can be imagined, the friends are not interested in his picture.Eddy feels very puzzled,in order to change all friends 's view to his technical of painting pictures ,so Eddy creates a problem for the his friends of you.
Problem descriptions as follows: Given you some coordinates pionts on a drawing paper, every point links with the ink with the straight line, causes all points finally to link in the same place. How many distants does your duty discover the shortest length which the ink draws?
 

Input
The first line contains 0 < n <= 100, the number of point. For each point, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the point. 

Input contains multiple test cases. Process to the end of file.
 

Output
Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the points. 
 

Sample Input
3 1.0 1.0 2.0 2.0 2.0 4.0
 

Sample Output
3.41
<span style="font-family:Microsoft YaHei;font-size:14px;">#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define maxn 105
#define INF 900000.000000

struct Point{
	float x,y;
};
float G[maxn][maxn];
float minlen[maxn];
float sum;
struct Point p[maxn];/*ponints*/
int intree[maxn]; 

void initialize()
{
	memset(intree,0,sizeof(intree));
	memset(minlen,0,sizeof(minlen));
	for(int i = 0 ; i <= maxn -1 ; i++)
		for(int j = 0 ; j <= maxn - 1 ;j++)
			G[i][j] = INF;
	sum = 0.0;
}
float distance(struct Point a,struct Point b)
{
	return sqrt((b.x - a.x) * (b.x - a.x) + (b.y - a.y) * (b.y - a.y));
}
void prime(int n)
{
	int node;
	float templen;
		
	for(int i = 1 ; i <= n ; i++)
		minlen[i] = G[1][i];
	intree[1] = 1;
	/*让第一个点进入最小生成树*/
	for(int i = 2 ; i <= n ; i++)/*从剩下的n-1个点中寻找*/
	{
		templen = INF;
		for(int j = 1 ; j <= n ; j++)
		{
			if(templen - minlen[j] > 0.000001 && !intree[j])
			{
				templen = minlen[j];
				node = j;
			}
		}
		intree[node] = 1 ;/*所有点搜索完毕后选取最符合条件的进入最小生成树*/
		sum += templen;
		/*更新B集合和A集合*/
		for(int j = 1 ; j <= n ; j++)
			if(minlen[j] - G[node][j] > 0.000001&& !intree[j])
				minlen[j] = G[node][j];
	} 
}
int main()
{
	int n;
	
	while(scanf("%d",&n) != EOF)
	{
		initialize();
		for(int i = 1 ; i <= n ; i++)
			scanf("%f%f",&p[i].x,&p[i].y);
		for(int i = 1 ; i <= n ; i++)
			for(int j = i ; j <= n ;j++)
				G[i][j] = G[j][i] = distance(p[i],p[j]);
			
		prime(n);
		printf("%.2f\n",sum);
	}
}</span>



posted @ 2014-08-10 15:29  SixDayCoder  阅读(240)  评论(0编辑  收藏  举报