POJ1751 Highways

Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %lld & %llu

Submit Status

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

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

Source

Northeastern Europe 1999

有n个点,其中一些点已经连通,求最小生成树。

因为是小范围稠密图,所以用邻接矩阵就可以。把已经连通的点之间的距离设为0,之后跑Prim,如果新选用的边长度不为0就输出。

 1 /*by SilverN*/
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 #include<vector>
 8 using namespace std;
 9 const int mxn=1810;
10 int read(){
11     int x=0,f=1;char ch=getchar();
12     while(ch<‘0‘ || ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
13     while(ch>=‘0‘ && ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
14     return x*f;
15 }
16 struct point{
17     int x,y;
18 }p[mxn];
19 double dist(int a,int b){
20     return sqrt((double)(p[a].x-p[b].x)*(p[a].x-p[b].x)+(double)(p[a].y-p[b].y)*(p[a].y-p[b].y));
21 }
22 double mp[mxn][mxn];
23 int ans[mxn];
24 int n,m;
25 int cm[mxn];
26 double dis[mxn];
27 bool vis[mxn];
28 void Prim(){
29     int i,j;
30     for(i=1;i<=n;i++){
31         dis[i]=mp[1][i];
32         cm[i]=1;//??????????
33     }
34     dis[1]=0;vis[1]=1;
35     for(i=1;i<=n;i++){
36         double mini=1e9;int pos=0;
37         for(j=1;j<=n;j++){
38             if(!vis[j] && dis[j]<mini){
39                 mini=dis[j];
40                 pos=j;
41             }
42         }
43         if(!pos)break;
44         if(mp[cm[pos]][pos])printf("%d %d\n",cm[pos],pos);
45         vis[pos]=1;
46         for(j=1;j<=n;j++){
47             if(!vis[j] && dis[j]>mp[pos][j]){
48                 dis[j]=mp[pos][j];
49                 cm[j]=pos;
50             }
51         }
52     }
53     return;
54 }
55 int main(){
56     int i,j;
57     n=read();
58     for(i=1;i<=n;i++){
59         p[i].x=read();p[i].y=read();
60     }
61     for(i=1;i<n;i++)
62         for(j=i+1;j<=n;j++)
63             mp[i][j]=mp[j][i]=dist(i,j);
64     m=read();
65     int u,v;
66     for(i=1;i<=m;++i){
67         u=read();v=read();
68         mp[u][v]=mp[v][u]=0;
69     }
70     Prim();
71     return 0;
72 }
/*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
using namespacestd;
const int mxn=1810;
int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<‘0‘ || ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘ && ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
structpoint{
	int x,y;
}p[mxn];
double dist(int a,int b){
	return sqrt((double)(p[a].x-p[b].x)*(p[a].x-p[b].x)+(double)(p[a].y-p[b].y)*(p[a].y-p[b].y));
}
double mp[mxn][mxn];
int ans[mxn];
int n,m;
int cm[mxn];
double dis[mxn];
bool vis[mxn];
void Prim(){
	int i,j;
	for(i=1;i<=n;i++){
		dis[i]=mp[1][i];
		cm[i]=1;//??????????
	}
	dis[1]=0;vis[1]=1;
	for(i=1;i<=n;i++){
		double mini=1e9;int pos=0;
		for(j=1;j<=n;j++){
			if(!vis[j] && dis[j]<mini){
				mini=dis[j];
				pos=j;
			}
		}
		if(!pos)break;
		if(mp[cm[pos]][pos])printf("%d %d\n",cm[pos],pos);
		vis[pos]=1;
		for(j=1;j<=n;j++){
			if(!vis[j] && dis[j]>mp[pos][j]){
				dis[j]=mp[pos][j];
				cm[j]=pos;
			}
		}
	}
	return;
}
int main(){
	int i,j;
	n=read();
	for(i=1;i<=n;i++){
		p[i].x=read();p[i].y=read();
	}
	for(i=1;i<n;i++)
		for(j=i+1;j<=n;j++)
			mp[i][j]=mp[j][i]=dist(i,j);
	m=read();
	int u,v;
	for(i=1;i<=m;++i){
		u=read();v=read();
		mp[u][v]=mp[v][u]=0;
	}
	Prim();
	return 0;
}
时间: 2024-10-23 06:38:00

POJ1751 Highways的相关文章

最小生成树练习3(普里姆算法Prim)

风萧萧兮易水寒,壮士要去敲代码.本女子开学后再敲了.. poj1258 Agri-Net(最小生成树)水题. 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int inf=0x3f3f3f3f; 6 const int N=101; 7 int n,m; 8 int g[N][N],low[N]; 9 void prim(int u0

「题解」kuangbin 最小生成树

POJ-1251 Jungle Roads (水题,%c) POJ-1287 Networking (水) POJ-2031 Building a Space Station (%f浮点数尴尬精度,两球间距离) POJ-2421 Constructing Roads (一些边已建好,简单处理一下) ZOJ-1586 QS Network (处理一下边权) HDU-1233 还是畅通工程 (水) HDU-1875 畅通工程再续 (浮点数,条件连边) HDU-1301 Jungle Roads (重

Highways POJ-1751 最小生成树 Prim算法

Highways POJ-1751 最小生成树 Prim算法 题意 有一个N个城市M条路的无向图,给你N个城市的坐标,然后现在该无向图已经有M条边了,问你还需要添加总长为多少的边能使得该无向图连通.输出需要添加边的两端点编号即可. 解题思路 这个可以使用最短路里面的Prim算法来实现,对于已经连接的城市,处理方式是令这两个城市之间的距离等于0即可. prim算法可以实现我们具体的路径输出,Kruskal算法暂时还不大会. 代码实现 #include<cstdio> #include<cs

C - Highways poj1751最小生成树

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 importa

[spoj104][Highways] (生成树计数+矩阵树定理+高斯消元)

In some countries building highways takes a lot of time... Maybe that's because there are many possiblities to construct a network of highways and engineers can't make up their minds which one to choose. Suppose we have a list of cities that can be c

UVA - 1393 Highways

Description Hackerland is a happy democratic country with m×n cities, arranged in a rectangular m by n grid and connected by m roads in the east-west direction and n roads in the north-south direction. By public demand, this orthogonal road system is

poj 2485 Highways

链接:poj 2485 题意:输入n个城镇相互之间的距离,输出将n个城镇连通费用最小的方案中修的最长的路的长度 这个也是最小生成树的题,只不过要求的不是最小价值,而是最小生成树中的最大权值,只需要加个判断 比较最小生成树每条边的大小就行 #include<cstdio> #include<algorithm> using namespace std; int f[510],n,m; struct stu { int a,b,c; }t[20100]; int cmp(struct

POJ 2485 Highways 最小生成树 (Kruskal)

Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They're planning to build some highways so that i

POJ 2485 Highways (求最小生成树中最大的边)

Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They're planning to build some highways so that i