UVa 10397: Connect the Campus

這道題是最小生成樹(MST)問題的變種問題。

在我的最小生成樹的Prim算法的模板(需要模板請單擊http://blog.csdn.net/rising_fallmoon/article/details/9819187)基礎上增加一個vis數組用於區分節點是否已加入集合T中。這裏不能使用節點的min_dis爲0作爲該節點是否加入T中,因爲題目中給出了已經相連的邊,而我們將其權值設爲了0,需要另加數組判斷。

另一個注意點是這裏Prim不一定需要執行循環N-1次,同樣因爲有邊權初始化爲0。及時終止循環可以稍微提高效率。

我的解題代碼如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <string>
#include <algorithm>

using namespace std;

#define Distance double
#define INF 1000000
#define MAXN 750
double x[MAXN],y[MAXN];
Distance dis[MAXN][MAXN];
Distance min_dis[MAXN];
//int nearest_v[MAXN];
int vis[MAXN];

Distance Prim(int v0, int N)
{
	//init
	for(int i=0; i<N; i++)
	{
		min_dis[i] = dis[i][v0];
//		nearest_v[i] = v0;
	}
	min_dis[v0] = 0;
	memset(vis,0,sizeof(vis));
	vis[v0] = 1;
	Distance total_dis = 0;
	for(int k=1; k<N; k++)
	{
		Distance md = INF;
		int nv = v0;
		for(int i=0; i<N; i++) if(!vis[i])
		{
			if(md > min_dis[i]) 
			{
				md = min_dis[i];
				nv = i;
			}
		}

		total_dis += md;
		min_dis[nv] = 0;
		vis[nv] = 1;

		for(int i=0; i<N; i++) if(!vis[i])
		{
			if(min_dis[i] > dis[i][nv])
			{
				min_dis[i] = dis[i][nv];
//				nearest_v[i] = nv;
			}
		}
		int ok = 0;
		for(int i=0; i<N; i++) if(min_dis[i]!=0) { ok=1; break; }
		if(!ok) break;
	}
	return total_dis;
}
int main()
{
	int N,M;
	while(cin >> N)
	{
		for(int i=0; i<N; i++)
		{
			scanf("%lf %lf",&x[i],&y[i]);
			for(int j=0; j<=i; j++)  
				dis[i][j]=dis[j][i]=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));  
		}
		cin >> M;
		int na,nb;
		for(int i=0; i<M; i++)
		{
			scanf("%d %d",&na,&nb);
			dis[na-1][nb-1]=dis[nb-1][na-1]=0;
		}
		printf("%.2lf\n", Prim(0,N));
	}
	return 0;
}
附上題目如下:

Many new buildings are under construction on the campus of the University of Waterloo. The university has hired bricklayers, electricians, plumbers, and a computer programmer. A computer programmer? Yes, you have been hired to ensure that each building is connected to every other building (directly or indirectly) through the campus network of communication cables.

We will treat each building as a point specified by an x-coordinate and a y-coordinate. Each communication cable connects exactly two buildings, following a straight line between the buildings. Information travels along a cable in both directions. Cables can freely cross each other, but they are only connected together at their endpoints (at buildings).

You have been given a campus map which shows the locations of all buildings and existing communication cables. You must not alter the existing cables. Determine where to install new communication cables so that all buildings are connected. Of course, the university wants you to minimize the amount of new cable that you use.

Fig: University of Waterloo Campus

 

Input

The input file describes several test case.  The description of each test case is given below:

The first line of each test case contains the number of buildings N (1<=N<=750). The buildings are labeled from 1 to N. The next N lines give the x and y coordinates of the buildings. These coordinates are integers with absolute values at most 10000. No two buildings occupy the same point. After that there is a line containing the number of existing cables M (0 <= M <= 1000) followed by M lines describing the existing cables. Each cable is represented by two integers: the building numbers which are directly connected by the cable. There is at most one cable directly connecting each pair of buildings.

Output

For each set of input, output in a single line the total length of the new cables that you plan to use, rounded to two decimal places.

Sample Input

4
103 104
104 100
104 103
100 100
1
4 2

4
103 104

104 100

104 103

100 100

1

4 2

 

Sample Output
4.41
4.41


(Problem-setters: G. Kemkes & G. V. Cormack, CS Dept, University of Waterloo)


發佈了53 篇原創文章 · 獲贊 32 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章