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. 【分块,莫队】【P4396】【AHOI2013】作业

    传送门 Description 此时己是凌晨两点,刚刚做了Codeforces的小A掏出了英语试卷.英语作业其实不算多,一个小时刚好可以做完.然后是一个小时可以做完的数学作业,接下来是分别都是一个小时 ...

  2. Hdu1828 Picture

    Picture Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  3. css等比例分割父级容器(完美三等分)

    html部分代码: 方法一: 浮动布局+百分比 (将子元素依次左浮动,根据子元素的个数,设定每个子元素的宽度百分比) 方法二:行内元素(inline-block)+百分比 方法三: 父元素  disp ...

  4. Turkey HSD检验法/W法

    sklearn实战-乳腺癌细胞数据挖掘(博主亲自录视频) https://study.163.com/course/introduction.htm?courseId=1005269003&u ...

  5. 使用git拉取github上的项目

    一. 安装Git 去Git官网,下载安装包,一路点next,默认安装. 安装之后,在空白处右键,菜单显示有 Git GUI Here 和 Git Bash Here ,表示Git安装成功. 二. 配置 ...

  6. vee-validate

    http://vee-validate.logaretm.com/ 表单校验,配合VUE使用

  7. [USACO07MAR]黄金阵容均衡Gold Balanced L…

    https://www.luogu.org/problem/show?pid=1360 题目描述 Farmer John's N cows (1 ≤ N ≤ 100,000) share many s ...

  8. C11线程管理:线程创建

    1.线程的创建 C11创建线程非常简单,只需要提供线程函数就行,标准库提供线程库,并可以指定线程函数的参数. #include <iostream> #include <thread ...

  9. JAVA多线程提高十一:同步工具Exchanger

    Exchanger可以在对中对元素进行配对和交换的线程的同步点.每个线程将条目上的某个方法呈现给 exchange 方法,与伙伴线程进行匹配,并且在返回时接收其伙伴的对象.Exchanger 可能被视 ...

  10. logstash 收集 IIS 日志实践

    IIS日志示例: 2017-02-20 00:55:40 127.0.0.1 GET /MkWebAPI/swagger/ui/index - 80 - 127.0.0.1 Mozilla/5.0+( ...