POJ 2560 Freckles Prime问题解决算法
这个问题正在寻求最小生成树。
给定节点的坐标,那么我们需要根据各个点之间的这些坐标来计算距离。
除了这是标准的Prime算法的,能源利用Prime基本上,你可以使用Kruskal。
经典的算法必须填写,熟练度。否则它是非常困难的利用。
并且经典的算法之所以为经典。原因之中的一个是没那么easy自己凭空想象出来的,所以要熟练。
#include <stdio.h>
#include <string.h>
#include <queue>
#include <float.h>
#include <algorithm>
#include <math.h>
using namespace std; struct Point
{
float x, y;
};
const int MAX_N = 101;
Point P[MAX_N];
bool vis[MAX_N];
float dist[MAX_N];
float minDist[MAX_N]; float calDist(Point &a, Point &b)
{
float x = a.x - b.x;
float y = a.y - b.y;
return sqrtf(x*x + y*y);
} void Prime(int n)
{
memset(vis, 0, sizeof(bool) * (n+1));
vis[1] = true;
dist[1] = 0;
for (int i = 2; i <= n; i++)
{
dist[i] = calDist(P[1], P[i]);
} for (int i = 1; i < n; i++)
{
float minD = FLT_MAX;
int id = 0;
for (int j = 2; j <= n; j++)
{
if (!vis[j] && dist[j] < minD)
{
minD = dist[j];
id = j;
}
}
vis[id] = true;
minDist[i] = minD;
for (int j = 2; j <= n; j++)
{
if (!vis[j])
{
float d = calDist(P[id], P[j]);
if (d < dist[j]) dist[j] = d;
}
}
}
} int main()
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%f %f", &P[i].x, &P[i].y);
}
Prime(n);
float ans = 0.f;
for (int j = 1; j < n; j++)
{
ans += minDist[j];
}
printf("%.2f\n", ans);
return 0;
}
版权声明:本文博客原创文章。博客,未经同意,不得转载。
POJ 2560 Freckles Prime问题解决算法的更多相关文章
- poj 2560 Freckles
题目连接 http://poj.org/problem?id=2560 Freckles Description In an episode of the Dick Van Dyke show, li ...
- 【POJ】1811 Prime Test
http://poj.org/problem?id=1811 题意:求n最小素因子.(n<=2^54) #include <cstdio> #include <cstring& ...
- POJ 1135.Domino Effect Dijkastra算法
Domino Effect Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10325 Accepted: 2560 De ...
- POJ 3259 Wormholes (Bellman_ford算法)
题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- POJ 1861 Network (模版kruskal算法)
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: Accepted: Special Judge Descripti ...
- poj 3662 Telephone Lines spfa算法灵活运用
意甲冠军: 到n节点无向图,它要求从一个线1至n路径.你可以让他们在k无条,的最大值.如今要求花费的最小值. 思路: 这道题能够首先想到二分枚举路径上的最大值,我认为用spfa更简洁一些.spfa的本 ...
- poj 2449 k短路+A*算法
http://poj.org/problem?id=2449 K短路的定义: 1.如果起点终点相同,那么0并不是最短路,而是要出去一圈回来之后才是最短路,那么第K短路也是一样. 2.每个顶点和每条边都 ...
- POJ 2914 - Minimum Cut - [stoer-wagner算法讲解/模板]
首先是当年stoer和wagner两位大佬发表的关于这个算法的论文:A Simple Min-Cut Algorithm 直接上算法部分: 分割线 begin 在这整篇论文中,我们假设一个普通无向图G ...
- POJ 3461 Oulipo[附KMP算法详细流程讲解]
E - Oulipo Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
随机推荐
- Cocos2d-x3.0游戏实例《不要救我》第一章——前言
我们可以学习? 这是一个非常easy游戏.但更多的东西用(对于初学者).至少,对于它的一个例子,有点多. 笨木头花心贡献.啥?花心?不呢.是用心~ 转载请注明,原文地址:http://www.benm ...
- 【Linux命令】--(9)其他常用命令
其他常用命令+++++++++++++++++++++++++++++++lndiffdatecal grep wcpswatchatcrontab++++++++++++++++++++++++++ ...
- Catalan数总结
财产: 前20条目:1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845, ...
- Oracle解锁的相关操作(转)
当某个数据库用户在数据库中插入.更新.删除一个表的数据,或者增加一个表的主键时或者表的索引时,常常会出现ora-00054:resource busy and acquire with nowait ...
- 【Machine Learning】Mahout基于协同过滤(CF)的用户推荐
一.Mahout推荐算法简介 Mahout算法框架自带的推荐器有下面这些: l GenericUserBasedRecommender:基于用户的推荐器,用户数量少时速度快: l GenericI ...
- nodejs中使用monk訪问mongodb
mongodb 安装mongodb 我认为还是用mannual install靠谱一点儿:http://docs.mongodb.org/manual/tutorial/install-mongodb ...
- 详细解释VB连接access几种方法数据库
在VB中,连接ACCESS数据库的方法主要有以下三种 使用ADO对象,通过编写代码訪问数据库 Connection 对象 ODBC数据源 使用ADO Data 控件高速创建数据库连接 有三种连接方法 ...
- 从头到尾彻底理解KMP(转)
引言 KMP原文最初写于2年多前的2011年12月,因当时初次接触KMP,思路混乱导致写也写得非常混乱,如此,留言也是骂声一片.所以一直想找机会重新写下KMP,但苦于一直以来对KMP的理解始终不够,故 ...
- Kinect SDK C++ - 2. Kinect Depth Data
Today we will learn how to get depth data from a kinect and what the format of the data is kinect co ...
- zTree市县实现三个梯级DAO接口测试
zTree市县实现三个梯级DAO接口测试 ProvinceDaoTest.java: /** * @Title:ProvinceDaoTest.java * @Package:com.gwtjs.da ...