10397 - Connect the Campus
来源:互联网 发布:4g网络架构 编辑:程序博客网 时间:2024/06/06 23:58
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 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 Nlines 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)
分析
就是最小生成树,只不过要对输入进行处理:已经连接的两点合并到一起,这里用的是Kruskal算法。下面是代码:
#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespace std;const int maxn = 800;struct node{ int x; int y; int vis;} G[maxn];int u[maxn*maxn],v[maxn*maxn];int r[maxn*maxn],p[maxn];double w[maxn*maxn];int n,m;int cmp(const int i,const int j){ return w[i] < w[j];}int find(int x){ if(x != p[x]) { p[x] = find(p[x]); } return p[x];}double Kruskal(int num){ double ans = 0; sort(r+1,r+num,cmp); for(int i = 1; i < num; i++) { int e = r[i]; int x = find(u[e]); int y = find(v[e]); if(x != y) { ans += w[e]; p[x] = y; } } return ans;}int main(){ while(scanf("%d",&n) != EOF) { int x,y; for(int i = 1; i <= n; i++) { scanf("%d%d",&x,&y); G[i].x = x; G[i].y = y; G[i].vis = 1; p[i] = i; } scanf("%d",&m); for(int i = 1; i <= m; i++) { scanf("%d%d",&x,&y); int px = find(x); int py = find(y); if(px != py) { p[px] = py; } } double dx,dy; int num = 1; for(int i = 1; i <= n; i++) { for(int j = i + 1; j <= n; j++) { u[num] = i; v[num] = j; r[num] = num; dx = G[i].x - G[j].x; dy = G[i].y - G[j].y; w[num++] = sqrt(dx*dx + dy*dy); } } printf("%.2lf\n",Kruskal(num)); }}
- 10397 - Connect the Campus
- 10397 - Connect the Campus
- 10397 - Connect the Campus
- UVaOJ 10397 - Connect the campus
- uva 10397 - Connect the Campus
- UVA 10397 - Connect the Campus
- UVa 10397 - Connect the Campus
- Uva 10397 - Connect the Campus
- UVa 10397 - Connect the Campus
- UVa 10397: Connect the Campus
- Uva - 10397 - Connect the Campus
- UVA - 10397 Connect the Campus
- UVa 10397 - Connect the Campus
- UVa 10397 Connect the Campus
- UVA 10397 - Connect the Campus
- UVa 10397 - Connect the Campus
- UVA - 10397 Connect the Campus kruskal算法
- UVA 10397 - Connect the Campus 翻译
- 在QT里添加图片资源
- Visual Studio 2010 更新NuGet Package Manager出错解决办法
- 循环实现递归
- 黑马程序员 java高新技术<二>--java5的枚举、反射的深入讲解
- poj2411 2663 2420 dp+状态压缩(多米诺骨牌问题)
- 10397 - Connect the Campus
- virtualbox 虚拟ubuntu如何全屏显示
- 实习第一周
- 调用c的printf
- 分割字符串 sscanf的用法
- BZOJ 2599 Race 点的分治
- HDOJ 1069
- 将JAVA 项目打包成JAR 并运行
- wp8小钟表