模拟退火算法A Star not a Tree?(poj2420)
http://write.blog.csdn.net/postedit
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3751 | Accepted: 1858 |
Description
problem in order to minimize the total cable length.
Unfortunately, Luke cannot use his existing cabling. The 100mbs system uses 100baseT (twisted pair) cables. Each 100baseT cable connects only two devices: either two network cards or a network card and a hub. (A hub is an electronic device that interconnects
several cables.) Luke has a choice: He can buy 2N-2 network cards and connect his N computers together by inserting one or more cards into each computer and connecting them all together. Or he can buy N network cards and a hub and connect each of his N computers
to the hub. The first approach would require that Luke configure his operating system to forward network traffic. However, with the installation of Winux 2007.2, Luke discovered that network forwarding no longer worked. He couldn't figure out how to re-enable
forwarding, and he had never heard of Prim or Kruskal, so he settled on the second approach: N network cards and a hub.
Luke lives in a loft and so is prepared to run the cables and place the hub anywhere. But he won't move his computers. He wants to minimize the total length of cable he must buy.
Input
Output
Sample Input
4
0 0
0 10000
10000 10000
10000 0
Sample Output
28284
题意:给出n个电脑的坐标,然后找出一个hub的位置,使hub到每个电脑的距离之和最小,输出最小值;
程序:
方法一:
#include"string.h"
#include"stdio.h"
#include"queue"
#include"stack"
#include"vector"
#include"algorithm"
#include"iostream"
#include"math.h"
#include"stdlib.h"
#define M 222
#define inf 100000000000
#define eps 1e-10
#define PI acos(-1.0)
using namespace std;
struct node
{
double x,y,dis;
}p[M],q[M];
int n;
double X1,X2,Y1,Y2;
double min(double a,double b)
{
return a<b?a:b;
}
double max(double a,double b)
{
return a>b?a:b;
}
double pow(double x)
{
return x*x;
}
double len(double x1,double y1,double x,double y)
{
return sqrt(pow(x1-x)+pow(y1-y));
}
double fun(double x,double y)
{
double ans=0;
for(int i=1;i<=n;i++)
ans+=len(x,y,p[i].x,p[i].y);
return ans;
}
void solve()
{
int i,j,po=20,est=25;
for(i=1;i<=po;i++)
{
q[i].x=(rand()%1000+10)/1000.0*(X2-X1)+X1;
q[i].y=(rand()%1000+10)/1000.0*(Y2-Y1)+Y1;
q[i].dis=fun(q[i].x,q[i].y);
}
double temp=len(X1,Y1,X2,Y2);
while(temp>eps)
{
for(i=1;i<=po;i++)
{
for(j=1;j<=est;j++)
{
double rad=(rand()%1000+10)/1000.0*PI*10;
node now;
now.x=q[i].x+temp*cos(rad);
now.y=q[i].y+temp*sin(rad);
if(now.x<0||now.y<0||now.x>10000||now.y>10000)continue;
now.dis=fun(now.x,now.y);
if(now.dis<q[i].dis)
q[i]=now;
}
}
temp*=0.9;
}
int id=1;
for(i=1;i<=po;i++)
{
if(q[i].dis<q[id].dis)
id=i;
}
printf("%.0lf\n",q[id].dis); }
int main()
{
int i;
while(scanf("%d",&n)!=-1)
{
X1=Y1=10000;
X2=Y2=0;
for(i=1;i<=n;i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
X1=min(X1,p[i].x);
X2=max(X2,p[i].x);
Y1=min(Y1,p[i].y);
Y2=max(Y2,p[i].y);
}
solve();
}
}
方法二:
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"algorithm"
#include"math.h"
#include"vector"
#include"queue"
#include"map"
#include"string"
#define M 10009
#define Maxm 10000
#define INF 10000000000000000LL
#define inf 100000000
#define eps 1e-5
#define pps 1e-8
#define PI acos(-1.0)
#define LL __int64
using namespace std;
struct node
{
double x,y;
node(){}
node(double xx,double yy){x=xx;y=yy;}
node operator-(node a)
{
return node(x-a.x,y-a.y);
}
node operator+(node a)
{
return node(x+a.x,y+a.y);
}
double operator ^(node a)
{
return x*a.y-y*a.x;
}
double operator *(node a)
{
return x*a.x+y*a.y;
}
}p[M];
int n;
double maxi;
node ret;
double len(node a)
{
return sqrt(a*a);
}
double dis(node a,node b)
{
return len(b-a);
}
double cross(node a,node b,node c)
{
return (b-a)^(c-a);
}
double fun(node q)
{
double sum=0;
for(int i=1;i<=n;i++)
sum+=dis(q,p[i]);
if(maxi>sum)
{
maxi=sum;
ret=q;
}
return sum;
}
void SA()
{
double temp=10000.0;
node now=ret;
maxi=INF;
while(temp>0.0001)
{
double rad=(rand()%1000)/1000.0*PI*10;
node cur;
cur.x=now.x+temp*cos(rad);
cur.y=now.y+temp*sin(rad);
double pe=fun(now)-fun(cur);
if(pe>0)
now=cur;
temp*=0.98;
}
for(int i=1;i<=1000;i++)
{
double rad=(rand()%1000)/1000.0*PI*10;
node cur;
cur.x=now.x+temp*cos(rad);
cur.y=now.y+temp*sin(rad);
fun(cur);
}
printf("%.0lf\n",fun(ret));
}
int main()
{
int i;
while(scanf("%d",&n)!=-1)
{
ret=node(0,0);
for(i=1;i<=n;i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
ret.x+=p[i].x;
ret.y+=p[i].y;
}
ret.x/=n;
ret.y/=n;
SA();
}
return 0;
}
模拟退火算法A Star not a Tree?(poj2420)的更多相关文章
- [模拟退火][UVA10228] A Star not a Tree?
好的,在h^ovny的安利下做了此题 模拟退火中的大水题,想当年联赛的时候都差点打了退火,正解貌似是三分套三分,我记得上一道三分套三分的题我就是退火水过去的... 貌似B班在讲退火这个大玄学... 这 ...
- poj-2420 A Star not a Tree?(模拟退火算法)
题目链接: A Star not a Tree? Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5219 Accepte ...
- poj2420 A Star not a Tree? 找费马点 模拟退火
题目传送门 题目大意: 给出100个二维平面上的点,让你找到一个新的点,使这个点到其他所有点的距离总和最小. 思路: 模拟退火模板题,我也不懂为什么,而且一个很有意思的点,就是初始点如果是按照我的代码 ...
- uva 10228 - Star not a Tree?(模拟退火)
题目链接:uva 10228 - Star not a Tree? 题目大意:给定若干个点,求费马点(距离全部点的距离和最小的点) 解题思路:模拟退火算法,每次向周围尝试性的移动步长,假设发现更长处, ...
- POJ 2420 A Star not a Tree? 爬山算法
B - A Star not a Tree? Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/co ...
- 初探 模拟退火算法 POJ2420 HDU1109
模拟退火算法来源于固体退火原理,更多的化学物理公式等等这里不再废话,我们直接这么来看 模拟退火算法简而言之就是一种暴力搜索算法,用来在一定概率下查找全局最优解 找的过程和固体退火原理有所联系,一般来讲 ...
- POJ 2420:A Star not a Tree?
原文链接:https://www.dreamwings.cn/poj2420/2838.html A Star not a Tree? Time Limit: 1000MS Memory Limi ...
- [POJ 2420] A Star not a Tree?
A Star not a Tree? Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4058 Accepted: 200 ...
- POJ 2420 A Star not a Tree? (计算几何-费马点)
A Star not a Tree? Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3435 Accepted: 172 ...
随机推荐
- mysql 修改用户权限,允许远程连接数据库
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'admin123' WITH GRANT OPTION; flush privileg ...
- 10 个强大的JavaScript / jQuery 模板引擎推荐
模板引擎是为了使用户界面与业务数据(内容)分离而产生的,它可以生成特定格式的文档.由于在开发过程中,网站或应用程序的界面与数据实现分离,大大提升了开发效率,良好的设计也使得代码重用变得更加容易. 本文 ...
- Message Code 【27796】 Failed to connect to server 'hostname';port_ld': 'reason'.
Message Code [27796] Failed to connect to server 'hostname';port_ld': 'reason'.Unable to connect to ...
- tensorflow中slim模块api介绍
tensorflow中slim模块api介绍 翻译 2017年08月29日 20:13:35 http://blog.csdn.net/guvcolie/article/details/77686 ...
- mips cfe命令
设置串口参数 setenv -p LINUX_CMDLINE "console=ttyS0,115200 root=mtd4 rw rootfstype=jffs2" 内核启动参数 ...
- Intellij IDEA:maven的本地仓库问题
不知是否我个人的问题,Intellij IDEA中设置的 maven本地仓库的位置 经常失效,动辄变回默认的路径(~/.m2/repository),然后疯狂下载内容. 很抓狂! 今天认真思考了一番, ...
- 以下哪个Hibernate主键生成策略是实现主键按数值顺序递增的?
A.increment B.identity C.sequence D.native 解答:A
- linux -- 查看Ubuntu命令行调用的文件
which 如: 输入:which reboot 输出:/sbin/reboot 输入:which shutdown -h now 输出:/sbin/shutdown
- 格局中@null的代码实现方式
布局中通常会用到@null.如RadioButton常用的技巧通过RadioGroup实现Tab,需要设置android:button="@null".如果要在代码中动态创建控件, ...
- 【Java面试题】39 Set里的元素是不能重复的,那么用什么方法来区分重复与否呢? 是用==还是equals()? 它们有何区别?
1.什么是Set?(what) Set是Collection容器的一个子接口,它不允许出现重复元素,当然也只允许有一个null对象. 2.如何来区分重复与否呢?(how) “ 用 iterator() ...