A Star not a Tree? (模擬退火)

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

 

 

 

 

題解

隨機化貪心(模擬退火)

模擬退火就是一開始的隨機範圍較大,到後來不斷縮小隨機的範圍

對於這道題而言

我的思路就是先選一個點,然後以它做一個半徑爲L的圓

把這個圓上的100等分點取出來,找到代價最小的一個,走過去

然後把L除以2,再繼續這樣操作(爲什麼是除以2,因爲我想到了倍增逼近)

直到精度符合要求

這其實就是模擬退火

٩(๑>◡<๑)۶人生第一道模擬退火٩(๑>◡<๑)۶

代碼:(我也不知道模擬退火是不是我這麼寫的。。。)

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 105
const double eps=1e-12;
const double PI=0.0628318530717958647692528677;
int n;
double a[N][2];
double f(double x,double y)
{
	double sum=0;
	for(int i=1;i<=n;i++)
		sum+=sqrt((a[i][0]-x)*(a[i][0]-x)+(a[i][1]-y)*(a[i][1]-y));
	return sum;
}
int main()
{
	double mxx=-100000,mix=100000;
	double mxy=-100000,miy=100000;
	int i;double sita,x,y,nx,ny,L,tmp;
	scanf("%d",&n);
	for(i=1;i<=n;i++){
		scanf("%lf%lf",&a[i][0],&a[i][1]);
		mxx=max(mxx,a[i][0]);mix=min(mix,a[i][0]);
		mxy=max(mxy,a[i][1]);miy=min(miy,a[i][1]);
	}
	x=(mxx+mix)/2;y=(mxy+miy)/2;
	L=2*max(mxx-mix,mxy-miy);
	for(;L>=eps;L/=2){
		double mi=1000000000.0;
		for(i=0,sita=0;i<=100;i++,sita+=PI){
			tmp=f(x+L*cos(sita),y+L*sin(sita));
			if(mi>tmp){
				mi=tmp;
				nx=x+L*cos(sita);
				ny=y+L*sin(sita);
			}
		}
		x=nx;y=ny;
	}
	printf("%.0f",f(x,y));
	//printf("\n%.12f %.12f",x,y);
}

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章