码迷,mamicode.com
首页 > 其他好文 > 详细

【BZOJ3680】吊打XXX 模拟退火

时间:2017-07-27 09:38:04      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:std   poi   can   return   led   name   sof   data   span   

【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

题解:设绳结最后的位置为(x,y),那么一定满足∑g[i]*sqrt((x-xi)2+(y-yi)2)最小。所以就变成了模拟退火的板子题了。

当然,爬山也可以做,就是每次算出当前点受到的合外力方向,然后一直朝着那个方向移动就行了。(然而我物理学的不好啊QAQ)

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
const int maxn=10010;
double minn,T;
int n;
struct point
{
	double x,y;
}p[maxn],ans,now,neo;
double g[maxn];
double Rand()
{
	return rand()%1000/1000.0;
}
double sqr(double x)
{
	return x*x;
}
double solve(point a)
{
	double ret=0;
	for(int i=1;i<=n;i++)	ret+=g[i]*sqrt(sqr(a.x-p[i].x)+sqr(a.y-p[i].y));
	if(ret<minn)	ans=a,minn=ret;
	return ret;
}
int main()
{
	scanf("%d",&n);
	srand(2333666);
	int i;
	for(i=1;i<=n;i++)	scanf("%lf%lf%lf",&p[i].x,&p[i].y,&g[i]),now.x+=p[i].x,now.y+=p[i].y;
	now.x/=n,now.y/=n,minn=9223372036854775807.0,solve(now);
	T=1000000;
	while(T>0.001)
	{
		neo.x=now.x+T*(Rand()+Rand()-1.0);
		neo.y=now.y+T*(Rand()+Rand()-1.0);
		double de=solve(now)-solve(neo);
		if(de>0||exp(de/T)>Rand())	now=neo;
		T*=0.993;
	}
	for(i=1;i<=1000;i++)
	{
		neo.x=ans.x+T*(Rand()+Rand()-1.0);
		neo.y=ans.y+T*(Rand()+Rand()-1.0);
		solve(neo);
	}
	printf("%.3lf %.3lf",ans.x,ans.y);
	return 0;
}

【BZOJ3680】吊打XXX 模拟退火

标签:std   poi   can   return   led   name   sof   data   span   

原文地址:http://www.cnblogs.com/CQzhangyu/p/7242952.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!