POJ 1751 Highways(最小生成树 prim算法)
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.
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.
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.
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
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
代码:
1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 5 int m,n; 6 7 const int NUM=751; 8 const double inf=9999999.00; 9 10 bool built[NUM][NUM]; 11 bool used[NUM]; 12 13 struct node 14 { 15 double cost; 16 int con; 17 }lowercost[NUM]; 18 19 struct point 20 { 21 int x; 22 int y; 23 }p[NUM]; 24 25 double caldisc(point a,point b) 26 { 27 return sqrt((double)((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y))); 28 } 29 30 void prim() 31 { 32 int i,j,k; 33 int pos; 34 used[1]=true; 35 for(i=2;i<=m;i++) 36 { 37 if(built[1][i]) 38 lowercost[i].cost=0.0; 39 else 40 lowercost[i].cost=caldisc(p[1],p[i]); 41 lowercost[i].con=1; 42 } 43 for(i=1;i<m;i++) 44 { 45 double Min=inf; 46 for(j=1;j<=m;j++) 47 { 48 if(!used[j]&&lowercost[j].cost<Min) 49 { 50 pos=j; 51 Min=lowercost[j].cost; 52 } 53 } 54 if(Min==inf)return; 55 if(lowercost[pos].cost!=0.0) 56 printf("%d %d\n",lowercost[pos].con,pos); 57 used[pos]=true; 58 for(k=1;k<=m;k++) 59 { 60 if(!used[k]&&k!=pos) 61 { 62 if(built[pos][k]) 63 { 64 lowercost[k].cost=0.0; 65 lowercost[k].con=pos; 66 continue; 67 } 68 else 69 { 70 double temp=caldisc(p[k],p[pos]); 71 if(temp<lowercost[k].cost) 72 { 73 lowercost[k].cost=temp; 74 lowercost[k].con=pos; 75 } 76 } 77 } 78 } 79 } 80 } 81 82 int main() 83 { 84 // freopen("in.txt","r",stdin); 85 int i,a,b; 86 memset(built,false,sizeof(built)); 87 memset(used,false,sizeof(used)); 88 scanf("%d",&m); 89 for(i=1;i<=m;i++) 90 scanf("%d%d",&p[i].x,&p[i].y); 91 scanf("%d",&n); 92 while(n--) 93 { 94 scanf("%d%d",&a,&b); 95 built[a][b]=built[b][a]=true; 96 } 97 prim(); 98 return 0; 99 }