UVA 10514 【computing geometry】

原创 2016年05月31日 23:19:53

Thinking: 

This problem is the combination of computing geometry and dijkstra algorithm. We consider  bank1 and bank2 as two point. Consider each island as a point.

There are some things that attentions should be paid to: 

(1)  Do remember the last segment of a n>=4 polygon, which is vertix[1] and vertix[m].

(2) Do remember to try two end point of a segment when computing the minimum distance between the segment and polygon.




AC code:

#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
double D[15];
bool visited[15];
double MIN(double a, double b)
{
	if (a > b)
		return b;
	else return a;
}
struct edge{
	int v, nxt;
	double w;
};
edge E[500];
int total_edge;
int head[15];
void add_edge(int u, int v, double w)
{
	E[total_edge].v = v;
	E[total_edge].w = w;
	E[total_edge].nxt = head[u];
	head[u] = total_edge++;
}
struct point{
	int x, y;
};
struct island{
	int m;
	point P[25];
};
island DAO[12];
point bank1[105];
point bank2[105];
double dis_p_to_p(point p1, point p2)
{
	int dx = p1.x - p2.x;
	int dy = p1.y - p2.y;
	int temp = dx*dx + dy*dy;
	return sqrt(temp);
}
double dot(point vec1, point vec2)
{
	return vec1.x*vec2.x + vec1.y*vec2.y;
}
double cross(point v1, point v2)
{
	return v1.x*v2.y - v2.x*v1.y;
}
double dis_point_to_line(point p1, point p2, point p3)
{
	point vec;
	vec.x = p1.x - p2.x;
	vec.y = p1.y - p2.y;
	point vec1;
	vec1.x= p3.x - p1.x;
	vec1.y = p3.y - p1.y;
	point vec2;
	vec2.x = p3.x - p2.x;
	vec2.y = p3.y - p2.y;
	if (dot(vec, vec1)*dot(vec, vec2)>= 0)
		return MIN(dis_p_to_p(p1, p3), dis_p_to_p(p2, p3));
	return fabs(cross(vec, vec1)) / dis_p_to_p(p1, p2);
}
double dis_line_to_polygon(point p1, point p2, int poly)
{
	island I = DAO[poly];
	double dis = 200000000;
	for (int i = 1; i <= I.m; i++)
	{
		dis = MIN(dis, dis_point_to_line(p1, p2, I.P[i]));
	}
	for (int j = 1; j < I.m; j++)
	{
		dis = MIN(dis, dis_point_to_line(I.P[j], I.P[j + 1], p1));
		dis = MIN(dis, dis_point_to_line(I.P[j], I.P[j + 1], p2));
	}
	dis = MIN(dis, dis_point_to_line(I.P[1], I.P[I.m], p1));
	dis = MIN(dis, dis_point_to_line(I.P[1], I.P[I.m], p2));
	return dis;
}
double polygon_to_polygon(int poly1,int poly2)
{
	double dis = 200000000;
	island L1 = DAO[poly1];
	island L2 = DAO[poly2];
	for (int i = 1; i <= L1.m; i++)
	{
		for (int j = 1; j < L2.m; j++)
			dis = MIN(dis, dis_point_to_line(L2.P[j], L2.P[j + 1], L1.P[i]));
		dis = MIN(dis, dis_point_to_line(L2.P[1], L2.P[L2.m], L1.P[i]));
	}
	for (int i = 1; i <= L2.m; i++)
	{
		for (int j = 1; j < L1.m; j++)
			dis = MIN(dis, dis_point_to_line(L1.P[j], L1.P[j + 1], L2.P[i]));
		dis = MIN(dis, dis_point_to_line(L1.P[1], L1.P[L1.m], L2.P[i]));
	}
	return dis;
}
int main()
{
	//freopen("in.txt", "r", stdin);
	int T;
	cin >> T;
	int r1, r2, n;
	while (T--)
	{
		memset(head, -1, sizeof(head));
		total_edge = 0;
		scanf("%d%d%d", &r1, &r2, &n);
		int bk2 = n + 1;
		for (int i = 1; i < 15; i++)
			D[i] = 200000000;
		for (int i = 1; i <= r1; i++)
			scanf("%d%d", &bank1[i].x, &bank1[i].y);

		for (int i = 1; i <= r2; i++)
			scanf("%d%d", &bank2[i].x, &bank2[i].y);
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &DAO[i].m);
			for (int j = 1; j <= DAO[i].m; j++)
				scanf("%d%d", &DAO[i].P[j].x, &DAO[i].P[j].y);
		}
		double dis_b_b = 200000000;
		for (int i = 1; i <= r1; i++) // bank to bank
			for (int j = 1; j < r2; j++)
			{
				dis_b_b = MIN(dis_b_b, dis_point_to_line(bank2[j], bank2[j + 1], bank1[i]));
			}
		for (int i = 1; i <= r2; i++)
			for (int j = 1; j < r1; j++)
			{
				dis_b_b = MIN(dis_b_b, dis_point_to_line(bank1[j], bank1[j + 1], bank2[i]));
			}
		add_edge(0, bk2, dis_b_b);
		//add_edge(bk2, 0, dis_b_b);////
		//compute polygon
		for (int i = 1; i <= n; i++)
			for (int j = i + 1; j <= n; j++)
			{
				double temp = polygon_to_polygon(i, j);
				add_edge(i, j, temp);
				add_edge(j, i, temp);
			}
		// compute poly to bank1
		for (int i = 1; i <= n; i++)
		{
			double temp = 200000000;
			for (int j = 1; j < r1; j++)
			{
				temp = MIN(dis_line_to_polygon(bank1[j], bank1[j + 1], i), temp);
			}
			add_edge(0, i, temp);
		}
		for (int i = 1; i <= n; i++)
		{
			double temp = 200000000;
			for (int j = 1; j < r2; j++)
			{
				temp = MIN(dis_line_to_polygon(bank2[j], bank2[j + 1], i), temp);
			}
			add_edge(i, bk2, temp);
		}
		D[0] = 0;
		memset(visited, false, sizeof(visited));
		for (int k = 1; k <= n + 2; k++)
		{
			int min = -1;
			for (int i = 0; i <= bk2; i++)
			{
				if ((min == -1) && !visited[i])
					min = i;
				if ((D[i] < D[min])&&!visited[i])
					min = i;
			}
			if (min == -1)
				break;
			visited[min] = true;
			for (int a = head[min]; a != -1; a = E[a].nxt)
			{
				int v = E[a].v;
				if (D[v] > D[min] + E[a].w)
					D[v] = D[min] + E[a].w;
			}
		}
		printf("%.3lf\n", D[bk2]);
	}
	return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

