题目链接:uva 10228 - Star not a Tree?
题目大意:给定若干个点,求费马点(距离所有点的距离和最小的点)
解题思路:模拟退火算法,每次向周围尝试性的移动步长,如果发现更优点,则转移。每次操作之后减少步长后做同样的操作,直到步长小于指定精度。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <algorithm>
using namespace std;
const int maxn = 105;
const int MOD = 1e4+1;
const double eps = 1e-9;
const double INF = 0x3f3f3f3f3f3f3f3f;
const int dir[8][2] = {{-1, -1}, {0, -1}, {1, -1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1} };
struct point {
double x, y;
point (double x = 0, double y = 0) {
this->x = x;
this->y = y;
}
}p[maxn];
int N;
double distance (point u) {
double ret = 0;
for (int i = 0; i < N; i++) {
double dx = u.x - p[i].x;
double dy = u.y - p[i].y;
ret += sqrt(dx * dx + dy * dy);
}
return ret;
}
double solve () {
int ti = 10;
double ret = INF, r = 0.9;
srand(time(NULL));
while (ti--) {
point u(rand() % MOD, rand() % MOD);
double step = 1e4;
double dis = distance(u);
while (step > eps) {
point v = u;
for (int i = 0; i < 8; i++) {
point tr(u.x + dir[i][0] * step, u.y + dir[i][1] * step);
double tmpd = distance(tr);
if (tmpd < dis) {
dis = tmpd;
v = tr;
}
}
u = v;
step *= r;
ret = min(ret, dis);
}
}
return ret;
}
int main () {
int cas;
scanf("%d", &cas);
while (cas--) {
scanf("%d", &N);
for (int i = 0; i < N; i++)
scanf("%lf%lf", &p[i].x, &p[i].y);
printf("%.0lf\n", solve());
if (cas)
printf("\n");
}
return 0;
}
分享到:
相关推荐
判断输入字符串是否为镜像或回文串。 来源于UVaOJ - 401. 水题。
开源项目-codingsince1985-UVa#uva-online-judge-solutions-in-golang.zip,两年来每天都在解决一个uva在线裁判问题,算起来…
uva705 Slash Maze 的代码,在UVaOJ上通过
PDF试题
Algorithm-UVA-Solutions-in-Python.zip,python 3中各种uva(acm)问题的解决方案。,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
uva532 Dungeon Master的源代码,并且AC了
这是UVA133 TheDoleQueue救济金发放问题,经典的算法问题。初学算法的人要对这种算法非常熟悉并且能熟练运用。
tpcw-nyu-uva-client 客户端
leetcode 2 算法-Java UVa Online Judge(ACM-ICPC Live ...使用:数组、哈希表、链表、二分搜索、动态规划、堆栈、堆、reedy、排序、树 DFS、BFS、图、二分搜索树、递归、记忆、队列、映射等。...Uva-ACM-ICPC