2013
11-12

Bee Garden

George’s father Ben is fond of bee breeding. He has got a bee garden near his country house and he likes to walk around the garden and gather honey from the hives.

Ben has n hives, located at various points of his garden. Some of them are connected to each other or Ben’s house by straight roads. Ben always walks along the roads, and he never moves from one road to another anywhere except at hives. Neither Ben’s house nor any hive is located on a road.

The roads are organized in such a way, that Ben has exactly one way to get from his house to any hive.

Each week Ben makes a roundtrip. He starts from his house and walks by the roads in such a way that he visits each hive at least once. George once came to see his father and noticed that his father takes the shortest path that allows him to visit each hive and return to his house.

He thought that his father is getting older, so Ben must be getting really tired with all that walking. He decided to build another straight road that would help his father to visit all the hives faster. That road must connect two hives or a hive and Ben’s house. Help George to build the road in such a way that his father needs to walk as short a path as possible to visit all the hives and return home.

Note that neither Ben’s house nor any hive must be located on the new road.

The first line of the input file contains n — the number of the hives (1 ≤ n ≤ 200).

Let us consider Ben’s garden located on a Cartesian plane in such a way that his house occupied the point (0, 0). The following n lines contain two integer numbers each — the coordinates of the hives. The coordinates do not exceed 10 000 by their absolute values, no two hives coincide, no hive is located at (0, 0). Let the hives be numbered from 1 to n.

The following n lines describe roads — each road is described by the numbers of hives it connects, Ben’s house is denoted by 0.

Output two numbers — the numbers of the hives to be connected by the road. Output 0 instead of hive’s number for Ben’s house.

If there is no straight road that shortens Ben’s roundtrip output “-1”.

3
1 0
1 1
0 1
0 2
1 2
1 3

0 3

/* @author: */
import java.util.*;
import java.io.*;
import java.lang.reflect.Array;

public class Main {
static double dis[][];
static int e[][];
static int [] x, y, en;

static double distance( int a, int b ) {
return Math.sqrt( (x[a]-x[b])*(x[a]-x[b]) +
(y[a]-y[b])*(y[a]-y[b]) );
}

static void dfs( int k, int f, double [] dis, double s ){
dis[k] = s;
int j;
for( int i=0; i< en[k]; i++ ) {
if( e[k][i] != f ) {
j = e[k][i];
dfs( j, k, dis, s+distance( k, j ) );
}
}
}

static public void main( String [] str ) throws Exception{
Scanner cin = new Scanner( System.in );
int n = cin.nextInt();
x = new int[n+1];
y = new int[n+1];
en = new int[n+1];
dis = new double[n+1][n+1];
e = new int[n+1][n+1];

for( int i=1; i<=n; i++ ) {
x[i] = cin.nextInt();
y[i] = cin.nextInt();
}

int a, b;
double t, ans = 0;
for( int i=1; i<=n; i++ ) {
a = cin.nextInt();
b = cin.nextInt();
e[a][en[a]++] = b;
e[b][en[b]++] = a;
}

for( int i=0; i<=n; i++ )
dfs( i, -1, dis[i], 0 );

a = -1;
b = -1;
for( int i=0; i<=n; i++ )
for( int j=i+1; j<=n; j++ ) {
int k;
for( k =0; k<=n; k++ )
if( k!= i && k!=j &&
(x[i]-x[k])*(y[j]-y[k])-(y[i]-y[k])*(x[j]-x[k]) == 0 &&
(x[i]-x[k])*(x[j]-x[k])+(y[i]-y[k])*(y[j]-y[k]) <= 0 )
break;
if( k <= n )
continue;
t = - dis[i][j] + distance( i, j );
if( t < ans ) {
ans = t;
a = i;
b = j;
}
}
System.out.println();

if( a < 0 )
System.out.println( -1 );
else
System.out.println( a+" "+b );
}
}

1. 有两个重复的话结果是正确的，但解法不够严谨，后面重复的覆盖掉前面的，由于题目数据限制也比较严，所以能提交通过。已更新算法

2. simple, however efficient. A lot of instances it is difficult to get that a??perfect balancea?? among usability and appearance. I must say that youa??ve done a exceptional task with this. Also, the blog masses quite fast for me on Web explore.

3. simple, however efficient. A lot of instances it is difficult to get that a??perfect balancea?? among usability and appearance. I must say that youa??ve done a exceptional task with this. Also, the blog masses quite fast for me on Web explore.

5. #!/usr/bin/env python
def cou(n):
arr =
i = 1
while(i<n):
arr.append(arr[i-1]+selfcount(i))
i+=1
return arr[n-1]

def selfcount(n):
count = 0
while(n):
if n%10 == 1:
count += 1
n /= 10
return count