【最小生成树专题】POJ 1751 Highways

 POJ 1751 Highways

Highways

题目链接->http://poj.org/problem?id=1751

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 18416 Accepted: 5415 Special Judge

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 ith 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

Source

Northeastern Europe 1999

Problem Idea

 【题意】
     题目理解:“有必要建设更多的高速公路,以便在任何一对城镇之间行驶而不会离开高速公路系统”说明图的连通性问题,“Flatopian政府希望减少建造新公路的成本。最便宜的高速公路系统将使公路总长度最小化。”说明本题要求一棵最小生成树。

  题意简述:现有一个N个城市M条路的无向图,要求你手动输入N个城市的坐标,现在该无向图已经有M条边了,问你还需要添加总长为多少的边能使得该无向图连通,输出需要添加边的两端点编号即可.

     也就是说,所求目标的最小生成树中,现在已有了一些边存在,求剩下需要添加的边,使得成为一棵最小生成树。

 【类型】

  最小生成树

 【分析】

  Kruskal算法,求最小生成树,模板题,入门。       区别:区别与传统的题目,“根据Kruskal算法求起边长之和”,所不同的是,这道题目是要判断哪些边已经存在于

最小生成树MST中,哪些边是不在MST中的。 同时需要自己根据各个节点的位置坐标,求现在已存在边的长度

     【思路】

  本题就是求最小生成树的,但是由于本题不需要输出最终生成树的权值,那么我们在求两点距离的时候时间需要保存距离 dist=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);即可,不用sqrt开方(因为开方费时间).
       然后对于已经连接上的边,我们令他们属于同一个并查集即可(或令这些边长为0,并添加到无向图中去也行)

//对于现在没有建设好的高速公路,对边长赋值
        for(int i=1;i<=n;i++){
            for(int j=i+1;j<=n;j++){
                KK.AddEdge(i,j,getDist(i,j));
            }
        }
scanf("%d",&m);
        for(int i=0;i<m;i++){//对于现有的已经建设好的道路,重新赋值为0
            int u,v;
            scanf("%d%d",&u,&v);
            KK.AddEdge(u,v,0);
        }

 【输入输出要求】

  输入输出要求,用中文复述一下:

  第一行给定一个实数N,作为城市节点的数量,即图中的节点数量。
  接下来的N行给出了每个城市的位置坐标。 

  <横坐标> <纵坐标>
  接下来一行:M,现在图中已经存在的边数。
  接下来的M行
  <节点A> <节点B> 
  最后需要输出添加边的两端点编号。

  【注意】

   1、代码提交要用G++,不能用C++,否则会运行超时。

Source Code

#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
#include <cstdio>

using namespace std;
const int nmax=750+10;
const int mmax=500000+10;

struct  Edge{
    int  u,v;
    int dist;
    Edge(){}
    Edge(int u,int v,int d):u(u),v(v),dist(d){}
    bool operator<(const Edge&rhs)const{
        return dist<rhs.dist;
    }
};
struct Kruskal{
    int n,m;//无向图的点数n和边数m
    Edge edges[mmax];    //定义静态数组的边列表
    //vector<Edge> edges;//定义动态数组的边列表

    int fa[nmax];
    int findset(int x){
        return fa[x]==-1?x:fa[x]=findset(fa[x]);
    }
    void init(int n){
        this->n=n;
        m=0;
        memset(fa,-1, sizeof(fa));
    }
    void AddEdge(int u,int v, int dist){
        edges[m++]=Edge(u,v,dist);
        //如若定义动态数组
        //edges.push_back(Edge(u,v,dist));
        //m=edges.size();
    }
    int kruskal(){
        int sum=0;//最小生成树的边长之和
        int cnt=0;
        sort(edges,edges+m);

        for(int i=0;i<m;i++) {
            int u = edges[i].u;//得到第i条边的起点
            int v = edges[i].v;//得到第i条边的终点
            if(findset(u)!=findset(v)){//如果这两个点不在同一个连通分量
                if(edges[i].dist>0){
                    printf("%d %d\n",u,v);
                }
                sum+=edges[i].dist;//同步更新生成树的边长之和
                fa[findset(u)]=findset(v);//合并这两个连通分量
                if(++cnt>=n-1){//如果图中仅剩一个连通分量,则退出
                    break;
                }
            }
        }
        if(cnt<n-1) return -1;//当最小生成树不存在时,返回-1
        return sum;//如若最小生成树存在,返回边长之和

    }
}KK;
struct Point{
    int x,y;
}p[nmax];
int getDist(int u,int v){
    int d=(p[u].x-p[v].x)*(p[u].x-p[v].x)+(p[u].y-p[v].y)*(p[u].y-p[v].y);
    return d;
}
int main() {
    int n,m;//无向图的点数n和边数m
    while(scanf("%d",&n)==1){
        KK.init(n);
        for(int i=1;i<=n;i++){//初始化n个节点的位置坐标
            scanf("%d%d",&p[i].x,&p[i].y);
        }
        //对于现在没有建设好的高速公路,对边长赋值
        for(int i=1;i<=n;i++){
            for(int j=i+1;j<=n;j++){
                KK.AddEdge(i,j,getDist(i,j));
            }
        }
        scanf("%d",&m);
        for(int i=0;i<m;i++){//对于现有的已经建设好的道路,重新赋值为0
            int u,v;
            scanf("%d%d",&u,&v);
            KK.AddEdge(u,v,0);
        }
        int ans=KK.kruskal();
        //printf("%d",ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值