UVa 10397 - Connect the Campus

来源:互联网 发布:2015淘宝刷钻价格表 编辑:程序博客网 时间:2024/05/20 09:24

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


最小生成树问题+并查集

kruskai算法

已经连通的点就把他们合并起来

有一点需要注意,楼的编号从1开始


#include<cstdio>#include <iostream>#include<algorithm>#include <math.h>#include <string.h>#define N 800using namespace std;int coor[N][2], n, m, father[N];struct Node{    int x, y;    double len;    friend bool operator < (const Node &a, const Node &b)    {        return a.len < b.len;    }    void fun()    {        int a = coor[x][0] - coor[y][0];        int b = coor[x][1] - coor[y][1];        len = sqrt(a*a + b*b + 0.0);    }}node[640000];int find(int x){    if (x != father[x])    {        father[x] = find(father[x]);    }    return father[x];}int main(){#ifndef ONLINE_JUDGE    freopen("1.txt", "r", stdin);#endif    int i, j, k, a, b, t, aa, bb;    double  ans;    while(~scanf("%d", &n))    {        for (i = 1; i <= n; i++)        {            scanf("%d%d", &coor[i][0], &coor[i][1]);            father[i] = i;        }        for (i = 1,k = 0; i <= n; i++)        {            for (j = 1;  j < i; j++)            {                node[k].x = i;                node[k].y = j;                node[k].fun();                k++;            }        }        scanf("%d", &m);        for (i = 0; i < m; i++)        {            scanf("%d%d", &a, &b);            aa = find(a);            bb = find(b);            if (aa != bb)            {                father[aa] = bb;            }        }        sort(node, node + k);        ans = 0;        for (i = 0; i < k; i++)        {            a = find(node[i].x);            b = find(node[i].y);            if (a == b)            {                continue;            }            ans += node[i].len;            father[a] = b;        }        printf("%.2lf\n", ans);    }    return 0;}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 淘宝卖精仿遇到打假怎么办? 没有收到提货码怎么办 增值税发票超过180天怎么办 花呗借钱还不上怎么办 实体店里没人气怎么办 EMS快件无人签收怎么办 邮政快递丢失了怎么办 快递手机丢了怎么办 拍照签收没收到怎么办 拼多多开店货源怎么办 被购买鉴定投诉怎么办 淘宝上买东西错想用花呗怎么办 淘宝想用花呗买东西差一点怎么办 买了假货怎么办还用吗 免税店买完东西怎么办 淘宝恶意拍单怎么办 二手西服不想要怎么办 苹果8p费电快怎么办 赌博被骗没证据怎么办 异地车子办保险怎么办 淘宝买到旧货怎么办 苹果官网买手机发票怎么办 车子异地换牌照怎么办 汽车异地上保险怎么办 后悔买保险了怎么办 平安run不计步怎么办 天梭力洛克有划痕怎么办 天梭手按钮坏了怎么办 天梭机械表总慢怎么办 买的天梭表没电了怎么办 天猫遇见假货怎么办 苹果六s内存不足怎么办 发票丢了报销怎么办 荣耀v9玩游戏卡怎么办 无极限不退货怎么办 高中想买手机怎么办 发票搞丢了怎么办 买车发票丢失怎么办 钻戒发票丢了怎么办 电子发票税号写错了怎么办 手机分期逾期了怎么办