24h購物| | PChome| 登入
2012-06-07 08:01:09| 人氣894| 回應0 | 上一篇 | 下一篇

[UVA][MST] 10397 - Connect the Campus

推薦 0 收藏 0 轉貼0 訂閱站台

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




#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct edge {
    int x, y, v;
};
int p[751], r[751];
void init(int n) {
    for(int i= 0; i <= n; i++)
        p[i] = i, r[i] = 1;
}
int find(int x) {
    return x == p[x] ? x : (p[x]=find(p[x]));
}
int joint(int x, int y) {
    x = find(x), y = find(y);
    if(x != y) {
        if(r[x] > r[y])
            r[x] += r[y], p[y] = x;
        else
            r[y] += r[x], p[x] = y;
        return 1;
    }
    return 0;
}
int cmp(const void *i, const void *j) {
    static edge *a, *b;
    a = (edge *)i, b = (edge *)j;
    return a->v - b->v;
}
edge D[500000];
int main() {
    int n, m, i, j;
    int x[750], y[750];
    while(scanf("%d", &n) == 1) {
        init(n);
        for(i = 0; i < n; i++) {
            scanf("%d %d", x+i, y+i);
        }
        scanf("%d", &m);
        int connect = 0;
        while(m--) {
            scanf("%d %d", &i, &j);
            i--, j--;
            connect += joint(i, j);
        }
        m = 0;
        for(i = 0; i < n; i++) {
            for(j = i+1; j < n; j++) {
                if(find(i) != find(j)) {
                    D[m].x = i, D[m].y = j;
                    D[m].v = (x[i]-x[j])*(x[i]-x[j])+
                        (y[i]-y[j])*(y[i]-y[j]);
                    m++;
                }
            }
        }
        qsort(D, m, sizeof(edge), cmp);
        double ans = 0;
        for(i = 0; i < m; i++) {
            if(joint(D[i].x, D[i].y)) {
                ans += sqrt(D[i].v);
                connect++;
                if(connect == n-1)
                    break;
            }
        }
        printf("%.2lf\n", ans);
    }
    return 0;
}

台長: Morris
人氣(894) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][正環][spfa] 10557 - XYZZY
此分類上一篇:[UVA][dfs] 165 - Stamps

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文