POJ1751 Highways(最小生成树,Kruskal模板题)
来源:互联网 发布:淘宝运费险怎么退订 编辑:程序博客网 时间:2024/04/29 15:28
题目:
Highways
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 14384 Accepted: 4152 Special Judge
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
91 50 0 3 24 55 10 45 21 25 331 39 71 2
Sample Output
1 63 74 95 78 3
Source
Northeastern Europe 1999
[Submit] [Go Back] [Status] [Discuss]
思路:建边的时候把任何一个点到任何一点的距离都建成边,如果有两个放点已经连通,那么就加入并查集使之混合,然后用一次Kruskal把要建的路打印出来就行
代码:
#include <stdio.h>#include <string.h>#include <string>#include <iostream>#include <stack>#include <cmath>#include <queue>#include <vector>#include <algorithm>#define mem(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3f#define N 1000+10#define M 1000000+10#define LL long longusing namespace std;int pre[N];int n,m,len,cnt;struct node{ int u,v; double w;} G[M];struct zzz{ int x,y;} zz[M];bool cmp(node x,node y){ return x.w<y.w;}void init(){ for(int i=1; i<=n; i++) pre[i]=i;}int find(int x){ if(x==pre[x]) return x; else { pre[x]=find(pre[x]); return pre[x]; }}int mix(int x,int y){ int fx=find(x); int fy=find(y); if(fx!=fy) { pre[fy]=fx; return 1; } return 0;}double dist(int x1,int y1,int x2,int y2){ return sqrt((double)((x1-x2)*(x1-x2))+(double)((y1-y2)*(y1-y2)));}void Kruskal(){ sort(G,G+len,cmp); cnt=0; for(int i=0; i<len; i++) { if(mix(G[i].u,G[i].v)) { zz[cnt].x=G[i].u; zz[cnt++].y=G[i].v; } if(cnt==n-1) break; }}int main(){ len=0; scanf("%d",&n); init(); for(int i=1; i<=n; i++) { scanf("%d%d",&zz[i].x,&zz[i].y); for(int j=1; j<i; j++) { G[len].u=i; G[len].v=j; G[len++].w=dist(zz[i].x,zz[i].y,zz[j].x,zz[j].y); } } int a,b; scanf("%d",&m); for(int i=0; i<m; i++) { scanf("%d%d",&a,&b); mix(a,b);//把已经连接的路连起来 } Kruskal(); for(int i=0; i<cnt; i++) printf("%d %d\n",zz[i].x,zz[i].y); return 0;}
0 0
- POJ1751 Highways(最小生成树,Kruskal模板题)
- poj1751 Highways(最小生成树)
- poj1751 Highways 最小生成树(小组赛A)
- poj1751 highways 最小生成树记录路径
- POJ1751 Highways(最小生成树)(prim)
- poj1751-最小生成树
- POJ1751->最小生成树
- Sicily 1090 Highways(Kruskal最小生成树)
- poj 2485Highways(最小生成树 Kruskal)
- poj2485 Highways 最小生成树,kruskal算法
- poj2485 Highways(kruskal求最小生成树)
- POJ 1751 Highways (Kruskal 最小生成树)
- 1007. Highways(kruskal最小生成树)
- poj1751——Highways(部分确定的最小生成树)
- 最小生成树-kruskal模板
- 最小生成树kruskal模板
- 【模板】最小生成树Kruskal
- 【模板】Kruskal 最小生成树
- 使用StrongSwan配置IPSec
- ASCII码对照表
- mysql知识积累
- utf-8编码举例
- 配置_LogFilter Druid
- POJ1751 Highways(最小生成树,Kruskal模板题)
- java -页面验证码生成
- Requests 安装
- linux如何判断是否插了U盘
- Java多线程的同步机制(synchronized)
- php将数组的下标从新定义自增 array_values
- Android完全退出应用程序的四种方法
- log4j.properties 详解与配置步骤
- AngularJS 中的Promise --- $q服务详解