• uva 10397


    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 <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
    ", 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;
    }
  • 相关阅读:
    java中volatile关键字的含义
    2019年个人总结
    跟随Javac代码来解答字节码的疑惑
    Python装饰器实现带参数和不带参数
    try with resource当中你没有注意到点。。
    IDEA中,已经被加到版本库的文件如何在提交的时候忽略它们
    连接mysql客户端报错: java.sql.SQLException: Unable to load authentication plugin 'caching_sha2_password'
    从字节码层次看i++和++i
    对lambda表达式的字节码实现个人理解
    解决mvn clean install的报错The packaging for this project did not assign a file to the build artifact
  • 原文地址:https://www.cnblogs.com/jecyhw/p/3916870.html
Copyright © 2020-2023  润新知