POJ 图论 Highways 修高铁 prim算法 中等题

Highways

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 22759   Accepted: 6704   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.

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.

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

扫描二维码关注公众号,回复: 5624983 查看本文章

题意解读:

给一个数n,是城市的总数,然后给出n个城市的坐标,再给出一个数m,是已经修好的高铁数,求:使这些城市连通的最小代价(给出路线,除去已建好的路线)

个人认为的难点:(如果是大佬,请忽略)

1.如何存储产生最小代价的高铁路线

2.已建好的高铁路线,如何处理?

第一个问题:

很容易想到,用一个节点数组,但是如何处理某一条线段的头呢?

这就需要一个前置数组,存放当前下标所在高铁线段的起始节点。

具体看代码

第二个问题:

让邻接矩阵相应的值为0,此外,还应在Prim算法中,在找到最短的dis【i】,还需要判断一下min!=0

#include<iostream>
#include<cstdio>
#include<cstring>
#include<math.h>
#include<algorithm>

using namespace std;
#define range 755
#define inf 0x3f3f3f3f

struct node 
{
	int x,y;
}s[range];

double map[range][range];
bool vis[range];
double dis[range];
double Distance(node a,node b)
{
	return sqrt((double)(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
void Prim(int n,int m)
{
	int k=0;
	node a[range];
	int pre[range];
	int pos=0;
	vis[pos]=true;
    int i,j;
//   
    for(i=0;i<n;i++)
    {
    	dis[i]=map[pos][i];
    	pre[i]=pos;
	}
	for(i=0;i<n-1;i++)
	{
		
		double min=inf;
		for(j=0;j<n;j++)
		{
			if(!vis[j]&&min>dis[j])
			{
				min=dis[j];
				pos=j;
			}
		}
		if(min!=0)
		{
			a[k].x=pre[pos];
			a[k].y=pos;
			//cout<<a[k].x<<"  "<<a[k].y<<endl;
		    k++;
		}
		
		vis[pos]=true;
		for(j=0;j<n;j++)
		{
			if(!vis[j]&&dis[j]>map[pos][j])
			{
				dis[j]=map[pos][j];
				pre[j]=pos;
			}
			
		}
	}
	

	for(i=0;i<k;i++)
	printf("%d %d\n",a[i].x+1,a[i].y+1);
	printf("\n");
	
	
}
int main()
{
	int n,m,i,j;
	int a,b;
	scanf("%d",&n);	
	for(i=0;i<n;i++)
	{
		scanf("%d %d",&s[i].x,&s[i].y);
		
	}
	memset(map,inf,sizeof(map));
	memset(vis,false,sizeof(vis));
	memset(dis,inf,sizeof(dis));
	
	
	for(i=0;i<n;i++)
	{
		for(j=i+1;j<n;j++)
		{
			map[i][j]=map[j][i]=Distance(s[i],s[j]);
		}
	}
	scanf("%d",&m);
	while(m--)
	{
		scanf("%d %d",&a,&b);
		map[a-1][b-1]=map[b-1][a-1]=0;
	}
	Prim(n,m);
	
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/qq_41325698/article/details/88382517