最小生成树模板(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 ...
随机推荐
- knockjs
用VS2012建立Web站点有个新惊喜,默认加了KnockoutJS这个Javascript的MVVM模式的实现库,方便Web前端的开发 官方站点 √主页: http://www.knockoutj ...
- 【BZOJ3527】[ZJOI2014] 力(FFT)
题目: BZOJ3527 分析: FFT应用第一题-- 首先很明显能把\(F_j\)约掉,变成: \[E_j=\sum _{i<j} \frac{q_i}{(i-j)^2}-\sum_{i> ...
- flask 初始
一.flask安装 这里提供两种安装方式: 第一种: pip3 install flask 第二种: pip3 install -i https://pypi.douban.com/simple/ f ...
- ACM_情人节
情人节 Time Limit: 2000/1000ms (Java/Others) Problem Description: 某发每天都在各大群水啊水,然后认识了很多崇拜他的妹子,毕竟是数学专业.这不 ...
- 题解报告:hdu 2647 Reward(拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 Problem Description Dandelion's uncle is a boss ...
- java.net.URISyntaxException: Illegal character in query
java使用httpclient爬取一个网站的时候,请求:String url3="http://sh.58.com/ershoufang/33562546149042x.shtml?amp ...
- Windows10开启热点
1.以网线的连接方式,已经连接. 2.打开CMD 3. 开启热点 3.1设置热点名称和密码 netsh wlan set hostednetwork mode=allow ssid=name key= ...
- 【GAN学习笔记】对抗式生成网络入门
今天观看学习了一下台大李宏毅所讲授的 <Introduction of Generative Adversarial Network (GAN)>,对GAN有了一个初步的了解. GAN的基 ...
- Android常用的Dialog对话框用法
Android的版本有很多通常开发的时候对话框大多数使用自定义或是 Google提供的V4, V7 兼容包来开发保持各个版本的对话框样式统一,所以这里使用的是V7 包里的AlertDialog. im ...
- jQuery——事件操作
事件绑定 1.简单事件绑定 $("button").click(function () {})//可重复绑定,不会被层叠 2.bind():不推荐使用 $("button ...