一、步长 *= 降火速度
二、当某次随机出来所有方向的点都没有比当前点更接近正解时,步长 *= 降火速度// 一、poj 1379
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <ctime>
using namespace std;
const int N = 11111, P = 15, D = 30; // P为随机出来的点数,D为随机出来的方向数
const double EPS = 1e-5, PI = acos(-1.0), INF = 1e20;
int m;
double dist[N];
struct Point
{
double x, y;
Point() {}
Point(double x, double y) : x(x), y(y) {}
} p[N], tp[N];
Point operator - (const Point &a, const Point &b) { return Point(a.x - b.x, a.y - b.y); }
inline double Veclen(const Point &a) { return sqrt(a.x * a.x + a.y * a.y); }
inline double cal(const Point &a)
{
double ret = INF;
for(int i = 0; i < m; ++i) {
ret = min(ret, Veclen(p[i] - a));
}
return ret;
}
int main()
{
srand(time(0));
int t, x, y;
scanf("%d", &t);
while(t-- && scanf("%d%d%d", &x, &y, &m)) {
for(int i = 0; i < m; ++i) {
scanf("%lf%lf", &p[i].x, &p[i].y);
}
for(int i = 0; i < P; ++i) {
tp[i].x = (rand() % 1000 + 1) / 1000.0 * x;
tp[i].y = (rand() % 1000 + 1) / 1000.0 * y;
dist[i] = cal(tp[i]);
}
double step = Veclen(Point(x * 1.0, y * 1.0)) / 2; // 此处为初始步长
while(step > EPS) {
for(int i = 0; i < P; ++i) {
Point cur, pre = tp[i];
for(int j = 0; j < D; ++j) {
double ang = (rand() % 1000 + 1) / 1000.0 * 2 * PI;
cur = Point(pre.x + cos(ang) * step, pre.y + sin(ang) * step);
if(cur.x < 0 || cur.x > x || cur.y < 0 || cur.y > y) continue;
double ret = cal(cur);
if(ret > dist[i]) {
dist[i] = ret;
tp[i] = cur;
}
}
}
step *= 0.8; // 0.8为降火速度
}
int ans = 0;
for(int i = 1; i < P; ++i) {
if(dist[i] > dist[ans]) {
ans = i;
}
}
printf("The safest point is (%.1f, %.1f).\n", tp[ans].x, tp[ans].y);
}
return 0;
}
// 二、SCU 4369
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <ctime>
const int N = 2333, D = 8;
const double EPS = 1e-7, PI = acos(-1.0);
struct Point
{
double x, y, w;
Point() { x = y = w = 0; }
Point(double x, double y) : x(x), y(y) {}
void input() { scanf("%lf%lf%lf", &x, &y, &w); }
} p[N];
Point operator - (const Point &a, const Point &b) { return Point(a.x - b.x, a.y - b.y); }
inline double Veclen(const Point &a) { return sqrt(a.x * a.x + a.y * a.y); }
int n;
double cal(const Point &a)
{
double ret = 0;
for(int i = 0; i < n; ++i) {
ret += Veclen(p[i] - a) * p[i].w;
}
return ret;
}
int main()
{
srand(time(0));
while(~scanf("%d", &n)) {
for(int i = 0; i < n; ++i) {
p[i].input();
}
Point a;
for(int i = 0; i < n; ++i) {
a.x += p[i].x * p[i].w;
a.y += p[i].y * p[i].w;
a.w += p[i].w;
}
a.x /= a.w, a.y /= a.w, a.w /= n;
double step = 20, val = cal(a);
while(step > EPS) {
Point cur, pre = a;
bool flag = 0;
for(int i = 0; i < D; ++i) {
double ang = (rand() % 1000) / 1000.0 * 2 * PI;
cur.x = pre.x + cos(ang) * step;
cur.y = pre.y + sin(ang) * step;
double ret = cal(cur);
if(ret + EPS < val) {
val = ret;
a = cur;
flag = 1;
}
}
if(!flag) {
step *= 0.5;
}
}
printf("%.2f\n", val / n);
}
return 0;
}