码迷,mamicode.com
首页 > 其他好文 > 详细

Highways

时间:2015-07-28 20:44:31      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:

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\n", j, Q[j].v);  // 如果边权值是-1,小于0,就是该点已经连到树上,就不用输出,反之就是要连接的树~和其边权值
        }
    }
    return 0;
}

 



专题链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82831#overview

Highways

标签:

原文地址:http://www.cnblogs.com/Tinamei/p/4684125.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!