Building Roads
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9360   Accepted: 2690

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 (XiYi) on the plane (0 ≤ X≤ 1,000,000; 0 ≤ Y≤ 1,000,000).
Given the preexisting M 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: Xand Y

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

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 1
1 1
3 1
2 3
4 3
1 4

Sample Output

4.00

Source

prim算法:

Memory: 8072K   Time: 188MS
Language: C++   Result: Accepted
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdio>
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1001;
double graph[N][N];
bool visit[N];
int n,M;
typedef struct
{
double x;
double y;
}dian;
dian m[N]; double prim()
{ memset(visit,0,sizeof(visit)); double low[1001];
int pos = 1;
visit[1] = 1;
double result = 0; for(int i = 2; i <= n; i++)
{
low[i] = graph[pos][i];
} for(int i = 0; i < n-1; i++)
{
double Min = INF; for(int j = 1; j <= n; j++)
{
if(!visit[j] && Min > low[j])
{
Min = low[j];
pos = j; }
}
visit[pos] = 1;
result += Min; for(int i = 1; i <= n; i++)
{
if(!visit[i] && low[i] > graph[pos][i])
{
low[i] = graph[pos][i];
}
} }
return result;
} double dis(dian a,dian b)
{
return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));
} int main()
{
// freopen("in.txt","r",stdin);
while(cin>>n>>M)
{
memset(graph,INF,sizeof(graph));
for(int i = 1; i <=n;i++)
{
cin >> m[i].x>>m[i].y;
}
for(int i = 1; i <= n; i++)
{
for(int j = i + 1; j <= n; j++)
{
graph[i][j] = graph[j][i] = dis(m[i],m[j]);
}
}
for(int i = 0; i < M ; i++)
{
int a,b;
cin>>a>>b;
graph[a][b] = graph[b][a] = 0;
}
printf("%.2lf\n",prim());
}
return 0;
}

kruskal算法

Memory: 8604K   Time: 735MS
Language: G++   Result: Accepted
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1001;
const int E = 1000000;
int n, M;
int cent;
int a[N];
int Count = 0; typedef struct
{
int x;
int y;
double vaule;
}dian;
dian m[E]; typedef struct
{
double x, y;
}situation;
situation p[N]; double dis(situation a, situation b)
{
return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y) * (a.y - b.y));
} bool cmp(dian a, dian b)
{
return a.vaule < b.vaule;
} void init()
{
// cent 这里应该初始化到n
for (int i = 1; i <= n; i++)
{
a[i] = i;
}
} int Find(int x)
{
while (x != a[x])
{
x = a[x];
}
return x;
} void Union(int x, int y)
{
// 建议做路径压缩
int fx = Find(x);
int fy = Find(y);
if (fx != fy)
{
a[fx] = fy;
}
} double Kruskal()
{
// init(); 不应该在这里init
sort(m, m + cent, cmp);
double result = 0;
for (int i = 0; i < cent&&Count != n - 1; i++)
{
if (Find(m[i].x) != Find(m[i].y))
{
Union(m[i].x, m[i].y);
result += m[i].vaule;
Count++;
}
}
return result;
} int main()
{
while (cin >> n >> M)
{ for (int i = 1; i <= n; i++)
{
cin >> p[i].x >> p[i].y;
}
cent = 0;
Count = 0;
for (int i = 1; i <= n; i++)
{
for (int j = i + 1; j <= n; j++)
{
m[cent].x = i;
m[cent].y = j;
m[cent++].vaule = dis(p[i], p[j]);
}
}
// init不应该放在Kruskal里面
init();
for (int i = 1; i <= M; i++)
{
int a, b;
cin >> a >> b;
// 这里还是要检查Find a 和 Find b是不是一样,不然Count会错
if (Find(a) != Find(b)) {
Union(a, b);
Count++;
}
} printf("%.2f\n", Kruskal());
}
return 0;
}

注意g++交的时候doubl要用f不用lf

