A Star not a Tree?
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6066   Accepted: 2853

Description

Luke wants to upgrade his home computer network from 10mbs to 100mbs. His existing network uses 10base2 (coaxial) cables that allow you to connect any number of computers together in a linear arrangement. Luke is particulary proud that he solved a nasty NP-complete 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

The
first line of input contains a positive integer N <= 100, the number
of computers. N lines follow; each gives the (x,y) coordinates (in mm.)
of a computer within the room. All coordinates are integers between 0
and 10,000.

Output

Output consists of one number, the total length of the cable segments, rounded to the nearest mm.

Sample Input

4
0 0
0 10000
10000 10000
10000 0

Sample Output

28284

在平面内找到一个点,到给出的n个点的距离最短.
题解:利用模拟退火找费马点。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#define Lim 0.999999
using namespace std;
const double eps = 1e-; ///温度下限
const double delta = 0.98;
const double T = ; ///初始温度
const double INF = ;
const int N = ;
struct Point{
double x,y;
}p[N];
int n;
int dir[][] = {{-,},{,},{,},{,-}};
double dis(Point a,Point b) ///距离
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double getSum(Point p[],Point s){
double ans = ;
for(int i=;i<n;i++){
ans += dis(p[i],s);
}
return ans;
}
double Search(Point p[]){
Point s = p[]; ///随机一个点
double res = INF;
double t = T;
while(t>eps){
bool flag = true;
while(flag){
flag = false;
for(int i=;i<;i++){
Point next;
next.x = s.x+dir[i][]*t;
next.y = s.y+dir[i][]*t;
double ans = getSum(p,next);
if(ans<res){
res = ans;
s = next;
flag = true;
}
}
}
t = t*delta;
}
return res;
}
int main(){
while(scanf("%d",&n)!=EOF){
for(int i=;i<n;i++){
scanf("%lf%lf",&p[i].x,&p[i].y);
}
printf("%.0lf\n",Search(p));
}
}
												

poj 2420(模拟退火)的更多相关文章

  1. poj 2420 模拟退火法基础

    /* 题意:给n个电脑,求一个点到这n个电脑的距离和最小. 模拟退火法:和poj1379的方法类似 因为坐标范围是0-10000 不妨把它看成是10000*10000的正方形来做 */ #includ ...

  2. POJ 1379 模拟退火

    模拟退火算法,很久之前就写过一篇文章了.双倍经验题(POJ 2420) 题意: 在一个矩形区域内,求一个点的距离到所有点的距离最短的那个,最大. 这个题意,很像二分定义,但是毫无思路,也不能暴力枚举, ...

  3. 三分 POJ 2420 A Star not a Tree?

    题目传送门 /* 题意:求费马点 三分:对x轴和y轴求极值,使到每个点的距离和最小 */ #include <cstdio> #include <algorithm> #inc ...

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

    题目链接:http://poj.org/problem?id=2420 题意:给n个点,找出一个点,使这个点到其他所有点的距离之和最小,也就是求费马点. 参考链接:http://www.cnblogs ...

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

    题目:http://poj.org/problem?id=2420 给出 n 个点的坐标,求费马点: 上模拟退火. 代码如下: #include<iostream> #include< ...

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

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

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

    题目链接 居然1Y了,以前写的模拟退火很靠谱啊. #include <cstdio> #include <cstring> #include <string> #i ...

  8. [POJ 2420] A Star not a Tree?

    A Star not a Tree? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4058   Accepted: 200 ...

  9. POJ 2420 A Star not a Tree? (计算几何-费马点)

    A Star not a Tree? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3435   Accepted: 172 ...

随机推荐

  1. 20165218 2017-2018-2 《Java程序设计》课程总结

    20165218 2017-2018-2 <Java程序设计>课程总结 一.每周作业链接汇总 20165218 我期望的师生关系 20165218 学习基础和C语言基础调查 2016521 ...

  2. git用户名和邮箱配置

    1. 设置全局用户名和邮箱 git config --global user.name "xxx" git config --global user.email "xxx ...

  3. struts2 文件下载的处理

  4. Spring MVC @PathVariable注解

    下面用代码来演示@PathVariable传参方式 @RequestMapping("/user/{id}") public String test(@PathVariable(& ...

  5. vs2003一查找就卡死了

    Visual Studio 2003一查找就卡死了.解决办法如下: win7 32位下 解决方法:找到VS2003的安装目录,修改"...\Microsoft Visual Studio . ...

  6. 由保存当前用户引发的springboot的测试方式postman/restlet还是swagger2

    今天在测试接口的到时候发现用springboot集成swagger2集成的项目,在测试session保存的当前用户的时候,发现执行不了,没有像postman或者restlet一样,保存这个接口的url ...

  7. iOS APNs实战分享

    序言: 因为App的功能需要,最近一直在调研苹果的APNs推送,开始时觉得超麻烦,现在感觉还是比较easy,“难者不会,会者不难”,自己踩过了这么多的坑终于会了,不出来吐槽(装X)一下对不起自己,23 ...

  8. SSH免密码登录,实现数据传输备份

    简单来说,就是通过ssh-keygen -t rsa命令来产生一组公私钥,私钥是id_rsa,公钥是id_rsa.pub,把公钥上传到另一台服务器对应账号的.ssh/authorized_keys,即 ...

  9. tf.train.batch的偶尔乱序问题

    tf.train.batch的偶尔乱序问题 觉得有用的话,欢迎一起讨论相互学习~Follow Me tf.train.batch的偶尔乱序问题 我们在通过tf.Reader读取文件后,都需要用batc ...

  10. Isomorphic JavaScript: The Future of Web Apps(译)

                                                                                                 Isomorp ...