您的位置:首页 > 其它

算法学习笔记:爬山法与模拟退火

2018-01-10 09:47 661 查看

bzoj3680: 吊打XXX

Description

gty又虐了一场比赛,被虐的蒟蒻们决定吊打gty。gty见大势不好机智的分出了n个分身,但还是被人多势众的蒟蒻抓住了。蒟蒻们将

n个gty吊在n根绳子上,每根绳子穿过天台的一个洞。这n根绳子有一个公共的绳结x。吊好gty后蒟蒻们发现由于每个gty重力不同,绳

结x在移动。蒟蒻wangxz脑洞大开的决定计算出x最后停留处的坐标,由于他太弱了决定向你求助。

不计摩擦,不计能量损失,由于gty足够矮所以不会掉到地上。

Input

输入第一行为一个正整数n(1<=n<=10000),表示gty的数目。

接下来n行,每行三个整数xi,yi,wi,表示第i个gty的横坐标,纵坐标和重力。

对于20%的数据,gty排列成一条直线。

对于50%的数据,1<=n<=1000。

对于100%的数据,1<=n<=10000,-100000<=xi,yi<=100000

Output

输出1行两个浮点数(保留到小数点后3位),表示最终x的横、纵坐标。

Sample Input

3

0 0 1

0 2 1

1 1 1

Sample Output

0.577 1.000

链接

这里写得很详细http://xxccxcxc.top/post/21/

代码

基本是抄上面博客的。水一波~

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
const int N = 501000;
int read() {
char ch = getchar(); int x = 0, f = 1;
while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
while(ch >= '0' && ch <= '9') {x = (x << 1) + (x << 3) - '0' + ch; ch = getchar();}
return x * f;
}
int n;
struct Point {double x, y;};
double sqr(double a) {return a * a;}
double dist(Point a, Point b) {return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y));}
struct Node {
Point p; double g;
void init() {scanf("%lf%lf%lf", &p.x, &p.y, &g);}
}P
;
struct Ans {
Point p; double val;
void calcval() {
val = 0;
for(int i = 1;i <= n; ++i)
val += dist(p, P[i].p) * P[i].g;
}
}ans;
double Grand() {return rand() % 10000 / 10000.0;}

void Move() {
Ans u = ans; double T;
for(T = 100000; T > 0.001; T *= 0.98) {
Ans v;
v.p.x = u.p.x + T * (Grand() * 2 - 1);
v.p.y = u.p.y + T * (Grand() * 2 - 1);
v.calcval(); if(v.val < ans.val) ans = v;
double delta = u.val - v.val;
if(delta > 0 || Grand() < exp(delta / T)) u = v;
}
for(int i = 1;i <= 1000; ++i) {
u.p.x = ans.p.x + T * (Grand() * 2 - 1);
u.p.y = ans.p.y + T * (Grand() * 2 - 1);
u.calcval(); if(u.val < ans.val) ans = u;
}
}

int main() {
srand(233333333);
scanf("%d", &n);
for(int i = 1;i <= n; ++i) {
P[i].init();
ans.p.x += P[i].p.x;
ans.p.y += P[i].p.y;
}
ans.p.x /= 1.0 * n;
ans.p.y /= 1.0 * n;
ans.calcval();
Move();
printf("%.3lf %.3lf\n", ans.p.x, ans.p.y);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: