天天看點

H - Highways (最小生成樹Kruskal算法和Prim算法)

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
           

最小生成樹輸出路徑問題,其他的和前面的D - Constructing Roads 相似

Prim算法

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#define INF 0x3f3f3f
using namespace std;
int n;
double G[1005][1005];
int q;
void Prim()
{
	int path[1005],visited[1005];
	double dis[1005];
	memset(visited,0,sizeof(visited));
	for(int i=2;i<=n;i++){
		dis[i]=G[1][i];
		path[i]=1;
	}
	visited[1]=1;
	for(int i = 1;i<=n-1;i++){
		int minid;
		double minn=INF;
		for(int j = 1;j<=n;j++)
		    if(minn>dis[j]&&!visited[j])
		        minn=dis[minid=j];
		if(G[path[minid]][minid]!=0) printf("%d %d\n",path[minid],minid);
		visited[minid]=1;
		dis[minid]=0; 
		for(int j = 1;j<=n;j++){
			if(dis[j]>dis[minid]+G[minid][j]&&!visited[j]){
				dis[j]=dis[minid]+G[minid][j];
				path[j]=minid;
			}
		}
	}
}
int main()
{
	scanf("%d",&n);
	int x[1005],y[1005]; 
	for(int i = 1;i<=n;i++)
		for(int j = 1;j<=n;j++)
		    G[i][j]=G[j][i]=(i==j)?0:INF;
	for(int i = 1;i<=n;i++)
	    scanf("%d%d",&x[i],&y[i]);
	for(int i = 1;i<=n;i++)
		for(int j = i+1;j<=n;j++)
			G[i][j]=G[j][i]=sqrt( (x[i]-x[j])*(x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j]) );
	scanf("%d",&q);
	while(q--)
	{
	    int a,b;
	    scanf("%d%d",&a,&b);
	    G[a][b]=G[b][a]=0;
	}
	Prim();
	return 0;
 } 
           

Kruskal算法(要有一部分的優化,不然會逾時)

#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
#define INF 0x3f3f3f
using namespace std;
struct edge{
	int from,to;
	double cost;
}es[1000005];
int q,n,cnt,p[1000005],flag;
bool cmp(edge x,edge y)
{
	return x.cost<y.cost;
}
int find(int x)
{
	return x==p[x]?x:p[x]=find(p[x]);
}
bool check(int a,int b) 
{
	int x=find(a);
	int y=find(b);
	if(x==y) return false;
	p[y]=x; 
	return true;
}
void Kruskal()
{
	sort(es,es+cnt,cmp);
	for(int i = 0;i<cnt;i++){
		edge &e=es[i];
		if(check(e.from,e.to)){
			printf("%d %d\n",e.from,e.to);
			flag++;
		}
		if(flag+1==n) break;//構成最小生成樹就跳過。	
	}
}
int main()
{
	double x[1005],y[1005];
	flag=0;
	scanf("%d",&n);
	for(int i = 1;i<=n;i++) p[i]=i;
	for(int i = 1;i<=n;i++)
		scanf("%lf%lf",&x[i],&y[i]); 
	scanf("%d",&q);
	while(q--)
	{
		int a,b;
	    scanf("%d%d",&a,&b);
	    if(check(a,b))
		   flag++;
	} 
	for(int i =1;i<=n;i++){
		for(int j = i+1;j<=n;j++){
			if(find(i)==find(j)) continue;//如果在同一連通分量裡面就跳過
			es[cnt].from=i;
			es[cnt].to=j;
			double dis=sqrt( (x[i]-x[j])*(x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j]) );
			es[cnt++].cost=dis;
		}
	}
	Kruskal();
	return 0;
}
           

繼續閱讀