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. 题解报告:poj 3233 Matrix Power Series(矩阵快速幂)

    题目链接:http://poj.org/problem?id=3233 Description Given a n × n matrix A and a positive integer k, fin ...

  2. 开始玩qt,使用代码修改设计模式生成的菜单

    之前制作菜单时,不是纯代码便是用设计模式 直接图形化完成. 今天我就是想用代码修改已经存在的菜单项,如果是用代码生成的可以直接调用指针完成: 但通过设计模式完成的没有暴露指针给我,至少我没发现. 在几 ...

  3. eclipse 安装svn插件记录

    每个人都有自己喜欢的和习惯的一套开发环境,其中对于喜欢用eclipse的同学来说.subclipse插件可以说是必不可少的插件了. 他的安装有两种方法,一种是在线安装.然而这种安装实在是太慢了,无法忍 ...

  4. Python--10、进程知识补充

    守护进程 基于进程启动的子进程,会和主进程一起结束.主进程结束的依据是程序的代码执行完毕. #创建守护进程p=Process(task) p.daemon = True p.start() 子进程需要 ...

  5. brew update失败提示:/System/Library/Frameworks/Ruby.framework/的解决方法

    本文由@ray 出品,转载请注明出处.  文章链接:http://www.cnblogs.com/wolfray/p/8040701.html 想用brew安装wget,但是提示失败,然后想先 bre ...

  6. STL之set篇

    insert为插入.set_intersection求交集,set_union求并集,是属于algorithm里的函数. 例题有 PAT甲级1063 #include<iostream> ...

  7. HTTP05--HTML常用知识

    一.URL地址含义 需要搞清URL和URI的差别,以及QueryString的含义. 二.GET和POST的区别 详细介绍可参考文章:http://zengrong.net/post/1802.htm ...

  8. JS高级——词法作用域

    作用域 1.js中没有块级作用域 2.如果有块级作用域,那么下面代码将会是undefined undefined <script> for (var i = 0; i < 10; i ...

  9. Redis系列(一)StackExchange.Redis的使用

    Redis系列(一)StackExchange.Redis的使用 一.DLL安装 用NuGet搜索StackExchange.Redis,然后下载就可以. ConnectionMultiplexer对 ...

  10. 170925_2 Python socket 创建UDP的服务器端和客户端

    [python版本]3.6 UDP服务器端: from socket import * from time import ctime host = '' port = 21567 buf_size = ...