• Highways


    Highways Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

    Description

    The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways. The Flatopian government is aware of this problem and has already constructed a number of highways connecting some of the most important towns. However, there are still some towns that you can't reach via a highway. It is necessary to build more highways so that it will be possible to drive between any pair of towns without leaving the highway system.

    Flatopian towns are numbered from 1 to N and town i has a position given by the Cartesian coordinates (xi, yi). Each highway connects exaclty two towns. All highways (both the original ones and the ones that are to be built) follow straight lines, and thus their length is equal to Cartesian distance between towns. All highways can be used in both directions. Highways can freely cross each other, but a driver can only switch between highways at a town that is located at the end of both highways.

    The Flatopian government wants to minimize the cost of building new highways. However, they want to guarantee that every town is highway-reachable from every other town. Since Flatopia is so flat, the cost of a highway is always proportional to its length. Thus, the least expensive highway system will be the one that minimizes the total highways length.

    Input

    The input consists of two parts. The first part describes all towns in the country, and the second part describes all of the highways that have already been built.

    The first line of the input file contains a single integer N (1 <= N <= 750), representing the number of towns. The next N lines each contain two integers, xi and yi separated by a space. These values give the coordinates of i th town (for i from 1 to N). Coordinates will have an absolute value no greater than 10000. Every town has a unique location.

    The next line contains a single integer M (0 <= M <= 1000), representing the number of existing highways. The next M lines each contain a pair of integers separated by a space. These two integers give a pair of town numbers which are already connected by a highway. Each pair of towns is connected by at most one highway.

    Output

    Write to the output a single line for each new highway that should be built in order to connect all towns with minimal possible total length of new highways. Each highway should be presented by printing town numbers that this highway connects, separated by a space.

    If no new highways need to be built (all towns are already connected), then the output file should be created but it should be empty.

    Sample Input

    9
    1 5
    0 0 
    3 2
    4 5
    5 1
    0 4
    5 2
    1 2
    5 3
    3
    1 3
    9 7
    1 2

    Sample Output

    1 6
    3 7
    4 9
    5 7
    8 3
    

    最小生成树,有的树之间有联系, 有的没有。求怎样连可以把所有点在一棵树上,而且权值和最小。只要权值最小就行。关键是怎样连最小,
    已经确定有联系的树的权值要避免对接下来最小值的判断的影响,全置为-1,表示这两点边权值最小,看其他边权值的比较,谁最小连谁,最后根据边权值是否> 0判断是否新连接的

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    #include<algorithm>
    
    using namespace std;
    
    #define N 1100
    #define INF 0xfffffff
    
    int n, m, vis[N];
    double maps[N][N];
    
    struct node
    {
        int x, y;
    }P[N];  // 存该点坐标
    
    struct point
    {
        int v;
        double len;
    }Q[N];   // 存该点连到树上的长度len,和通过谁连到的树上v
    
    void prim()
    {
    
        vis[1] = 1;
    
        for(int i = 1; i <= n; i++)
            Q[i].len = maps[i][1], Q[i].v = 1;  // 最开始都是通过1连到树上的,距离也是其与1的距离,已经连上的是-1,最小的
    
        for(int i = 1; i < n; i++)
        {
            int index = 0;
            double Min = INF;
    
            for(int j = 1; j <= n; j++)
            {
                if(!vis[j] && Q[j].len < Min)
                    Min = Q[j].len, index = j;
             }
    
             vis[index] = 1;
    
             for(int j = 1; j <= n; j++)
             {
                 if(!vis[j] && Q[j].len > maps[j][index])
                    Q[j].len = maps[j][index], Q[j].v = index;  // 求最小生成树,如果边权值改变,所连的树 v 也改变,本题也就是问你树跟哪棵树边权值连最小
             }
        }
    }
    
    int main()
    {
        int a, b;
    
        while(cin >> n)
        {
            memset(vis, 0, sizeof(vis));
    
            for(int i = 1; i <= n; i++)
            {
                cin >> P[i].x >> P[i].y;
            }
    
            for(int i = 1; i <= n; i++)
            {
                for(int j = 1; j <= n; j++)
                {
                    int nx = P[i].x-P[j].x, ny = P[i].y-P[j].y;
                    double g = sqrt(nx*nx+ny*ny);
                    maps[i][j] = maps[j][i] = g;  // 求两棵树的距离
                }
            }
            cin >> m;
    
            for(int j = 0; j < m; j++)
            {
                cin >> a >> b;
                maps[a][b] = maps[b][a] = -1;
            }
    
            prim();
    
    
            for(int j = 2; j <= n; j++)
            {
                if(Q[j].len > 0)
                    printf("%d %d
    ", j, Q[j].v);  // 如果边权值是-1,小于0,就是该点已经连到树上,就不用输出,反之就是要连接的树~和其边权值
            }
        }
        return 0;
    }


    专题链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82831#overview
    让未来到来 让过去过去
  • 相关阅读:
    深入理解 ProtoBuf 原理与工程实践(概述)
    高性能缓存 Caffeine 原理及实战
    Java 多线程上下文传递在复杂场景下的实践
    SpringBoot 2.0 中 HikariCP 数据库连接池原理解析
    MySQL 5.6.35 索引优化导致的死锁案例解析
    gitlab安装升级(大版本跨度9.4.5----13.2.1)
    mysql 查看表的索引
    python安装mysql库 ,MySQL-python
    Alpine包管理工具apk使用介绍
    docker容器添加hosts
  • 原文地址:https://www.cnblogs.com/Tinamei/p/4684125.html
Copyright © 2020-2023  润新知