题目链接:

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. saltstack内置state模块file之managed

    managed管理一个模板文件,载入到各个节点并运行相应配置 salt.states.file.managed(name, source=None, source_hash='', user=None ...

  2. 移动Web开发技巧汇总(转)

    META相关 1. 添加到主屏后的标题(IOS) <meta name="apple-mobile-web-app-title" content="标题" ...

  3. 谷歌高管无意中透露Google Glass未获得成功的原因

    Google X高管Astro Teller在接受媒体采访时无意中透露了这款设备没有取得预期成绩的原因 最终我们发现,在他们生活的世界里,数字生活和即时物理生活根本无法融为一体. Teller提出的这 ...

  4. 将PHP 5.3.3 (cli)升级到PHP 5.6.31 (cli)

    centos默认系统安装的是php5.3 [root@sz-local1 scripts]# rpm -qa |grep phpphp-pdo-5.3.3-47.el6.x86_64php-mysql ...

  5. Android string.xml 显示特殊符号

    项目中要在string.xml 中显示特殊符号,如@号冒号等,直接写肯定不行啦..只能考虑使用ASCII码进行显示: 省略号 …@号 @:号 :空格   以下为常见的ASCII十进制交换编码: --& ...

  6. Unity3d监听手机暂停与退出事件

    做移动互联网类型的开放,很多情况得考虑移动设备的暂停与退出时,做某些数据操作或UI. 1,退出事件,Unity3d,InPut就包含了: Input.GetKey(KeyCode.Escape) .  ...

  7. 【BZOJ5018】[Snoi2017]英雄联盟 背包

    [BZOJ5018][Snoi2017]英雄联盟 Description 正在上大学的小皮球热爱英雄联盟这款游戏,而且打的很菜,被网友们戏称为「小学生」.现在,小皮球终于受不了网友们的嘲讽,决定变强了 ...

  8. 第三课 nodejs读取文件

    //引入文件操作模块var fs = require('fs'); //读取文件 使用 回调函数 utf-8编码读取 a.txt在当前文件目录fs.readFile('a.txt','UTF-8',f ...

  9. iOS 蓝牙开发之(CoreBlueTooth)

    CoreBlueTooth 简介: 可用于第三方的蓝牙交互设备 设备必须支持蓝牙4.0 iPhone的设备必须是4S或者更新 iPad设备必须是iPad mini或者更新 iOS的系统必须是iOS 6 ...

  10. bug-3——onload,onbeforeunload,Onunload的区别

    window.onload事件设置页面加载时执行的动作,即进入页面的时候执行的动作.   window.onunload已经从服务器上读到了需要加载的新的页面,在即将替换掉当前页面时调用 一般用于设置 ...