【Computing Geometry --计算几何】初级篇 <一>

定义 计算几何computational geometry,研究几何模型和数据处理的学科,探讨几何形体的计算机表示。分析和综合,研究如何灵活、有效的建立几何形体的数学模型以及在计算机中更好地存储和管...

Introduction to Computing with Geometry Notes

  • 2017年07月20日 16:24
  • 5.35MB
  • 下载

【动态规划】【状态压缩DP】[UVa 1354]Mobile Computing

其实就是枚举一下每一次左边的用那些右边的用那些,处理一下当前所有组合的左端点和又短点就行了#include #include #include #include #include using...

uva 1354——Mobile Computing

There is a mysterious planet called Yaen, whose space is 2-dimensional. There are many beautiful sto...
  • bobodem
  • bobodem
  • 2015年10月24日 19:50
  • 325

搜索 【uva1354】 Mobile Computing (练习题7-7 天平难题)

7-7天平难题(Mobile Computing,uva 1354) (mobile.cpp,Time Limit:3000MS )题目描述: 给出房间的宽度r和s个挂坠的重量wi,设计一个尽量宽...
  • Todobe
  • Todobe
  • 2017年01月07日 11:21
  • 275

UVA1354 Mobile Computing(DFS)

题目大意:给出房间宽度,重物个数n(n 思路:显然的搜索题。由于重物的个数最多只有6个,因此我们可以状态压缩,用一个01串表示。然后暴力枚举左右天平的情况,用记忆化搜索。 #include #in...
  • cqbzwja
  • cqbzwja
  • 2015年07月20日 08:36
  • 250

UVa 1354 - Mobile Computing <枚举+二叉树>

There is a mysterious planet called Yaen, whose space is 2-dimensional. There are many beautiful sto...
  • kun768
  • kun768
  • 2015年01月29日 16:03
  • 871

UVA - 1354 Mobile Computing: 枚举二叉树 位运算枚举子集,枚举子集的子集 回溯

题目点此跳转 There is a mysterious planet called Yaen, whose space is 2-dimensional. There are many beau...
  • a27038
  • a27038
  • 2017年07月19日 14:27
  • 124

例题7-7 UVA 1354 Mobile Computing (搜索+二叉树(类哈夫曼树))

思路: 想一想就可以知道,一个结点要么有两个儿子,要么没儿子。 那么这就很像哈夫曼树,因此我们可以枚举所有的哈夫曼树(任意枚举两个不同的结点来合并) 然后从根结点开始遍历二叉树,在遍历的同时,来...

UVa 1354 - Mobile Computing(二叉树 + DFS)

题目链接https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:UVA 10514 【computing geometry】
举报原因:
原因补充:

(最多只允许输入30个字)