最小生成树的应用--城市高速公路问题

类别:编程语言 点击:0 评论:0 推荐:
Highways

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


以下是我的Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_IMT 100000000
#define M 750
int prim(int w[][M],int n,int v,int tree[][3]){
 int i,j,k,p,min,c;
 int lowcost[M],closest[M];
 for(i=0;i<n;i++){
  closest[i]=v;
  lowcost[i]=w[v][i];
 }
 c=0;p=0;
 for(i=0;i<n-1;i++){
  min=MAX_IMT;
  for(j=0;j<n;j++)
   if(lowcost[j]!=-1 && lowcost[j]<min){
    min=lowcost[j];
    k=j;
   }
  tree[p][0]=closest[k];
  tree[p][1]=k;
  tree[p][2]=min;p++;
  c+=min;
  lowcost[k]=-1;
  for(j=0;j<n;j++)
   if(w[k][j]!=-1 && w[k][j]<lowcost[j]){
    lowcost[j]=w[k][j];
    closest[j]=k;
   }
 }
 return c;
}
int process(){
 int town[M][2],n=0,m=0,i=0,j=0,tempa=0,tempb=0;
 int w[M][M],MST[M-1][3];
 scanf("%d",&n);
 for(i=0;i<=n-1;i++)
  scanf("%d %d",&town[i][0],&town[i][1]);
 for(i=0;i<=n-1;i++)
  for(j=0;j<=n-1;j++){
    if(i==j)w[i][j]=-1;
    else w[i][j]=(town[i][0]-town[j][0])*(town[i][0]-town[j][0])+(town[i][1]-town[j][1])*(town[i][1]-town[j][1]);
    }
 scanf("%d",&m);
 for(j=0;j<=m-1;j++){
  scanf("%d %d",&tempa,&tempb);
  w[tempa-1][tempb-1]=0;
  w[tempb-1][tempa-1]=0;
 }
 
 prim(w,n,0,MST);
 for(i=0;i<=n-2;i++)
  if(MST[i][2]>0)
   printf("%d %d\n",MST[i][0]+1,MST[i][1]+1);
 return 0;
}
int main(){
 int cn,i;
 scanf("%d",&cn);
 for(i=0;i<cn;i++){
   process();
   if(i<cn-1)printf("\n");
  }
}

本文地址:http://com.8s8s.com/it/it23457.htm