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 ...
随机推荐
- Linux下一个CD翻录 创CUE 压缩flac攻略
于Windows我们通常使用较低EAC翻录压缩发生器CUE找工作的步骤,但在Linux在稍微有点麻烦.每一步,我们需要自己做.经过我的反复尝试寻找和总结了相当不错的方法,使用软件和步骤如下面的. 使用 ...
- 高性能 TCP & UDP 通信框架 HP-Socket v3.2.3 正式公布
HP-Socket 是一套通用的高性能 TCP/UDP 通信框架,包括服务端组件.client组件和 Agent 组件,广泛适用于各种不同应用场景的 TCP/UDP 通信系统,提供 C/C++.C#. ...
- 【MongoDB】Serveral common command of MongoDb
In the recent days, since the overwork made me exhaused, on arrival to home I will go to bed, which ...
- 手游client思考框架
手游新公司新项目client我不太同意框架.虽然我也终于让步,当他居然问老板,使这个幼稚的行为而悔恨. 然而,就在最近我写了一些代码视图,我更坚定了自己的想法和思想.和思路不一定适合其它人,所以我并不 ...
- tomcat如何避免遭遇ClassNotFoundException
于Tomcat紧接着为什么要创建一个类加载器Thread.currentThread().setContextClassLoader(catalinaLoader)?这里加载失败主要是为了避免以后加载 ...
- java中的执行顺序
静态,非静态,构造,先父再子另外,静态块与静态变量的顺序取决于代码中的顺序 Comparable接口应用
- WinForm LED循环显示信息,使用定时器Threading.Timer
原文:WinForm LED循环显示信息,使用定时器Threading.Timer 这里用一个示例来演示timer如何使用.示例:LED屏幕显示描述:这个示例其实很简单,LED屏幕上显示3个信息: ...
- OS X升级到10.10使用后pod故障解决方案出现
最新的mac 10.10强大的好奇心,所以,你的系统升级到10.10.结果表明,使用pod出现下述问题: /System/Library/Frameworks/Ruby.framework/Versi ...
- aul 学习测试(测量)
-------------------aul5 ----------test0------------------------- select file#,rfile#,name from v$dat ...
- java线 生产者和消费者
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGlhbmdydWkxOTg4/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...