poj1751-最小生成树

来源:互联网 发布:大学宿舍网络怎么样 编辑:程序博客网 时间:2024/04/30 14:28
Highways
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 7310 Accepted: 2014 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

91 50 0 3 24 55 10 45 21 25 331 39 71 2

Sample Output

1 63 74 95 78 3

Source

Northeastern Europe 1999

提示:答案不唯一.
/*最小生成树*/#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>using namespace std;const int maxn1=800;const int maxn2=290000;int i,n,m;int ancestor[maxn1];struct Town              //城镇  坐标(x,y){int x;int y;}town[maxn1];           //内存池--保存所有城镇坐标.struct Highway          //高速路 两个城镇的编号(town1,town2){int town1;int town2;}highway[maxn1*maxn1];   //内存池--保存所有高速公路.//*********并查集*****************int get_ancestor(int num)  {if(ancestor[num]==num) return ancestor[num];else return ancestor[num]=get_ancestor(ancestor[num]);}void merge(int num1,int num2){ancestor[get_ancestor(num1)]=get_ancestor(num2);//father[getanc(a)] = getanc(b);}//******************************//输入函数void input(){scanf("%d",&n);for(i=1;i<=n;i++)scanf("%d%d",&town[i].x,&town[i].y);for(i=1;i<=n;i++)ancestor[i]=i;scanf("%d",&m);int k1,k2;for(i=1;i<=m;i++){scanf("%d%d",&k1,&k2);merge(k1,k2);}m=1;for(i=1;i<=n-1;i++)for(int j=i+1;j<=n;j++){highway[m].town1=i;highway[m].town2=j;m++;}}//计算两个城镇之间距离,即高速公路的长度.int dist(int a,int b){Town p;p.x=town[a].x-town[b].x;p.y=town[a].y-town[b].y;return p.x*p.x+p.y*p.y;}//重载<   应用于高速公路的长度比较.bool operator<(const Highway &a,const Highway &b){return dist(a.town1,a.town2)<dist(b.town1,b.town2);}//排完序后,用并查集生成 最小生成树!void work(){for(i=1;i<=m;i++)if(get_ancestor(highway[i].town1)!=get_ancestor(highway[i].town2)){merge(highway[i].town1,highway[i].town2);printf("%d %d\n",highway[i].town1,highway[i].town2);}}int main(){input();sort(highway+1,highway+m);                                      //printf("\n");                                     //for(int k1=1;k1<m;k1++)                                 //printf("highway[%d].town1=%d   highway[%d].town2=%d\n",k1,highway[k1].town1,k1,highway[k1].town2);                                     //printf("\n");work();return 0;}/*①:输入n--城镇个数②:输入n个城镇坐标.③:初始化并查集--祖先值.④:输入m--已经有的高速公路个数.⑤:输入m个已经有的高速公路所连接的城镇编号.⑥:合并--两个城镇的祖先值.⑦:计算出所有的高速公路的长度--构成一个完全图.⑧:高速公路长度 从小到大 排序 。⑨:利用并查集生成最小生成树.解释:拥有共同的祖先的话不会再合并(即不会形成环),如果没有共同祖先值的话(即不是连通的),因为排序了,所以是从长度(权值)最小的开始合并的。--得到了最小生成树.*/



原创粉丝点击