最小生成树模板(poj3625)的更多相关文章

  1. poj 1258 最小生成树 模板

    POJ 最小生成树模板 Kruskal算法 #include<iostream> #include<algorithm> #include<stdio.h> #in ...

  2. POJ-图论-最小生成树模板

    POJ-图论-最小生成树模板 Kruskal算法 1.初始时所有结点属于孤立的集合. 2.按照边权递增顺序遍历所有的边,若遍历到的边两个顶点仍分属不同的集合(该边即为连通这两个集合的边中权值最小的那条 ...

  3. POJ 2031 Building a Space Station 最小生成树模板

    题目大意:在三维坐标中给出n个细胞的x,y,z坐标和半径r.如果两个点相交或相切则不用修路,否则修一条路连接两个细胞的表面,求最小生成树. 题目思路:最小生成树树模板过了,没啥说的 #include& ...

  4. 最小生成树模板【kruskal & prim】

    CDOJ 1966 Kruskal 解法 时间复杂度O(mlogm) m为边数,这里主要是边排序占时间,后面并查集还好 #include <cstdio> #include <cst ...

  5. luogu p3366 最小生成树模板

    倒腾了一个小时  自己也没去看网上的 总算自己能写出来模板了 kruskal //最小生成树 每次找最短的边 #include<bits/stdc++.h> using namespace ...

  6. 最小生成树模板题-----P3366 【模板】最小生成树

    题目描述 如题,给出一个无向图,求出最小生成树,如果该图不连通,则输出orz 输入格式 第一行包含两个整数N.M,表示该图共有N个结点和M条无向边.(N<=5000,M<=200000) ...

  7. Prime算法 与 Kruskal算法求最小生成树模板

    算法原理参考链接 ==> UESTC算法讲堂——最小生成树 关于两种算法的复杂度分析 ==> http://blog.csdn.net/haskei/article/details/531 ...

  8. POJ 1789 Truck History (Kruskal最小生成树) 模板题

    Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for v ...

  9. POJ 1258:Agri-Net Prim最小生成树模板题

    Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 45050   Accepted: 18479 Descri ...

随机推荐

  1. [Luogu 1966] noip13 火柴排队

    [Luogu 1966] noip13 火柴排队 Problem 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度. 现在将每盒中的火柴各自排成一列, 同一列火柴的高度互不相同, 两列火柴之 ...

  2. Struts之 拦截器配置 ( Interceptor )

    struts2体系结构的核心就是拦截器. 以链式执行,对真正要执行的方法(execute())进行拦截.首先执行Action配置的拦截器,在Action和Result执行之后,拦截器再一次执行(与先前 ...

  3. 搭建本地wordpress

    1.首先,下载xampp,安装按默认勾选即可. 2.安装完成后,启动Apache和MySQL这两个服务. 启动后变成绿色,表示启动成功. 3.点击MySQL项的Admin进入数据库后台. 4.点击用户 ...

  4. jsp之认识 servlet (基础、工作原理、容器请求处理)

    Tomcat 的安装: eclipse 需要自行安装tomcat,这是web 项目运行的服务器.如果用的是MyEclipse,里面自带tomcat,方便清除部署垃圾,利于项目运行. Tomcat的安装 ...

  5. PHP中单例模式与工厂模式

    单例模式概念 单例模式是指整个应用中类只有一个对象实例的设计模式. 单例模式的特点 一个类在整个应用中只有一个实例 类必须自行创建这个实例 必须自行向整个系统提供这个实例 php中使用单例模式的原因 ...

  6. 解决sql server死锁

    -- 查询死锁 select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) tableName from sys ...

  7. html5——地理位置

    获取地理位置 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  8. 4星|《JAC写给外贸公司老板的企管书》:善总结爱学习、有业绩的老外贸的经验谈

    作者从事外贸10余年,作出了业绩,也善总结.爱学习.爱分享.本书是作者在外贸行业的从业经验集.有一些战略方面的,比如开发小语种市场,大部分都是战术方面的操作细节(比如如何做营销),应该是非常适合从业者 ...

  9. selenium的三种等待时间

    //隐式等待(20秒以内没哥一段时间就会去找元素,如果没找大也不会报错,过了20s才会报错) //driver.manage().timeouts().implicitlyWait(20, TimeU ...

  10. MyBatis入门3_mapper.xml优化(parameterType简写_NameSpace简写_sql片段_特殊字符处理)_动态SQL

    本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! 优化 1.起别名(一般不用,写全方便查看类出处) 以前 ...