分享
 
 
 

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

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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");

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有