UVa 10397: Connect the Campus

来源:互联网 发布:优化设计方案的方法有 编辑:程序博客网 时间:2024/05/19 10:12

这道题是最小生成树(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 750double 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){//initfor(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)