题目描述: http://poj.org/problem?id=2420
题目大意: 其实是求n 边形的费马点, 即到n 边形的n 个顶点距离和最小的点,
该题要求计算出最小距离, 并四舍五入.
算法大意: 先拿一个点(该算法用n 个顶点的x, y 坐标和的均值所在的点)去计算距离和
最小值, 然后拿它的四个方向上的点去测试比较哪个点更优, 不断
迭代, 最终得到在精度允许下的费马点.
#include <cstdio> #include <cmath> struct point { double x, y; point() { x = y = 0.0; } }; //返回两点间的距离 inline double mydistance(point p1, point p2) { return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)); } //求n 边形的费马点(参数p_fermat), 返回最小距离和 double fermat_point(point p[], int n, point& p_fermat) { point u, v; double step = 0.0, curlen, explen, minlen; int i, j, k, idx; bool flag; //u.x = u.y = v.x = v.y = 0.0; //point "构造函数" for(i = 0; i < n; i++) { step += fabs(p[i].x) + fabs(p[i].y); u.x += p[i].x; u.y += p[i].y; } u.x /= n; u.y /= n; flag = 0; while(step > 1e-10) { for(k = 0; k < 10; step /= 2, k++) for(i = -1; i <= 1; i++) for(j = -1; j <= 1; j++) { v.x = u.x + step * i; v.y = u.y + step * j; curlen = explen = 0.0; for(idx = 0; idx < n; idx++) { curlen += mydistance(u, p[idx]); explen += mydistance(v, p[idx]); } if(curlen > explen) { u = v; minlen = explen; flag = 1; } } } p_fermat = u; return flag ? minlen : curlen; } int main() { point ploygon[101], p_fermat; double len; int n; while(scanf("%d", &n) != EOF) { for(int i = 0; i < n; i++) scanf("%lf %lf", &ploygon[i].x, &ploygon[i].y); len = fermat_point(ploygon, n, p_fermat); // rounded to the nearest integer /* if(len - (int)len > 0.5) printf("%d\n", (int)(len + 1)); else printf("%d\n", int(len)); */ printf("%d\n", int(len + 0.5)); } return 0; }
以上算法参考自代码疯子的博客:http://www.programlife.net/poj-2420-polygon-fermat-point.html
发表评论
-
ACM 之 Java BigInteger
2011-06-01 20:26 0Java 的大整数类在ACM 中大有用武之地 ... -
判断点是否构成多边形, 顶点连续给出
2011-05-26 14:27 0#include <cstdio> #inc ... -
poj pku 1981 Circle and Points 点与圆 位置关系
2011-05-26 11:29 1261题目描述: http://poj.org/problem?id ... -
poj 1032 Parliament 数学
2011-05-25 17:34 1211题目描述: http://poj.org/problem?i ... -
poj 1385 Lifting the Stone 多边形重心
2011-05-25 11:13 1026题目描述: http://poj.org/problem?i ... -
poj 2676 Sudoku dfs 深搜
2011-05-16 21:05 872题目描述: http://poj.org/problem?i ... -
hdoj 2064 汉诺塔III 递推
2011-05-15 22:29 883题目描述: http://acm.hdu.edu.cn/sh ... -
hdoj 1207 汉诺塔II dp 动态规划
2011-05-15 21:22 1666题目描述: http://acm.hdu.edu.cn/sh ... -
poj 2506 Tiling 递推
2011-05-15 11:18 906题目描述: http://poj.org/problem?i ... -
poj 2954 Triangle Pick 定理
2011-05-14 16:36 1083题目描述: http://poj.org/problem?i ... -
poj 1012 Joseph
2011-05-10 17:42 1231题目描述:poj.org/problem?id=10 ... -
zoj 1081 Points Within 点与多边形关系
2011-05-07 17:51 1132题目描述: http://acm.zju.edu.cn/on ... -
poj 1835 宇航员
2011-05-03 17:00 798题目描述:http://poj.org/problem?id ... -
poj 2398 Toy Storage
2011-04-23 20:19 713题目描述:http://www.poj.org/proble ... -
poj 1654 Area 多边形面积
2011-04-23 20:10 895题目描述:http://poj.org/proble ... -
poj 2318 TOYS 点 直线 位置关系
2011-04-23 10:06 664题目描述:http://poj.org/problem?id= ... -
poj pku 1673 EXOCENTER OF A TRIANGLE 三角形 垂心
2011-04-09 16:41 542题目描述:http://poj.org/problem?id= ... -
pc 111303 uva 10195 The Knights Of The Round Table
2011-04-04 16:06 750题目描述:http://www.programming-cha ... -
pc 111302 uva 10180 Rope Crisis in Ropeland!
2011-04-03 20:46 837题目描述: http://www.programming-ch ... -
poj 1971 Parallelogram Counting 平行四边形个数
2011-04-03 10:05 1212题目描述:http://poj.org/problem?id= ...
相关推荐
北大POJ2525-Text Formalization【TrieTree】 解题报告+AC代码+测试数据 http://hi.csdn.net/!s/XW4FQ3
北大POJ2513-Colored Sticks【TrieTree+MergeSet+EulerPath】 解题报告+AC代码
poj 1909 Marbles on a tree.md
poj 1308 Is It A Tree_.md
北大POJ1584-A Round Peg in a Ground Hole 解题报告+AC代码
POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类
北大POJ2255-Tree Recovery 解题报告+AC代码
北大POJ2488-A Knight's Journey 解题报告+AC代码
北大POJ1942-Paths on a Grid 解题报告+AC代码
poj 2201 Cartesian Tree.md
poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题...
北大POJ2031-Building a Space Station【Prim+计算几何】 POJ2031-Building a Space Station【Prim+计算几何】
Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dist(u,v)=The min distance between node u and v. Give an integer k,for every pair (u,v) of vertices is ...
北大POJ1005-I Think I Need a Houseboat 解题报告+AC代码
北大POJ1691-Painting A Board 【拓扑+DFS】 解题报告+AC代码
POJ1584 -A Round Peg in a Ground Hole 测试数据。数据来源 Mid-Atlantic 2003 问题D
Let the level of a star be an amount of the stars that are not higher and not to the right of the given star. Astronomers want to know the distribution of the levels of the stars. For example, ...
北大POJ1004-Financial Management 解题报告+AC代码
POJ第1861题源码 POJ第1861题源码 POJ第1861题源码
poj分类poj分类poj分类poj分类