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);
}