码迷,mamicode.com
首页 > 其他好文 > 详细

1331 - Minimax Triangulation (区间DP+几何)

时间:2014-05-07 12:15:38      阅读:367      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   tar   color   

题目链接:1331 - Minimax Triangulation

题意:按顺序给定一些点,把这些点分割为n - 2个三角形,代价为最大三角形面积,求代价最小
思路:区间DP,dp[i][j]代表一个区间内,组成的情况,枚举k,dp[i][j] = min(max(dp[i][k],dp[k][j], area(i, j, k)),area代表i、j、k三点构成的三角形面积,然后判断该三角形内有没其他点即可
代码:
#include <stdio.h>
#include <string.h>
#include <math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))

#define INF 0x3f3f3f3f
const int N = 55;
const double eps = 1e-6;
int t, n;
double dp[N][N];
struct Point {
	double x, y;
	void read() {
		scanf("%lf%lf", &x, &y);
	}
} p[N];

double area(Point a, Point b, Point c) {
	return fabs((b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y)) / 2.0;
}

void init() {
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
		p[i].read();
}

bool judge(int i, int j, int k) {
	double s = area(p[i], p[j], p[k]);
	for (int x = 0; x < n; x++) {
		if (x == i || x == j || x == k) continue;
		double sum = area(p[i], p[j], p[x]) + area(p[i], p[k], p[x]) + area(p[k], p[j], p[x]);
		if (fabs(sum - s) < eps) return false;
	}
	return true;
}

double solve() {
	double ans = INF;
	for (int len = 2; len < n; len++) {
		for (int l = 0; l < n; l++) {
			int r = (l + len) % n;
			dp[l][r] = INF;
			for (int k = (l + 1) % n; k != r; k = (k + 1) % n) {
				if (!judge(l, k, r)) continue;
				dp[l][r] = min(dp[l][r], max(max(dp[l][k], dp[k][r]), area(p[l], p[k], p[r])));
			}
			if (len == n - 1)
				ans = min(ans, dp[l][r]);
		}
	}
	return ans;
}

int main() {
	scanf("%d", &t);
	while (t--) {
		init();
		printf("%.1lf\n", solve());
	}
	return 0;
}


1331 - Minimax Triangulation (区间DP+几何),布布扣,bubuko.com

1331 - Minimax Triangulation (区间DP+几何)

标签:style   blog   class   code   tar   color   

原文地址:http://blog.csdn.net/accelerator_/article/details/25186769

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!