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. 第13章 MySQL服务器的状态--高性能MySQL学习笔记

    13.1 系统变量 -- 服务器配置变量 MySQL通过SHOW VARIABLES  SQL命令显示许多系统变量. 13.2 状态变量--SHOW STATUS SHOW STATUS 命令会在一个 ...

  2. bzoj 2795 [Poi2012]A Horrible Poem hash+数论

    2795: [Poi2012]A Horrible Poem Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 640  Solved: 322[Subm ...

  3. 13.UiAutomator 辅助APK的使用

    一.在测试中弹出提示框 UiAutomator无法直接实现,,但是可以通过让辅助APK接受UiAutomator发送的指令信息来实现,使用am命令 实例: 1.新建辅助apk,用来接收Uiautoma ...

  4. Python基础之面向对象(进阶篇)

    面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使用(可以讲多函数中公用的变量封装到对象中) 对象,根据模板创建的实例(即:对象),实 ...

  5. 「Python-Django」django 实现将本地图片存入数据库,并能显示在web上

    1. 将图片存入数据库 关于数据库基本操作的学习,请参见这一篇博客:https://www.cnblogs.com/leejy/p/6745186.html 这里我默认,您已经会了基本操作,能在数据库 ...

  6. numpy数组中冒号和负号的含义

    numpy数组中":"和"-"的意义 觉得有用的话,欢迎一起讨论相互学习~Follow Me 在实际使用numpy时,我们常常会使用numpy数组的-1维度和& ...

  7. OpenCV---图像加载与保存

    一:获取图像的信息 什么是图像: 结构化存储的数据信息 图像属性: -通道数目 -高与宽 -像素数据 -位图深度 import cv2 as cv def get_image_info(image): ...

  8. POJ 3304 Segments 基础线段交判断

    LINK 题意:询问是否存在直线,使得所有线段在其上的投影拥有公共点 思路:如果投影拥有公共区域,那么从投影的公共区域作垂线,显然能够与所有线段相交,那么题目转换为询问是否存在直线与所有线段相交.判断 ...

  9. 周末发福利了!26个免费的HTML5模版

    本期文章我们为大家搜集了很多专业且高质量的HTML5模版,而且还是免费的呦.如果你对编码很熟悉,那么从这些网站里你可以学到很多新技能.来这些国际范的案例中挑选您喜欢的模版学习起来吧:) Zeences ...

  10. javascript小技巧之with()方法

    With()方法平时用得不多,本文用个小例子来学习一下.在这里记录.个人感觉还是很方便的. 有了 With 语句,在存取对象属性和方法时就不用重复指定参考对象,在 With 语句块中,凡是 JavaS ...