首页 > 代码库 > uva 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 thecampus of the University of Waterloo.The university has hired bricklayers, electricians, plumbers, and a computerprogrammer. A computer programmer? Yes, you have been hired to ensure that eachbuilding is connected to every other building (directly or indirectly) throughthe campus network of communication cables.

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

You have been given a campus map which shows thelocations of all buildings and existing communication cables. You must notalter the existing cables. Determine where to install new communication cablesso that all buildings are connected. Of course, the university wants you tominimize 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 givenbelow:

The first line of each test case contains thenumber 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. Thesecoordinates are integers with absolute values at most 10000. No two buildings occupy the same point. After that there isa line containing the number of existing cables M (0 <= M <= 1000) followed by M lines describing the existing cables. Each cable is representedby two integers: the building numbers which are directly connected by thecable. There is at most one cable directly connecting each pair of buildings.

Output

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

Sample Input

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

4
103 104

104100

104103

100100

1

4 2

 

Sample Output
4.41
4.41

#include <iostream>#include <stack>#include <cstring>#include <cstdio>#include <string>#include <algorithm>#include <queue>#include <set>#include <map>#include <fstream>#include <stack>#include <list>#include <sstream>#include <cmath>using namespace std;#define ms(arr, val) memset(arr, val, sizeof(arr))#define mc(dest, src) memcpy(dest, src, sizeof(src))#define N 755#define INF 0x3fffffff#define vint vector<int>#define setint set<int>#define mint map<int, int>#define lint list<int>#define sch stack<char>#define qch queue<char>#define sint stack<int>#define qint queue<int>/*最小生成树算法prim:稠密图(n*n),kruskal:稀疏图(e*lge)*/struct point{    int x, y;}in[N];bool visit[N];double low[N];double g[N][N];int n, m;int multi2(int x1, int x2, int y1, int y2){    int x = x1 - x2;    int y = y1 - y2;    return x * x + y * y;}void prim(){    ms(visit, 0);    double ans = 0;    int s = n - 1, _minp;    double _min;    for (int i = 1; i <= n; i++)    {        low[i] = g[1][i];    }    low[1] = INF;    visit[1] = 1;    while (s--)    {        _min = low[1];        for (int i = 1; i <= n; i++)//寻找最小边        {            if (!visit[i] && low[i] < _min)            {                _min = low[i];                _minp = i;            }        }        visit[_minp] = true;        ans += sqrt(_min);        for (int i = 1; i <= n; i++)//更新low数组        {            if (!visit[i] && g[i][_minp] < low[i])            {                low[i] = g[i][_minp];            }        }    }    printf("%.2lf\n", ans);}int main(){    while (~scanf("%d", &n))    {        for (int i = 1; i <= n; i++)        {            scanf("%d%d", &in[i].x, &in[i].y);            for (int j = 1; j < i; j++)            {                g[j][i] = g[i][j] = multi2(in[i].x, in[j].x, in[i].y, in[j].y);            }        }        ms(visit, 0);        int s, e;        scanf("%d", &m);        while (m--)        {            scanf("%d%d", &s, &e);            g[s][e] = g[e][s] = 0;//将已经连通的点之间的距离变为0,在prim算法中最短边肯定会被选择(关键之处)        }        prim();    }    return 0;}