源码力量

Math Algorithm Art

HDU 3694计算几何

/****************************************************************************************************
    找四边形的Fermat点,对于任意多边形来说找Fermat点的方法就是模拟退火,但是这道题貌似故意卡模拟退火的精度,试了好多次
都没过。。。后来看了白书,原来四边形的Fermat点就是两条对角线的焦点,还有个证明,用的是反证法,挺NX的~接着就码代码A了~
****************************************************************************************************/
#include <iostream>
#include <functional>
#include <algorithm>
//#include <hash_map>		//Visual C++ lib
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <sstream>
#include <utility>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <limits>
#include <memory>
#include <string>
#include <vector>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
using namespace std;

#define LOWBIT(x) ( (x) & ( (x) ^ ( (x) - 1 ) ) )
#define CLR(x, k) memset((x), (k), sizeof(x))
#define CPY(t, s) memcpy((t), (s), sizeof(s))
#define SC(t, s) static_cast<t>(s)
#define LEN(s) static_cast<int>( strlen((s)) )
#define SZ(s) static_cast<int>( (s).size() )

typedef double LF;
//typedef long long LL;		//GNU C++
//typedef unsigned long long ULL;
typedef __int64 LL;		//Visual C++ 2010
typedef unsigned __int64 ULL;

typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<double, double> PDD;

//typedef hash_map<int, int>::iterator HMI;
typedef map<int, int>::iterator MI;
typedef vector<int>::iterator VI;
typedef list<int>::iterator LI;
typedef set<int>::iterator SI;

template <typename T>
T sqa(const T &x)
{
	return x * x;
}

const int INF_INT = 0x3f3f3f3f;
const LL INF_LL = 0x7fffffffffffffffLL;		//15f
const double oo = 10e9;
const double eps = 10e-9;
const double PI = acos(-1.0);

const int MAXN = 6;

struct Point
{
	LF x, y;
	Point()
	{
		x = y = 0.0;
	}
	Point(const LF &_x, const LF &_y)
	{
		x = _x;
		y = _y;
	}
}pnt[MAXN];
struct Seg
{
	LF a, b, c;
	Seg()
	{
		a = b = c = 0.0;
	}
	Seg(const LF &_a, const LF &_b, const LF &_c)
	{
		a = _a;
		b = _b;
		c = _c;
	}
};
int n;

bool inline equals(const LF &a, const LF &b)
{
	return fabs(a - b) < eps;
}
bool parallel(const Seg &sa, const Seg &sb)
{
	return fabs(sa.a * sb.b - sa.b * sb.a) < eps;
}
LF dist(const Point &pa, const Point &pb)
{
	return sqrt( sqa(pa.x - pb.x) + sqa(pa.y - pb.y) );
}
Seg getseg(const Point &pa, const Point &pb)
{
	Seg sr(pb.y - pa.y, pa.x - pb.x, pa.y * pb.x - pa.x * pb.y);
	return sr;
}
LF get_all_dist(const Point &pk, const int &n)
{
	LF res = 0.0;
	for (int i = 0; i < n; ++i)
	{
		res += dist(pk, pnt[i]);
	}
	return res;
}
Point cross(const Seg &sa, const Seg &sb)
{
	Point pr;
	pr.x = (sb.c * sa.b - sa.c * sb.b) / (sa.a * sb.b - sb.a * sa.b);
	pr.y = (-sa.c - sa.a * pr.x) / sa.b;
	return pr;
}
void ace()
{
	n = 4;
	bool end_sign;
	LF mi;
	Seg sa, sb;
	while (true)
	{
		end_sign = true;
		for (int i = 0; i < n; ++i)
		{
			cin >> pnt[i].x >> pnt[i].y;
			if (!equals(-1.0, pnt[i].x) || !equals(-1.0, pnt[i].y))
			{
				end_sign = false;
			}
		}
		if (end_sign)
		{
			break ;
		}
		mi = oo;
		for (int i = 0; i < n; ++i)
		{
			mi = min(mi, get_all_dist(pnt[i], n));
		}
		sa = getseg(pnt[0], pnt[1]);
		sb = getseg(pnt[2], pnt[3]);
		if (!parallel(sa, sb))
		{
			mi = min(mi, get_all_dist( cross(sa, sb), n ) ) ;
		}
		sa = getseg(pnt[0], pnt[2]);
		sb = getseg(pnt[1], pnt[3]);
		if (!parallel(sa, sb))
		{
			mi = min(mi, get_all_dist( cross(sa, sb), n ) );
		}
		sa = getseg(pnt[0], pnt[3]);
		sb = getseg(pnt[1], pnt[2]);
		if (!parallel(sa, sb))
		{
			mi = min(mi, get_all_dist( cross(sa, sb), n ) );
		}
		printf("%.4lf\n", mi);
	}
	return ;
}
int main()
{
	ace();
	return 0;
}

阅读更多
个人分类: HDU ACM-计算几何
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