2013
11-11

# 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.

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 consists of one number, the total length of the cable segments, rounded to the nearest mm.

4
0 0
0 10000
10000 10000
10000 0


28284


//* @author: ccQ.SuperSupper
import java.io.*;
import java.util.*;
import java.math.*;
class Point{
double x,y;
}
public class Main {
static int n;
static final int N = 100+10;
static Point point[] = new Point[N];

static void start(){
for(int i=0;i< N;++i)
point[i] = new Point();
}

public static void main(String[]args) throws Exception{
int i;
double min;

start();
while(true){
cin.nextToken();
n = (int)cin.nval;
if(n<=0) break;
for(i=0;i< n;++i){
cin.nextToken();
point[i].x = cin.nval;
cin.nextToken();
point[i].y = cin.nval;
}
min = best(point[n/2].x,point[n/2].y,n);
System.out.printf("%.0f\n", min);
}
}

static double f(double a,double b,double n){
int i;
double t = 0;
for(i=0;i< n;++i){
t+=Math.sqrt((point[i].x-a)*(point[i].x-a)+(point[i].y-b)*(point[i].y-b));
}
return t;
}

static double best(double a,double b,int n){
int i,j=0,w=0;
double T[] = new double[4],min;
T[0] = f(a,b+1,n);
T[1] = f(a+1,b,n);
T[2] = f(a-1,b,n);
T[3] = f(a,b-1,n);
min = f(a,b,n);
for(i=0;i< 4;++i){
if(T[i]< min){
w = 1;
j = i;
min = T[i];
}
}
if(w!=0){
if(j==0) min = best(a,b+1,n);
else if(j==1) min = best(a+1,b,n);
else if(j==2) min = best(a-1,b,n);
else if(j==3) min = best(a,b-1,n);
}
return min;
}

}