uvaoj 10397 - Connect the Campus【最小生成树】

uvaoj 10397 - Connect the Campus

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.

Input

The input file describes several test cases. 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

Sample Output

4.41

题意:给你一个数字n表示建筑的数量,接下来n行是每座建筑的坐标,然后一个数字m接下来m行每行两个数a,b,表示建筑a和建筑b之间已经联通,问联通n个建筑的最短距离

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#define INF 0x3f3f3f
#define DD double
#define MAX 1010
using namespace std;
int n,m;
int sum;
DD b[MAX],a[MAX];
DD map[MAX][MAX];
int vis[MAX];
DD low[MAX];
DD fun(int i,int j)
{
	return sqrt((a[i]-a[j])*(a[i]-a[j])+(b[i]-b[j])*(b[i]-b[j]));//求两个城市之间的距离
}
void init()
{
	int i,j;
	for(i=1;i<=n;i++)
	    for(j=1;j<=n;j++)
	        map[i][j]=i==j?0:INF;
}
int main()
{
	int t,i,j,k;
    while(scanf("%d",&n)!=EOF)
    {
    	init();
    	for(i=1;i<=n;i++)
    		scanf("%lf%lf",&a[i],&b[i]);
    	for(i=1;i<=n;i++)
    	{
    		for(j=i;j<=n;j++)
    		{
    			map[i][j]=map[j][i]=fun(i,j);
			}
		}

		scanf("%d",&m);
		while(m--)
		{
			int x,y;
			scanf("%d%d",&x,&y);
			map[x][y]=map[y][x]=0;//已经联通的城市距离为0
		}
		int next;
		DD min,mindis=0;
		memset(vis,0,sizeof(vis));
		for(i=1;i<=n;i++)
		    low[i]=map[1][i];

		vis[1]=1;
		for(i=1;i<n;i++)
		{
			min=INF;
			next=1;
			for(j=1;j<=n;j++)
			{
				if(min>low[j]&&!vis[j])
				{
					next=j;
					min=low[j];
				}
			}

			mindis+=min;
			vis[next]=1;
			for(j=1;j<=n;j++)
			{
				if(!vis[j]&&low[j]>map[next][j])
				low[j]=map[next][j];
			}
		}
		printf("%.2lf\n",mindis);
	}
	return 0;
}

  

时间: 2024-12-25 16:17:03

uvaoj 10397 - Connect the Campus【最小生成树】的相关文章

uva 10397 - Connect the Campus

Problem E Connect the Campus Input: standard input Output: standard output Time Limit: 2 seconds Many new buildings are under construction on the campus of the University of Waterloo. The university has hired bricklayers, electricians, plumbers, and

uva 10397 Connect the Campus kruskal || prim

uva上的题目总是要难一些的...总是会拐弯抹角,这道题目给出有的建筑之间已经建好了光缆,让求最小生成树,我还是比较喜欢用kruskal算法,但是这道题目肯定用prim比较快,prim复杂度是n^2,kruskal复杂度eloge. 用kruskal时需要预先用并查集处理一下已经建好的光缆,让他们属于同一个祖先: 用prim算法的时候需要把他们的边置为0,这样算sum的时候就不会加上了. 代码:(kruskal) #include<iostream> #include<cstdio>

uva 10397 Connect the Campus kruskal 算法变形

#include <cstdio> #include <iostream> #include <algorithm> #include <queue> #include <stack> #include <cstdlib> #include <cmath> #include <set> #include <map> #include <vector> #include <cstri

UVA10397 - Connect the Campus(最小生成树+并查集)

UVA10397 - Connect the Campus(最小生成树) 题目链接 题目大意:给你n个点,然后再给你m个已经连接的边,问如何使得所有的点都相连并且新建的边长度之和最小. 解题思路:最小生成树,但是有m条边是已经建好的,就将这些边的权值变成0,然后用kruskal的方法来求长度. 代码: #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> usin

UVa10397_Connect the Campus(最小生成树)(小白书图论专题)

解题报告 题目传送门 题意: 使得学校网络互通的最小花费,一些楼的线路已经有了. 思路: 存在的线路当然全都利用那样花费肯定最小,把存在的线路当成花费0,求最小生成树 #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #define inf 0x3f3f3f3f using namespace std; int n,m,_hash[1110][1110],vis

Connect the Campus (Uva 10397 Prim || Kruskal + 并查集)

题意:给出n个点的坐标,要把n个点连通,使得总距离最小,可是有m对点已经连接,输入m,和m组a和b,表示a和b两点已经连接. 思路:两种做法.(1)用prim算法时,输入a,b.令mp[a][b]=0.然后进行一遍prim(2)Kruskal算法+并查集 代码: //prim写法 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <

ZOJ 3204 Connect them(最小生成树之Krusal 输出字典序最小的)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3367 You have n computers numbered from 1 to n and you want to connect them to make a small local area network (LAN). All connections are two-way (that is connecting computers i and j is

ZOJ 3204 Connect them(最小生成树:kruscal算法)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3204 Connect them Time Limit: 1 Second     Memory Limit:32768 KB You have n computers numbered from 1 to n and you want to connect them to make a small local area network (LAN).All connecti

HDU_3371 Connect the cities(最小生成树)

Connect the Cities Description In 2100, since the sea level rise, most of the cities disappear. Though some survived cities are still connected with others, but most of them become disconnected. The government wants to build some roads to connect all