poj 1751 Highways(Kruskal)

来源:互联网 发布:美孚齿轮油632数据 编辑:程序博客网 时间:2024/05/21 09:39
 Highways

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

题意:一个城市中有n个城镇,现在已经修了m条铁路,问,要使每一个城镇都能到达其他的城镇的最小花费是多少
思路:Kruskal模板题,

入坑:
1.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. 
这一句没理解什么意思,还以为这种情况是直接输出空行呢,其实直接跳过就好了
2.只有单组输入,多组输入分分钟TEL
3.合并已经修的m条道路时,要join(u,v),不要只是pre[u]=v,因为要改变的是集合的关系

代码:
#include<stdio.h>#include<math.h>#include<queue>#include<map>#include<stdlib.h>#include<iostream>#include<stack>#include<string.h>#include<algorithm>using namespace std;#define min(a,b) (a<b?a:b)#define max(a,b) (a>b?a:b)#define LL long long intconst int inf=0x3f3f3f3f;#define maxn 750+10#define maxv 600000+10struct egde{    int u,v;    double w;} e[maxv];struct node{    int x,y;} g[maxv];int pre[maxn];int n,m,len;bool cmp(egde a,egde b){    return a.w<b.w;}void init(){    for(int i=1; i<maxn; i++)        pre[i]=i;}int Find(int x){    if(x==pre[x])        return x;    pre[x]=Find(pre[x]);    return pre[x];}int join(int x,int y){    int fx=Find(x),fy=Find(y);    if(fx!=fy)    {        pre[fy]=fx;        return 1;    }    return 0;}void Kruckal(){    sort(e,e+len,cmp);    int cont=0;    for(int i=0; i<len; i++)    {        if(join(e[i].u,e[i].v))            g[cont].x=e[i].u,g[cont++].y=e[i].v;        if(cont==n-1)            break;    }    for(int i=0; i<cont; i++)        printf("%d %d\n",g[i].x,g[i].y);}int main(){    scanf("%d",&n);    init();    int i,j;    len=0;    for(i=0; i<n; i++)    {        scanf("%d%d",&g[i].x,&g[i].y);        for(j=0; j<i; j++)        {            double w=sqrt(1.0*(g[i].x-g[j].x)*(g[i].x-g[j].x)+1.0*(g[i].y-g[j].y)*(g[i].y-g[j].y));            e[len].u=i+1,e[len].v=j+1,e[len++].w=w;        }    }    int u,v;    scanf("%d",&m);    for(i=0; i<m; i++)    {        scanf("%d%d",&u,&v);        join(u,v);    }    Kruckal();    return 0;}


1 0