ZOJ 2048 Highways 最小生成树 Kruskal && Prim

来源:互联网 发布:用爸妈身份证开淘宝店 编辑:程序博客网 时间:2024/05/20 09:46

ZOJ Problem Set - 2048
Highways

Time Limit: 5 Seconds      Memory Limit: 32768 KB      Special Judge

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 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 should be created but it should be empty.


This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.


Sample Input


1

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

注-此题为:ZOJ 2048 Highways 

题意:
             给定n个城市,给定了已经连接起来的道路,输出的是城镇的编号,

         最小生成树 Kruskal && Prim    (注意格式要求 a blank line between output blocks)

        此题 要输出路径,用Kruskal  较简单,用Prim 需要 标记 

已AC代码:(Kruskal

#include<cstdio>#include<cmath>#define MAX 1000000#include<algorithm>using namespace std;int n,m,per[1100];   // 并查集 int x[1100],y[1100];struct node{int u,v;double w;    //w为距离 }s[MAX];bool cmp(node a,node b){return a.w<b.w;}double dlen(int i,int j){return sqrt( 1.0*( (x[i]-x[j])*(x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j]) ) );}void into()    //初始化 {for(int i=0;i<=n;++i)per[i]=i;}int find(int x)    // 查找根节点 {return x==per[x]?x:per[x]=find(per[x]);}bool join(int a,int b)    //合并根节点,并判断是否成环 {int fa=find(a);int fb=find(b);if(fa!=fb){per[fa]=fb;return true;}return false;}int main() {int T,i,j;scanf("%d",&T);while(T--){scanf("%d",&n);for(i=1;i<=n;++i)scanf("%d%d",&x[i],&y[i]);int k=0;for(i=1;i<n;++i){for(j=i+1;j<=n;++j){s[k].u=i;s[k].v=j;s[k].w=dlen(i,j);k++;}}into();  //初始化根节点 int a,b,c;scanf("%d",&m);while(m--){scanf("%d%d",&a,&b);c=join(a,b);  //已连得路 }sort(s,s+k,cmp);  //按距离从小到大排序 for(i=0;i<k;++i)if(join(s[i].u,s[i].v))   //输出的是城镇的编号,printf("%d %d\n",s[i].u,s[i].v);if(T!=0)printf("\n");}return 0;}

已AC代码:(Prim

#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#define INF 0xfffffffusing namespace std;double map[1100][1100],low[1100];int n,m,vis[1100];  //map二维数组存图,low记录每2个点间最小权值,vis标记某点是否已访问int x[1100],y[1100];double dlen(int i,int j){return sqrt( 1.0*( (x[i]-x[j])*(x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j]) ) );}double prim(){memset(vis,0,sizeof(vis));int i,j,pos,t,bj[1100];  //bj 标记已连的路 double MIN;for(i=1;i<=n;++i)  //从某点开始,分别标记vis和记录该点pos{low[i]=map[1][i];  //第一次给low数组赋值 map的第一行 bj[i]=1;}vis[1]=1;pos=1;t=0;for(i=1;i<n;++i)   //再运行n-1次,一次找一个最小 {MIN=INF;pos=0;for(j=1;j<=n;++j){if(vis[j]==0&&low[j]<MIN){MIN=low[j];  // 找出最小值min,记录位置pospos=j;}}if(map[bj[pos]][pos]!=0)printf("%d %d\n",pos,bj[pos]);vis[pos]=1;   //标记该点已访问 for(j=1;j<=n;++j)    //更新权值low 把 map的 pos 行中比对应的 low 小的赋给low if(vis[j]==0&&low[j]>map[pos][j]) {low[j]=map[pos][j];bj[j]=pos;}}}int main() {int T,i,j;scanf("%d",&T);while(T--){scanf("%d",&n);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)map[i][j]=INF;for(i=1;i<n;++i){for(j=i+1;j<=n;++j){map[i][j]=map[j][i]=dlen(i,j);}}int a,b,c;scanf("%d",&m);while(m--){scanf("%d%d",&a,&b); //已连的赋为 0 map[a][b]=map[b][a]=0;}prim();if(T!=0)printf("\n");}return 0;}

0 0