您的位置:首页 > 其它

poj 1751 prim输出路径

2016-07-29 17:35 260 查看
Highways

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 13124 Accepted: 3786 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


题意:给出 m 个城市的坐标

然后给出 n 条已知的路径

求另外几条需要连接的路径,使得路径之和最短,并且输出路径

题解:

根据prim这个算法的贪心法则进行寻找最短路径



这里把点转换为距离,但是题目给出的那些路径的长度全部初始化为 0 



1· 先遍历第一个点,并且标记已经走过

2  然后从这个点出发,寻找最短的一截路

3  若是这一截路是已知的(已知的路径长度我们将它初始化为 0 ),就标记走过就行了

4  若不是已知的,就输出就行了

5  然后遍历 点 p (就是第二步中找到的那一截路的终点)的周围的路径,寻找最短的一截

6  然后回到第三步,直到遍历完所有的

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;

#define INF 0x3f3f3f3f

int n,m;
struct node
{
int x,y;
}a[1000];
double map[800][800];
double weight[1000];
int flag[1000];

double calc(int i,int j)
{
double x=a[i].x-a[j].x;
double y=a[i].y-a[j].y;
return sqrt(x*x+y*y);
}

void init()
{
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d%d",&a[i].x,&a[i].y);
for(int j=1;j<i;j++)
map[i][j]=map[j][i]=calc(i,j);
}

scanf("%d",&m);
int tx,ty;
for(int i=1;i<=m;i++){
scanf("%d%d",&tx,&ty);
map[ty][tx]=map[tx][ty]=0;
}
memset(weight,INF,sizeof(weight));
}

void prim()
{
int pos=1;
double min_weight;
int p;
//--------------表示遍历了第一个点-------------------//
for(int i=1;i<=n;i++){
weight[i]=map[pos][i];
flag[i]=pos;//记录它的父亲节点
}
flag[pos]=-1;

//----------------寻找最短的一截路---------------//
for(int i=1;i<n;i++){
min_weight=INF;
p=-1;

for(int j=1;j<=n;j++){
if(flag[j]!=-1&&min_weight>weight[j]){
min_weight=weight[j];
p=j;
}
}

//-------------判断是否有短路到达---------------//
if(p!=-1){
if(min_weight==0)
flag[p]=-1;
else{
printf("%d %d\n",flag[p],p);
flag[p]=-1;
}
}
//----------------更新一截路---------------------//
for(int j=1;j<=n;j++)
if(flag[j]!=-1&&weight[j]>map[p][j]){
weight[j]=map[p][j];
flag[j]=p;
}
}
}

int main()
{
freopen("in.txt","r",stdin);
init();
prim();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: