标签:style http color os io for ar cti
题意:给定一些点,费马点(到这些点距离和最短),输出距离和
思路:模拟退火去搞,初始温度1W步,降温系数设为0.83,然后每次找周围4个方向,做10次保证答案准确
代码:
#include <cstdio> #include <cstring> #include <cmath> #include <ctime> #include <cstdlib> #include <algorithm> using namespace std; const int d[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; const int N = 105; const double eps = 1e-3; int t, n; struct Point { double x, y, d; Point() {} Point(double a, double b) { x = a; y = b; } } p[N]; double getdis(Point P) { double ans = 0; for (int i = 0; i < n; i++) { double dx = p[i].x - P.x; double dy = p[i].y - P.y; ans += sqrt(dx * dx + dy * dy); } return ans; } double gao() { int cnt = 10; double ans = 1e20, r = 0.83; srand(time(NULL)); while (cnt--) { double step = 1e4; Point now = Point(rand() % 10001, rand() % 10001); now.d = getdis(now); while (step > eps) { Point next; for (int i = 0; i < 4; i++) { next = Point(now.x + step * d[i][0], now.y + step * d[i][1]); next.d = getdis(next); if (next.d < now.d) now = next; } step *= r; ans = min(ans, now.d); } } return ans; } int main() { scanf("%d", &t); while (t--) { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%lf %lf", &p[i].x, &p[i].y); printf("%.0lf\n", gao()); if (t) printf("\n"); } return 0; }
UVA 10228 - Star not a Tree?(模拟退火),布布扣,bubuko.com
UVA 10228 - Star not a Tree?(模拟退火)
标签:style http color os io for ar cti
原文地址:http://blog.csdn.net/accelerator_/article/details/38497533