题意:在一个二维平面上若干个点,每个点有自己的坐标和重量,然后他们通过无重量绳子连在某一个端点上(端点不一定在点集中,一定在平面上)。然后他们会对整个系统贡献能量,要求找到一个最优端点,使整个系统最稳定。
- 求最优解
- 统计贡献
- 答案不单调
还是一起愉快的玄学吧
模拟退火原理(来自百度百科)
模拟退火的原理也和金属退火的原理近似:将热力学的理论套用到统计学上,将搜寻空间内每一点想像成空气内的分子;分子的能量,就是它本身的动能;而搜寻空间内的每一点,也像空气分子一样带有“能量”,以表示该点对命题的合适程度。演算法先以搜寻空间内一个任意点作起始:每一步先选择一个“邻居”,然后再计算从现有位置到达“邻居”的概率。
简单的来说就是,一开始在可接受范围内(温度)随机找解
- 若当前状态比最优解更优,就把当前解当做最优解
- 否则,以一个玄学概率接受这个解(即下一次找解从当前解开始,而不是从上一个解开始)
- 根据Metropolis接受准则,这个概率与exp(−ΔE/kT)相关
然后逐步减少可接受范围(即退火,降温)
最后可接受区间为一个点时,我们认为这个解有可能是全局最优解
inline void SA(){
double tmpx=resx,tmpy=resy;//初始解
t=2000;
while(t>eps){
double nx=tmpx+(2*rand()-RAND_MAX)*t;//当前解
double ny=tmpy+(2*rand()-RAND_MAX)*t;
double nres=cal(nx,ny);//计算当前解
double D=nres-res;//与最优解比较
if(D<0){
tmpx=nx,tmpy=ny;
resx=nx,resy=ny;
res=nres;
}else if(exp(-D/t)*RAND_MAX>rand())tmpx=nx,tmpy=ny;
t*=delta;//退火
}
}
最后的最后最重要的是
。
。
。
。
。
。
多重复几遍
while((double)clock()/CLOCKS_PER_SEC<0.8)SA();
找个好看点的随机值
完整代码
#include<algorithm>
#include<vector>
#include<iostream>
#include<math.h>
#include<cstring>
#include<string>
#include<stack>
#include<map>
#include<set>
#include<unordered_map>
#include<queue>
#include<assert.h>
#include<iomanip>
#include<bitset>
#define qcin; ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp make_pair
#define clr(x) memset(x,0,sizeof x)
#define fmax(x) memset(x,0x3f,sizeof x)
#define finit(x) memset(x,-1,sizeof x)
#define iio(n,m) io(n),io(m)
#define ls(p) (p<<1)
#define rs(p) ((p<<1)|1)
#define dis(l,r) r-l+1
#define gstr(str) scanf("%s",str)
#define glen(str) strlen(str)
using namespace std;
namespace Input{
const int BUF = 65536;
char buf[BUF + 1];
char *head = buf, *tail = buf;
}
inline char inputchar(){
using namespace Input;
if(head == tail)
*(tail = (head = buf) + fread(buf, 1, BUF, stdin)) = 0;
return *head++;
}
template<class T>
inline void io(T &ret){
ret = 0;
char ch = inputchar();
while((ch < '0' || ch > '9') && ch != '-')
ch = inputchar();
bool neg = false;
if(ch == '-')
neg = true, ch = inputchar();
while(ch >= '0' && ch <= '9')
{
ret = ret * 10 + ch - '0';
ch = inputchar();
}
if(neg)
ret = -ret;
}
typedef long long ll;
typedef pair<int,int>pll;
const int maxn = 1010;
const int mod = 1e9+7;
const ll inf = 1e18;
typedef ll arr[maxn];
typedef char str[maxn];
void file(int x){if(x&&fopen("123.in","r")){freopen("123.in","r",stdin);}}
const long double pi = acos(-1);
const double eps=1e-14;
const double delta=0.993;
double resx,resy,res,t;
int n;
struct node{
int x,y,w;
void input(){
io(x),io(y),io(w);
resx+=x,resy+=y;
}
}a[maxn];
inline double cal(double x,double y){
double res=0;
for(int i=0;i<n;i++){
double dx=x-a[i].x,dy=y-a[i].y;
res+=sqrt(dx*dx+dy*dy)*a[i].w;
}
return res;
}
inline void SA(){
double tmpx=resx,tmpy=resy;//初始解
t=2000;
while(t>eps){
double nx=tmpx+(2*rand()-RAND_MAX)*t;//当前解
double ny=tmpy+(2*rand()-RAND_MAX)*t;
double nres=cal(nx,ny);//计算当前解
double D=nres-res;//与最优解比较
if(D<0){
tmpx=nx,tmpy=ny;
resx=nx,resy=ny;
res=nres;
}else if(exp(-D/t)*RAND_MAX>rand())tmpx=nx,tmpy=ny;
t*=delta;//退火
}
}
void sol(){
res=1e18;
while((double)clock()/CLOCKS_PER_SEC<0.8)SA();
}
int main(){
srand(time(NULL));
srand(rand());
file(1);
io(n);
for(int i=0;i<n;i++)a[i].input();
resx/=n,resy/=n;
sol();
printf("%.3f %.3f\n",resx,resy);
return 0;
}