poj2420——求多边形费马点(随机化变步长贪心法即模拟退火)

题目链接:http://poj.org/problem?id=2420

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<=100)个点,你需要找到一个这样的点,使得这个点到N个点的距离之和尽可能小。输出这个最小的距离和(四舍五入到最近的整数)。

Input

第一行N,接下来N行每行两个整数,表示N个点

Output

一行一个正整数,最小的距离和。

 

经典的模拟退火例题,求多边形的费马点。

关于模拟退火的内容可以参考这几篇文章:https://blog.csdn.net/acdreamers/article/details/10019849

大致思路是先随机找一个点,然后进行上下左右的移动,朝着对决策最优的地方移动。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const double eps=1e-8;
const double INF=0x7ffffff;
const double delta=0.99;
const int T=100;
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};	
struct Point{
	double x,y;
	Point(double x=0,double y=0):x(x),y(y){}
}p[105];
int n;
double Dist(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 pp){
	double ans=0;
	for(int i=0;i<n;i++)
		ans+=Dist(pp,p[i]);
	return ans;
}
double Search(){
	Point s=p[0];
	double t=T;
	double ans=INF;
	while(t>eps){
		int flag=1;
		while(flag){
			flag=0;
			for(int i=0;i<4;i++){
				Point temp;
				temp.x=s.x+dir[i][0];
				temp.y=s.y+dir[i][1];
				double tmp=GetSum(temp);
				if(ans>tmp){
					flag=1;
					ans=tmp;
					s=temp;
				}
			}
		}
		t*=delta;
	}
	return ans;
} 
int main(){
	scanf("%d",&n);
	for(int i=0;i<n;i++)
		scanf("%lf%lf",&p[i].x,&p[i].y);
	printf("%.0f\n",Search()); 
	return 0;
}

 

上一篇:Linux环境搭建docker-selenium来运行UI自动化


下一篇:PyTorch Hub发布!一行代码调用所有模型:torch.hub