博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj3625 最小生成树 Prim
阅读量:6968 次
发布时间:2019-06-27

本文共 2565 字,大约阅读时间需要 8 分钟。

Description

Farmer John had just acquired several new farms! He wants to connect the farms with roads so that he can travel from any farm to any other farm via a sequence of roads; roads already connect some of the farms.

Each of the N (1 ≤ N ≤ 1,000) farms (conveniently numbered 1..N) is represented by a position (Xi,Yi) on the plane (0 ≤Xi ≤ 1,000,000; 0 ≤Yi ≤ 1,000,000). Given the preexistingM roads (1 ≤ M ≤ 1,000) as pairs of connected farms, help Farmer John determine the smallest length of additional roads he must build to connect all his farms.

Input

* Line 1: Two space-separated integers: N and M

* Lines 2..N+1: Two space-separated integers: Xi andYi
* Lines N+2..N+M+2: Two space-separated integers: i and j, indicating that there is already a road connecting the farmi and farmj.

Output

* Line 1: Smallest length of additional roads required to connect all farms, printed without rounding to two decimal places. Be sure to calculate distances as 64-bit floating point numbers.

Sample Input

4 11 13 12 34 31 4

Sample Output

4.00

 

 

坐标类的最短路;

基础题。同时再总结一下Prim算法。

选取一个根节点,每次选取与树距离最短的一条路加入。

此题利用邻接矩阵。

 

在这总结一下最短路Prim与Kruskal的区别:

Prim适合于邻接矩阵,而Kruskal更适合于邻接表。

同时得出一个结论,Prim算法比较适合求稠密图的最小生成树,Kruskal算法比较适合求稀疏图的最小生成树。

 

在做题时要注意如何选择Prim与Kruskal。

当点少边多时,如1000个点500000条边时,这种变态数据,Prim就要开1000*1000的数组;

而Kruskal只需开一个500000数组。

当点多边少时如500000个点,1000条边,这主要是为了限制内存。Prim就绝对没戏,Kruskal就笑了。

同时,在满足空间的情况下,一般数据的Prim的时间是能完全碾压Kruskal的,大概在20倍左右。此时会有,在变态空间数据选择Kruskal时会超时,而Prim优化内存这不能搞,这时就得Kruskal优化时间了。即在普通Kruskal搜索选择最小边时,做文章,由于每次都得重复搜素最小边,不如一开始将其排序,改进后变为Kruskal+qsort,这时时间就能与Prim相差无几。事实上我们用kruskal一般都是先排序的。所以空间大的时候,和其他情况都可以用到kruskal。

 

#include 
#include
#include
#include
using namespace std;const int maxn=1002;const double maxm=2000000;bool visit[maxn];int n;double sum;double x[maxn],y[maxn],lowcost[maxn],weight[maxn][maxn];double dist(double x1,double y1,double x2,double y2){ return sqrt(pow(x2-x1,2)+pow(y2-y1,2));}void Prim(int u){ int i,j,k; double min; for(i=1;i<=n;i++) if(i!=u) lowcost[i]=weight[1][i]; //lowcost取1节点到其他节点的距离。 visit[1]=true; for(i=1;i
weight[k][j] && !visit[j])//能是lowcost始终为树外最小的n条路。 lowcost[j]=weight[k][j]; }}int main(){ int i,j,m,a,b; while(scanf("%d%d",&n,&m)!=EOF) { for(i=1;i<=n;i++) //农场从1开始标号 scanf("%lf%lf",&x[i],&y[i]); for(i=1;i<=n;i++) for(j=1;j<=n;j++) weight[i][j]=maxm; for(i=1;i

 

 

转载于:https://www.cnblogs.com/amourjun/archive/2012/12/04/5134233.html

你可能感兴趣的文章
Memcached的Web管理工具MemAdmin(待实践)
查看>>
嵌入式学习难点 嵌入式软件学习
查看>>
11204 ASM 在线存储迁移。
查看>>
eclipse不会自动编译的问题解决
查看>>
linux netstat命令
查看>>
淘宝卖家遭恶退诈骗 阿里一年来协助警方抓获103人
查看>>
拥2180亿美元收入,苹果成全球最大IT企业
查看>>
数据库连接池的工作原理
查看>>
网络抓包工具wireshark and tcpdump 及其实现基于的libpcap
查看>>
市值410亿美元!VR内容在5年后将成下一座金矿
查看>>
easyui的combobox根据后台数据实现自动输入提示功能
查看>>
ASP.NET MVC WEB API必知必会知识点总结
查看>>
Test2 unit6
查看>>
sql注入<二>
查看>>
26、OSPF配置实验之不规则区域虚链路
查看>>
[C++再学习系列] 引用和指针
查看>>
未能加载文件或程序集“********”或它的某一个依赖项。试图加载格式不正确的程序。...
查看>>
bootstrap4-图像
查看>>
Centos7 MariaDB10.1.22编译安装
查看>>
路由器配置基础(中)
查看>>