POJ - 1751----Highways(Kruskal)

来源:互联网 发布:淘宝怎么添加二级页面 编辑:程序博客网 时间:2024/05/29 02:43

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 Input91 50 0 3 24 55 10 45 21 25 331 39 71 2Sample Output1 63 74 95 78 3

题目大意: 大意是一个有n个城市的国家,已知有些城市有道路联通,问增加哪些道路使得所有的城市都可以彼此联通代价最小。
思路:
注意注意,样例的答案不唯一
//最小生成树Kruskal算法
//题目巨坑,样例的输出完全不是题目给的,害的检查了好多遍,
//最后还是试着提交,就突然过了,过了,过了。。。

#include<cstdio> #include<iostream>#include<algorithm> #include<cmath>#include<string.h>using namespace std;const int MAX = 1000;const int INF = 0x3f3f3f3f;//最小生成树Kruskal算法//题目巨坑,样例的输出完全不是题目给的,害的检查了好多遍,//最后还是试着提交,就突然过了,过了,过了。。。 struct dot{//顶点     int x,y;    //点的坐标 }D[MAX];struct edge{//边     int u,v;    double w;  //边的起始点,结束点,权值 }E[MAX*MAX/2];int numdot;//顶点的数目 int pre[MAX];bool cmp(const edge &a, const edge &b){    return a.w<b.w;}//并查集int find(int x){        //查找根节点    int r=x;    while(pre[r] != r){  //返回根节点r        r=pre[r];    }     int i=x,j;    while(i != r){     //路径压缩(为了将x~r这条路上所有的点的父节点直接设为r)        j = pre[i];    //在改变上级之前用临时变量j记录下他的值         pre[i] = r;    //把上级改为根节点        i = j;    }    return r;}//判断加入xy边是否形成连通如果已经连通则说明会形成回路返回false //如果不连通,就把它们所在的连通分支合并返回true   bool join(int x,int y){        int fx=find(x),fy=find(y);    if(fx != fy){         pre[fx] = fy;        return true;     }    return false; }void Kruskal(void){     //共numdot*(numdot-1)/2条边     for(int i=1 ; i<=numdot*(numdot-1)/2 ; i++){        if(join(E[i].u,E[i].v)){            printf("%d %d\n",E[i].u,E[i].v);        }    }    return;}  int main(void){     cin>>numdot;    //初始化并查集    for(int i=1 ; i<=numdot ; i++){        pre[i] = i;//每个点的上级为自己     }     //输入顶点信息     for(int i=1 ; i<=numdot ; i++){          cin>>D[i].x>>D[i].y;    }    //赋边值     int n;    cin>>n;    for(int i=0 ; i<n ; i++){        int a,b;        cin>>a>>b;        //加入边         join(a,b);    }    int k=1;    for(int i=1 ; i<=numdot ; i++){        for(int j=i+1 ; j<=numdot ; j++){            E[k].u = i;            E[k].v = j;            E[k].w = sqrt((D[i].x-D[j].x)*(D[i].x-D[j].x)+                          (D[i].y-D[j].y)*(D[i].y-D[j].y));            k++;        }     }    //对边排序     sort(E+1,E+k,cmp);    Kruskal();    return 0;  } 
0 0
原创粉丝点击