最小生成树模板(poj3625)
|
Building Roads
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). Input * Line 1: Two space-separated integers: N and M 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 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)的更多相关文章
- poj 1258 最小生成树 模板
POJ 最小生成树模板 Kruskal算法 #include<iostream> #include<algorithm> #include<stdio.h> #in ...
- POJ-图论-最小生成树模板
POJ-图论-最小生成树模板 Kruskal算法 1.初始时所有结点属于孤立的集合. 2.按照边权递增顺序遍历所有的边,若遍历到的边两个顶点仍分属不同的集合(该边即为连通这两个集合的边中权值最小的那条 ...
- POJ 2031 Building a Space Station 最小生成树模板
题目大意:在三维坐标中给出n个细胞的x,y,z坐标和半径r.如果两个点相交或相切则不用修路,否则修一条路连接两个细胞的表面,求最小生成树. 题目思路:最小生成树树模板过了,没啥说的 #include& ...
- 最小生成树模板【kruskal & prim】
CDOJ 1966 Kruskal 解法 时间复杂度O(mlogm) m为边数,这里主要是边排序占时间,后面并查集还好 #include <cstdio> #include <cst ...
- luogu p3366 最小生成树模板
倒腾了一个小时 自己也没去看网上的 总算自己能写出来模板了 kruskal //最小生成树 每次找最短的边 #include<bits/stdc++.h> using namespace ...
- 最小生成树模板题-----P3366 【模板】最小生成树
题目描述 如题,给出一个无向图,求出最小生成树,如果该图不连通,则输出orz 输入格式 第一行包含两个整数N.M,表示该图共有N个结点和M条无向边.(N<=5000,M<=200000) ...
- Prime算法 与 Kruskal算法求最小生成树模板
算法原理参考链接 ==> UESTC算法讲堂——最小生成树 关于两种算法的复杂度分析 ==> http://blog.csdn.net/haskei/article/details/531 ...
- POJ 1789 Truck History (Kruskal最小生成树) 模板题
Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for v ...
- POJ 1258:Agri-Net Prim最小生成树模板题
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 45050 Accepted: 18479 Descri ...
随机推荐
- HTML--文本域,支持多行文本输入
当用户需要在表单中输入大段文字时,需要用到文本输入域. 语法: <textarea rows="行数" cols="列数">文本</texta ...
- 安卓5.0新特性之Palette
根据图片来决定标题的颜色和标题栏的背景色,这样视觉上更具有冲击力和新鲜感,而不像统一色调那样呆板. Palette这个类能提取以下突出的颜色: Vibrant(充满活力的) Vibrant dark( ...
- 如何解决error LNK2001(转载)
转自:http://www.cnblogs.com/myzhijie/articles/1658545.html 解决外部符号错误:_main,_WinMain@16,__beginthreadex ...
- 非常无聊——STD::sort VS 基数排序
众所周知,Std::sort()是一个非常快速的排序算法,它基于快排,但又有所修改.一般来说用它就挺快的了,代码一行,时间复杂度O(nlogn)(难道不是大叫一声“老子要排序!!”就排好了么...). ...
- [转]Android自定义Adapter的ListView的思路及代码
本文转自:http://www.jb51.net/article/37236.htm 在开发中,我们经常使用到ListView这个控件.Android的API也提供了许多创建ListView适配器的快 ...
- MyBatis 配置控制台上显示sql语句(log4j.properties 之三)
### direct log messages to stdout ###log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.app ...
- debounce还是throttle(去抖和节流)
debounce 去抖 我的理解很简单,比方说window.onscroll会疯狂触发handler,此时给它一个debounce(handler, delayTime). 就是不管你延时时间内触发了 ...
- AP聚类
基于代表点的聚类算法可以说是聚类算法中"最经典的,最流行的,也是最前沿的". "最经典"是因为K均值是最早出现的聚类算法之一; "最流行"是 ...
- android中TextView内容竖向显示
项目中遇到需要textview内容竖着排的需求,如图所示: 网上那些“教程”并不能达到需要的效果,发现有一个属性可以支持这种效果,android:ems=“*”,这是属性表示一行只显示*个字符. 具体 ...
- C#入门经典 Chapter1&2
Chapter1 1.1 .Net Framework的内容 主要包含一个庞大的代码库,可以在客户端通过OOP来使用这些代码(OOP:Object Oriented Programming面对对象程序 ...