Highways POJ 1751 【Prim || Kruscal】

来源:互联网 发布:淘宝助手怎么下架宝贝 编辑:程序博客网 时间:2024/03/29 21:37

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 ithtown (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


TLE了十次,我也是醉了

//Prim

#include <stdio.h>#include <string.h>#include <math.h>#define N 800#define INF 0x3f3f3f3fint n;int i,j;int map[N][N];bool vis[N];//标记是否已经放入最小生成树的那个集合里了 int low[N];//记录不在已经加入最小生成树的这个集合里的元素到这个 集合的最小距离 int x[N],y[N];int near[N];int a,b;int dis(int i,int j){return (x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);} void input(){//memset(map,INF,sizeof(map));for(i=1;i<=n;++i)scanf("%d%d",&x[i],&y[i]);for(i=1;i<=n;++i){for(j=1;j<=n;++j){if(i==j) map[i][j]=INF;else if(i<j) map[i][j]=map[j][i]=dis(i,j);}}int m;scanf("%d",&m);while(m--){scanf("%d%d",&a,&b);//vis[a]=vis[b]=1;//把已经连接了的两个点加入最小生成树集合 map[a][b]=map[b][a]=0; //把ab两点的权值变为0 }}void prim(){memset(vis,0,sizeof(vis));int pos=1;//从1开始 for(i=1;i<=n;++i)//第一次给low赋值 {low[i]=map[1][i];near[i]=1;}vis[1]=1;//已经找到一个点1,再找n-1个for(i=1;i<n;++i){int min=INF;for(j=1;j<=n;++j){if(!vis[j]&&min>low[j])//找下一个点到这个集合的最小值 {min=low[j];//记下这个最小值 pos=j;//记下这个点 }}if(!vis[pos]&&min==0)vis[pos]=1;//把刚刚找到的这个点加入集合else{printf("%d %d\n",near[pos],pos);vis[pos]=1;}for(j=1;j<=n;++j) //更新low数组 {if(!vis[j]&&low[j]>map[pos][j]){low[j]=map[pos][j];near[j]=pos;}}}}int main(){scanf("%d",&n);input();prim();return 0;}

//Kruscal

#include<cstdio>#include<algorithm>#include<cmath>#define N 800using namespace std;struct node {int u,v;double w;};node  arr[N*N/2];int per[N];int n;int x[N],y[N];bool cmp(node a,node b){return a.w<b.w;}double dist(int i,int j){return (y[j]-y[i])*(y[j]-y[i])+(x[i]-x[j])*(x[i]-x[j]);}void init(){for(int i=1;i<=n;++i){per[i]=i;}}int find(int x){if(x==per[x]) return x;return per[x]=find(per[x]);}bool join(int x,int y){int fx=find(x);int fy=find(y);if(fx!=fy){per[fx]=fy;return 1;} return 0;}int main(){int T;int i,j,k;int a,b;scanf("%d",&n);{init();for(i=1;i<=n;++i){scanf("%d%d",&x[i],&y[i]);}//m=n*(n-1)/2;k=0;for(i=1;i<=n;++i){for(j=i+1;j<=n;++j){arr[k].u=i;arr[k].v=j;arr[k++].w=dist(i,j);}}scanf("%d",&T);while(T--){scanf("%d%d",&a,&b);join(a,b);}sort(arr,arr+k,cmp);for(i=0;i<k;++i){if(join(arr[i].u,arr[i].v)){printf("%d %d\n",arr[i].u,arr[i].v);}}}return 0;}



0 0
原创粉丝点击