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

题目链接:http://poj.org/problem?id=1751
Highways
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 14785 Accepted: 4265 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 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
题意:现在平面上有n个点的坐标。给出m条边,让你继续连接两个点使所有点连起来,使得新连的边长度最短,输出你连接的两点(输出顺序可以乱)
思路:这题用最小生成树的话,Prim算法要先弄懂。

MST(Minimum Spanning Tree,最小生成树)问题有两种通用的解法,Prim算法就是其中之一,它是从点的方面考虑构建一颗MST,大致思想是:设图G顶点集合为U,首先任意选择图G中的一点作为起始点a,将该点加入集合V,再从集合U-V中找到另一点b使得点b到V中任意一点的权值最小,此时将b点也加入集合V;以此类推,现在的集合V={a,b},再从集合U-V中找到另一点c使得点c到V中任意一点的权值最小,此时将c点加入集合V,直至所有顶点全部被加入V,此时就构建出了一颗MST。因为有N个顶点,所以该MST就有N-1条边,每一次向集合V中加入一个点,就意味着找到一条MST的边。


用图示和代码说明:

初始状态:


设置2个数据结构

lowcost[i]:表示以i为终点的边的最小权值,当lowcost[i]=0说明以i为终点的边的最小权值=0,也就是表示i点加入了MST

mst[i]:表示对应lowcost[i]的起点,即说明边<mst[i],i>是MST的一条边,当mst[i]=0表示起点i加入MST


我们假设V1是起始点,进行初始化(*代表无限大,即无通路):


lowcost[2]=6lowcost[3]=1lowcost[4]=5lowcost[5]=*,lowcost[6]=*

mst[2]=1mst[3]=1,mst[4]=1mst[5]=1,mst[6]=1(所有点默认起点是V1)


明显看出,以V3为终点的边的权值最小=1,所以边<mst[3],3>=1加入MST


此时,因为点V3的加入,需要更新lowcost数组和mst数组:

lowcost[2]=5lowcost[3]=0lowcost[4]=5lowcost[5]=6,lowcost[6]=4

mst[2]=3mst[3]=0,mst[4]=1mst[5]=3,mst[6]=3


明显看出,以V6为终点的边的权值最小=4,所以边<mst[6],6>=4加入MST


此时,因为点V6的加入,需要更新lowcost数组和mst数组:

lowcost[2]=5lowcost[3]=0lowcost[4]=2lowcost[5]=6lowcost[6]=0

mst[2]=3mst[3]=0,mst[4]=6mst[5]=3,mst[6]=0


明显看出,以V4为终点的边的权值最小=2,所以边<mst[4],4>=4加入MST


此时,因为点V4的加入,需要更新lowcost数组和mst数组:

lowcost[2]=5,lowcost[3]=0,lowcost[4]=0,lowcost[5]=6lowcost[6]=0

mst[2]=3,mst[3]=0,mst[4]=0mst[5]=3mst[6]=0


明显看出,以V2为终点的边的权值最小=5,所以边<mst[2],2>=5加入MST


此时,因为点V2的加入,需要更新lowcost数组和mst数组:

lowcost[2]=0,lowcost[3]=0,lowcost[4]=0,lowcost[5]=3,lowcost[6]=0

mst[2]=0,mst[3]=0,mst[4]=0mst[5]=2mst[6]=0


很明显,以V5为终点的边的权值最小=3,所以边<mst[5],5>=3加入MST

lowcost[2]=0,lowcost[3]=0lowcost[4]=0,lowcost[5]=0lowcost[6]=0

mst[2]=0,mst[3]=0mst[4]=0,mst[5]=0mst[6]=0


至此,MST构建成功,如图所示:


思路:最坏的情况需要遍历图中的每一条边,很明显的稠密图,优先选用普利姆算法。 
建图:根据坐标建立无向图,权值设为距离的平方即可,这样可以避免sqrt后权值变为double型,避免精度损失。对于已联通的两点,更新这两点的权值为0即可。 

附上AC代码:(这个模板挺重要的)
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<math.h>
using namespace std;
//普利姆,注意建图技巧
const int maxn=751;
const int INF=0x3f3f3f3f;
int map[maxn][maxn];//两点间距离
int dis[maxn];//dis[i]表示i点(未连接的点)到(已连接的点)的最短距离
int vis[maxn];//标记,1为已选过的点
int Edge[maxn];//Edge[i]的值为k,表示i到k是一条生成树内的边
struct node
{
    int x;
    int y;
} Point[maxn]; //第i个点的坐标
int N;//点的数量
int M;//更新边的数量
void init()
{
    scanf("%d",&N);
    for(int i=1; i<=N; i++)//建图
    {
        scanf("%d%d",&Point[i].x,&Point[i].y);
        for(int j=1; j<i; j++)//为什么这里不取sqrt,因为完全没必要
            map[i][j]=map[j][i]=(Point[i].x-Point[j].x)*(Point[i].x-Point[j].x)+(Point[i].y-Point[j].y)*(Point[i].y-Point[j].y);
        map[i][i]=INF;//自己不可能到自己
    }
    scanf("%d",&M);
    int x,y;
    while(M--)//更新图
    {
        scanf("%d%d",&x,&y);
        map[x][y]=map[y][x]=0;
    }
    memset(vis,0,sizeof(vis));
    vis[1]=1;
    for(int i=1; i<=N; i++)
    {
        dis[i]=map[i][1];
        Edge[i]=1;//初始化为存储i到1的边
    }
}
void Prim()
{
    for(int i=1; i<N; i++)
    {
        int minn=INF;
        int point_minn;
        for(int j=1; j<=N; j++)
            if(vis[j]==0&&minn>dis[j])
            {
                minn=dis[j];
                point_minn=j;
            }
            //选出最优点;
        vis[point_minn]=1;
        for(int k=1; k<=N; k++)//用最优点更新其他点。
            if(vis[k]==0&&dis[k]>map[point_minn][k])
            {
                Edge[k]=point_minn;//这里是输出方式的技巧
                dis[k]=map[point_minn][k];
            }
        if(map[Edge[point_minn]][point_minn])//判断是否为已知的m条边
            printf("%d %d\n",Edge[point_minn],point_minn);
    }
}
int main()
{
    init();
    Prim();
    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值