题目链接:

A Star not a Tree?

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5219   Accepted: 2491

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
题目:求一个多边形的费马点到所有点的距离和,费马点是多边形内到顶点距离和最短的点;
思路:更poj1379一个人思路,用模拟退火算法找费马点求距离;
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctime>
#include <algorithm>
using namespace std;
const int num=;
const int cnt=;
const double PI=acos(-1.0);
const double inf=1e12;
struct node
{
double px,py,dis;
};
node ans[];
int n;
double x[],y[];
double get_dis(double a,double b,double c,double d)
{
return sqrt((c-a)*(c-a)+(d-b)*(d-b));
}
void Iint()
{
for(int i=;i<num;i++)
{
ans[i].px=1.0*(rand()%)/*;
ans[i].py=1.0*(rand()%)/*;
ans[i].dis=;
for(int j=;j<n;j++)
{
ans[i].dis+=get_dis(ans[i].px,ans[i].py,x[j],y[j]);
}
}
}
int main()
{
srand((unsigned)time(NULL));
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%lf%lf",&x[i],&y[i]);
}
Iint();
double temp=get_dis(,,,);
while(temp>0.2)
{
for(int i=;i<num;i++)
{
double fx,fy;
for(int j=;j<cnt;j++)
{
double cur=2.0*PI*(rand()%)/;
fx=ans[i].px+cos(cur)*temp;
fy=ans[i].py+sin(cur)*temp;
if(fx<||fx>||fy<||fy>)continue;
double dist=;
for(int k=;k<n;k++)
{
dist+=get_dis(fx,fy,x[k],y[k]);
}
if(dist<ans[i].dis)
{
ans[i].px=fx;
ans[i].py=fy;
ans[i].dis=dist;
}
}
}
temp*=0.8;
}
double ans_dis=inf;
int ans_x,ans_y;
for(int i=;i<num;i++)
{
if(ans[i].dis<ans_dis)
{
ans_dis=ans[i].dis;
}
}
printf("%.0lf\n",ans_dis);
return ;
}

poj-2420 A Star not a Tree?(模拟退火算法)的更多相关文章

  1. 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 ...

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

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

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

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

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

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

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

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

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

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

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

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

  8. POJ 2420 A Star not a Tree?【爬山法】

    题目大意:在二维平面上找出一个点,使它到所有给定点的距离和最小,距离定义为欧氏距离,求这个最小的距离和是多少(结果需要四舍五入)? 思路:如果不能加点,问所有点距离和的最小值那就是经典的MST,如果只 ...

  9. Poj2420 A Star not a Tree? 模拟退火算法

    题目链接:http://poj.org/problem?id=2420 题目大意:每组数据中给n个点(n<=100),求平面中一个点使得这个点到n个点的距离之和最小. 分析:一开始看到这个题想必 ...

随机推荐

  1. JSP指令用来设置整个JSP页面相关的属性

    JSP 指令 JSP指令用来设置整个JSP页面相关的属性,如网页的编码方式和脚本语言. 语法格式如下: <%@ directive attribute="value" %&g ...

  2. Linux进程间通信(四) - 共享内存

    共享内存的优势 采用共享内存通信的一个显而易见的好处是效率高,因为进程可以直接读写内存,而不需要任何数据的拷贝.对于像管道和消息队列等通信方式,则需要在内核和用户空间进行四次的数据拷贝,而共享内存则只 ...

  3. window下php5安装redis扩展 设置自启动服务

    最近想在5.6版本的开发环境装一下redis的扩展,结果找了半天都是失效链接,特此做下备份 5.3-5.6 https://pecl.php.net/package/redis/2.2.7/windo ...

  4. (总结)RHEL/CentOS 7.x的几点新改变

    一.CentOS的Services使用了systemd来代替sysvinit管理 1.systemd的服务管理程序: systemctl是主要的工具,它融合之前service和chkconfig的功能 ...

  5. 我的Android进阶之旅------>Android基于HTTP协议的多线程断点下载器的实现

    一.首先写这篇文章之前,要了解实现该Android多线程断点下载器的几个知识点 1.多线程下载的原理,如下图所示 注意:由于Android移动设备和PC机的处理器还是不能相比,所以开辟的子线程建议不要 ...

  6. Android Studio 1.1 使用介绍及导入 jar 包和第三方依赖库

    导入 jar 包 导入 jar 包的方式非常简单,就是在项目中的 libs 中放入你需要导入的 jar 包,然后右键你的 jar 文件,选择“add as a library”即可在你的项目中使用这个 ...

  7. java基础入门之数组排序冒泡法

    public class ArrayTest03{ /* Name:数组排序,冒泡法 Power by :Stuart Date:2015-4-23*/ public static void main ...

  8. $.ajax()方法详解(转)

    转: http://www.cnblogs.com/tylerdonet/p/3520862.html 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: ...

  9. HTTP基础概念讲解

    HTTP基础概念讲解 作者:Danbo 时间:2016-03-17 1.1.http协议头部:curl -I www.meituan.com 1.2.静态和动态 静态网页:纯HTML格式的网页,后台没 ...

  10. 每天一个Linux命令(6)rmdir命令

         rmdir命令用来删除空目录. 利用rmdir命令可以从一个目录中删除一个或多个空的子目录.该命令从一个目录中删除一个或多个子目录,其中dirname表示目录名.如果dirname中没有指定 ...