POJ 2420

模拟退火算法。昨天看了PPT,原来模拟退火算法涉及到马尔什么链,开始理解,它其实就是一个关于抽样的问题。随机抽样,选取足够多的样本,然后逐步逼近。而在平面上,由于T的下降,使得逐渐缩小至一点。然后,就可以了。

算法:

在平面上随机选取一些点,当然这些点应当有一点相关的性吧。我猜的。

然后在这些点随机移动,若移动后更优,则移动,否则,留待下一次循环。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>

using namespace std;
const int MAXN=110;
const int MAX=50;
struct point {
	double x,y;
};
point p[MAXN]; int n; point s,tt; double ans_dis,tmp_dis;
point tar[MAX];

double getdouble(){
	double ret=(rand()*rand()%10000)*1.0/1e5;
	if(rand()&1) ret*=-1;
	return ret;
}

double dist(point &u,point &v){
	return sqrt((u.x-v.x)*(u.x-v.x)+(u.y-v.y)*(u.y-v.y));
}

double work(point &s){
	double ret=0;
	for(int i=0;i<n;i++){
		ret+=dist(p[i],s);
	}
	return ret;
}

int main(){
	srand(time(0));
	while(scanf("%d",&n)!=EOF){
		s.x=s.y=0; ans_dis=tmp_dis=0;
		for(int i=0;i<n;i++){
			scanf("%lf%lf",&p[i].x,&p[i].y);
			s.x+=p[i].x; s.y+=p[i].y;
		}
		s.x/=n; s.y/=n;
		for(int i=0;i<MAX;i++){
			tar[i].x=s.x+getdouble()*100;
			tar[i].y=s.y+getdouble()*100;
		}
		double T=100;
		double ans_dis=work(s);
		for(double t=T; t>1e-8;t*=0.8){
			for(int i=0;i<MAX;i++){
				double tmp_dis=work(tar[i]);
				for(int j=0;j<10;j++){
					tt.x=tar[i].x+getdouble()*t;
					tt.y=tar[i].y+getdouble()*t;
					double f=work(tt);
					if(f<tmp_dis) tar[i]=tt;
				}
			}
		}
		for(int i=0;i<MAX;i++){
			double mm=work(tar[i]);
			ans_dis=min(ans_dis,mm);
		}
		printf("%.0lf\n",ans_dis);
	}
	return 0;
}

  

POJ 2420

时间: 2024-08-29 18:31:26

POJ 2420的相关文章

POJ 2420 模拟退火法

A Star not a Tree? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3272   Accepted: 1664 Description Luke wants to upgrade his home computer network from 10mbs to 100mbs. His existing network uses 10base2 (coaxial) cables that allow you

三分 POJ 2420 A Star not a Tree?

题目传送门 1 /* 2 题意:求费马点 3 三分:对x轴和y轴求极值,使到每个点的距离和最小 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 10 const int MAXN = 1e2 + 10; 11 const int INF = 0x3f3f3f3f; 12 double x[MAXN], y[MAXN]; 13 i

POJ 2420 A Star not a Tree? (模拟退火)

题目地址:POJ 2420 今天在比赛遇到了这题..于是现场学了一下模拟退火.... 这题是先初始化为一个点,然后不断趋近距离和最短的点.还是挺简单的.. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h&g

模拟退火 (poj 2420, poj 2069)

模拟退火基本知识 其伪代码例如以下: Let s = s0 For k = 0 through k_max (exclusive): T := temperature(k / k_max) Pick a random neighbour, s_new := neighbour(s) If P(E(s), E(s_new), T) > random(0, 1), move to the new state: s := s_new Output: the final state s 样例: poj

poj 2420,模拟退火算法,费马点

题目链接:http://poj.org/problem?id=2420 题意:给n个点,找出一个点,使这个点到其他所有点的距离之和最小,也就是求费马点. 参考链接:http://www.cnblogs.com/heaad/archive/2010/12/20/1911614.html 这一篇文章写的很好,我这个小白都有一点小明白了. 记一下笔记: 模拟退火: 就是在一定范围内接受一些不是最优的解,来跳出局部最优,靠近整体最优. 贴一下伪代码: //#pragma comment(linker,

POJ 2420 模拟退火

链接: http://poj.org/problem?id=2420 题意: 给出n个点,找到一个点,使得它到所有的点的距离最小. 题解: 最近要做一个排课系统,需要用到模拟退火算法,之前虽然了解过这个算法,但是没有写过题.就先在POJ上找了一道学习一下. 代码: 1 #include <iomanip> 2 struct Point { double x, y; }; 3 4 const double eps = 1e-8; //搜索条件阀值 5 const double T = 100;

poj 2420 A Star not a Tree?——模拟退火

题目:http://poj.org/problem?id=2420 精度设成1e-17,做三遍.ans设成double,最后再取整. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<cstdlib> #include<ctime> #define db double usin

[POJ 2420] A Star not a Tree?

A Star not a Tree? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4058   Accepted: 2005 Description Luke wants to upgrade his home computer network from 10mbs to 100mbs. His existing network uses 10base2 (coaxial) cables that allow you

【POJ 2420】A Star not a Tree?

A Star not a Tree? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3929   Accepted: 1952 Description Luke wants to upgrade his home computer network from 10mbs to 100mbs. His existing network uses 10base2 (coaxial) cables that allow you