博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 1751 Highways (最小生成树)
阅读量:5909 次
发布时间:2019-06-19

本文共 4952 字,大约阅读时间需要 16 分钟。

Highways
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u
Submit     
Appoint description: System Crawler  (2015-06-02)

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 i 
th 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. 

Sample Input

91 50 0 3 24 55 10 45 21 25 331 39 71 2

Sample Output

1 63 74 95 78 3 计算的时候不用开方,每合并一次就输出一次。
1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 using namespace std; 15 16 const int SIZE = 800; 17 int FATHER[SIZE],N,M,NUM; 18 struct Node 19 { 20 int from,to; 21 double cost; 22 }G[SIZE * SIZE]; 23 struct 24 { 25 int x,y; 26 }TEMP[SIZE]; 27 28 void ini(void); 29 int find_father(int); 30 void unite(int,int); 31 bool same(int,int); 32 void kruskal(void); 33 bool comp(const Node &,const Node &); 34 double dis(int,int,int,int); 35 int main(void) 36 { 37 int x,y; 38 39 while(~scanf("%d",&N)) 40 { 41 ini(); 42 for(int i = 1;i <= N;i ++) 43 scanf("%d%d",&TEMP[i].x,&TEMP[i].y); 44 for(int i = 1;i <= N;i ++) 45 for(int j = i + 1;j <= N;j ++) 46 { 47 G[NUM].from = i; 48 G[NUM].to = j; 49 G[NUM].cost = dis(TEMP[i].x,TEMP[i].y,TEMP[j].x,TEMP[j].y); 50 NUM ++; 51 } 52 sort(G,G + NUM,comp); 53 scanf("%d",&M); 54 for(int i = 1;i <= M;i ++) 55 { 56 scanf("%d%d",&x,&y); 57 unite(x,y); 58 } 59 kruskal(); 60 } 61 62 return 0; 63 } 64 65 void ini(void) 66 { 67 NUM = 0; 68 for(int i = 1;i <= N;i ++) 69 FATHER[i] = i; 70 } 71 72 int find_father(int n) 73 { 74 if(FATHER[n] == n) 75 return n; 76 return FATHER[n] = find_father(FATHER[n]); 77 } 78 79 void unite(int x,int y) 80 { 81 x = find_father(x); 82 y = find_father(y); 83 84 if(x == y) 85 return ; 86 FATHER[x] = y; 87 } 88 89 bool same(int x,int y) 90 { 91 return find_father(x) == find_father(y); 92 } 93 94 bool comp(const Node & a,const Node & b) 95 { 96 return a.cost < b.cost; 97 } 98 99 void kruskal(void)100 {101 int count = 0;102 103 for(int i = 0;i < NUM;i ++)104 if(!same(G[i].from,G[i].to))105 {106 unite(G[i].from,G[i].to);107 printf("%d %d\n",G[i].from,G[i].to);108 count ++;109 if(count == N - 1)110 break;111 }112 }113 114 double dis(int x_1,int y_1,int x_2,int y_2)115 {116 return pow(x_1 - x_2,2) + pow(y_1 - y_2,2);117 }

 

转载于:https://www.cnblogs.com/xz816111/p/4549270.html

你可能感兴趣的文章
PayPal贝宝集成
查看>>
如何用js获取当前url的参数值
查看>>
怎样阻止超链接跳转
查看>>
zoj Candies 思维
查看>>
在C#中读写INI配置文件(转)
查看>>
IT行业常见职位职业路线图
查看>>
High performance web site
查看>>
Eclipse+Maven命令创建webapp项目<三>
查看>>
[UML]UML系列——类图class的泛化关系
查看>>
史上最全JS表单验证封装类
查看>>
构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(11)-系统日志和异常的处理①...
查看>>
一种松耦合的分层插件系统的设计和实现
查看>>
COM编程之二 接口
查看>>
javascript实现的网页打印
查看>>
web.xml中的contextConfigLocation的作用
查看>>
java 名词解释等
查看>>
Android录音与播放 .
查看>>
SQL with(unlock)与with(readpast) (转)
查看>>
Android 下使用 JSON 实现 HTTP 请求
查看>>
Dubbo架构设计详解
查看>>