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;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

时间: 2024-08-24 11:34:42

POJ 2560 Freckles Prime问题解决算法的相关文章

POJ 2560 Freckles Prime算法题解

本题是求最小生成树. 给出的是坐标节点,然后需要根据这些坐标计算出各个点之间的距离. 除此就是标准的Prime算法了,能使用Prime的基本上都可以使用Kruskal. 这些经典的算法一定要多写,熟练掌握,否则很难灵活运用的. 而且经典的算法之所以为经典,原因之一是没那么容易自己凭空想象出来的,所以要熟练. #include <stdio.h> #include <string.h> #include <queue> #include <float.h> #

poj 2560 Freckles

题目连接 http://poj.org/problem?id=2560 Freckles Description In an episode of the Dick Van Dyke show, little Richie connects the freckles on his Dad's back to form a picture of the Liberty Bell. Alas, one of the freckles turns out to be a scar, so his Ri

POJ 2560 Freckles(最小生成树)

原题地址:http://poj.org/problem?id=2560 Freckles Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7863   Accepted: 3776 Description In an episode of the Dick Van Dyke show, little Richie connects the freckles on his Dad's back to form a pictu

POJ 3318 Matrix Multiplication(随机化算法)

给你三个矩阵A,B,C.让你判断A*B是否等于C. 随机一组数据,然后判断乘以A,B之后是否与乘C之后相等. 很扯淡的啊,感觉这种算法不严谨啊... Matrix Multiplication Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16255   Accepted: 3515 Description You are given three n × n matrices A, B and C. Does the e

POJ 3264 RMQ Spare Table算法

今天下午大帝讲的,我以前也不懂,所以也就跟着学学了,把中间的那个状态转移方程学错了好几次,于是就wa了 好几发. #include<iostream> #include<cstdio> #include<algorithm> #define maxn 200010 using namespace std; int a[maxn],m,n,b[maxn],fl[maxn][50],fr[maxn][50]; void solve() { b[1]=0;//其实就是用来计算

[POJ] 3264 Balanced Lineup [ST算法]

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34306   Accepted: 16137 Case Time Limit: 2000MS Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer Joh

poj——2031 最小生成树(MST) Kruskal算法

poj——2031 最小生成树(MST)  Kruskal算法 Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4985   Accepted: 2503 Description You are a member of the space station engineering team, and are assigned a task in the constructio

POJ 1125 Stockbroker Grapevine (Floyd算法)

Floyd算法计算每对顶点之间的最短路径的问题 题目中隐含了一个条件是一个人可以同时将谣言传递给多个人 题目最终的要求是时间最短,那么就要遍历一遍求出每个点作为源点时,最长的最短路径长是多少,再求这些值当中最小的是多少,就是题目所求 #include<bits/stdc++.h> using namespace std; int n,x,p,t; int m[120][120],dist[120][120],Max[120]; void floyd(int n,int m[][120],int

POJ 2135 Farm Tour (dinic算法,网络流)

构图方法: 注意题目中的边为无向边.新建源点s 和 汇点t 每两条道路连一条容量为1,费用为w的边.s到1连一条容量为1,费用为0 的边,n到 t 连一条容量为1,费用为0 的边,求最大流. #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <algorithm> #include <queue> #include