poj 2420(模拟退火)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6066 | Accepted: 2853 |
Description
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
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
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(模拟退火)的更多相关文章
- poj 2420 模拟退火法基础
/* 题意:给n个电脑,求一个点到这n个电脑的距离和最小. 模拟退火法:和poj1379的方法类似 因为坐标范围是0-10000 不妨把它看成是10000*10000的正方形来做 */ #includ ...
- POJ 1379 模拟退火
模拟退火算法,很久之前就写过一篇文章了.双倍经验题(POJ 2420) 题意: 在一个矩形区域内,求一个点的距离到所有点的距离最短的那个,最大. 这个题意,很像二分定义,但是毫无思路,也不能暴力枚举, ...
- 三分 POJ 2420 A Star not a Tree?
题目传送门 /* 题意:求费马点 三分:对x轴和y轴求极值,使到每个点的距离和最小 */ #include <cstdio> #include <algorithm> #inc ...
- poj 2420,模拟退火算法,费马点
题目链接:http://poj.org/problem?id=2420 题意:给n个点,找出一个点,使这个点到其他所有点的距离之和最小,也就是求费马点. 参考链接:http://www.cnblogs ...
- poj 2420 A Star not a Tree? —— 模拟退火
题目:http://poj.org/problem?id=2420 给出 n 个点的坐标,求费马点: 上模拟退火. 代码如下: #include<iostream> #include< ...
- poj 2420 A Star not a Tree?——模拟退火
题目:http://poj.org/problem?id=2420 精度设成1e-17,做三遍.ans设成double,最后再取整. #include<iostream> #include ...
- POJ 2420 A Star not a Tree?(模拟退火)
题目链接 居然1Y了,以前写的模拟退火很靠谱啊. #include <cstdio> #include <cstring> #include <string> #i ...
- [POJ 2420] A Star not a Tree?
A Star not a Tree? Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4058 Accepted: 200 ...
- POJ 2420 A Star not a Tree? (计算几何-费马点)
A Star not a Tree? Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3435 Accepted: 172 ...
随机推荐
- BZOJ5314 [Jsoi2018]潜入行动 【背包类树形dp】
题目链接 BZOJ5314 题解 设\(f[i][j][0|1][0|1]\)表示\(i\)为根的子树,用了\(j\)个监测器,\(i\)节点是否被控制,\(i\)节点是否放置的方案数 然后转移即可 ...
- windows提权基础大全
Not many people talk about serious Windows privilege escalation which is a shame. I think the reason ...
- php配置说明
1上传文件限制配置 post_max_size = 200M upload_max_file_size = 200M
- [ACM][2018南京预赛]Magical Girl Haze
一.题面 样例输入: 15 6 11 2 21 3 42 4 33 4 13 5 64 5 2 样例输出: 3 二.思路 关键词:分层BFS 考试时觉得题干意思很清晰——求可将k条边赋值为0的最短路. ...
- 洛谷 P1486 BZOJ 1503 NOI 2004 郁闷的出纳员 fhq treap
思路: 1. 此处的fhq treap的分裂是按照权值分裂然后插入的.将小于k的分为一棵子树,大于等于k的分为另一棵子树. 2. 删除的时候只要将大于等于min的分裂到以root为根的树中,另一部分不 ...
- Linux之Makefile20160707
说一下LINUX下的Makefile,直接根据实际碰到的Makefile进行解读: 当make的目标为all时,-C $(KDIR) 指明跳转到内核源码目录下读取那里的Makefile:M=$(PWD ...
- Codeforces Round #336 (Div. 2)B 暴力 C dp D 区间dp
B. Hamming Distance Sum time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Codeforces Round #546 (Div. 2) ABCDE 题解
1136A: 题意:一本书有n个章节,每个章节的分别在li到ri页,小明读完书后将书折在第k页,问还有多少章节没有读 题解:控制k在li~ri的范围内后输出n-i即可 #include <set ...
- linux 内存计算
原文: http://www.open-open.com/lib/view/open1424325362577.html Linux中的Cache Memory 什么是Cache Memory(缓存内 ...
- 洛谷 U3357 C2-走楼梯
https://www.luogu.org/problem/show?pid=U3357 题目背景 在你成功地解决了上一个问题之后,方方方不禁有些气恼,于是他在楼梯上跳来跳去,想要你求出他跳的方案数. ...