您的位置:首页 > 其它

UVA 10397 - Connect the Campus(最小生成树)

2013-11-23 11:11 417 查看
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 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

题意:给定n个校园坐标,有一些已经连通了,要求连接所有校园的最小代价

思路:最小生成树

代码:

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;

int t, n, i, j, mapn, parent[755], num, m, a, b;
double ans;
int find(int x) {
if (x == parent[x])
return parent[x];
return find(parent[x]);
}

struct Point {
double x, y;
} p[755];

struct Map {
int a, b;
double value;
} map[555555];

int cmp(Map a, Map b) {
return a.value < b.value;
}
int main() {
while (~scanf("%d", &n)) {
mapn = 0;
ans = 0;
num = 0;
for (i = 0; i < n; i ++) {
scanf("%lf%lf", &p[i].x, &p[i].y);
parent[i] = i;
}
for (i = 0; i < n; i ++)
for (j = i + 1; j < n; j ++) {
map[mapn].a = i;
map[mapn].b = j;
map[mapn ++].value = sqrt((p[i].x - p[j].x) * (p[i].x - p[j].x) + (p[i].y - p[j].y) * (p[i].y - p[j].y));
}
sort(map, map + mapn, cmp);
scanf("%d", &m);
while (m --) {
scanf("%d%d", &a, &b);
a --; b --;
int pa = find(a);
int pb = find(b);
if (pa != pb) {
parent[pa] = pb;
num ++;
}
}
for (i = 0; i < mapn; i ++) {
int pa = find(map[i].a);
int pb = find(map[i].b);
if (pa != pb) {
parent[pa] = pb;
ans += map[i].value;
num ++;
if (num == n - 1)
break;
}
}
printf("%.2lf\n", ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: