首页 > 专题系列 > Java解POJ > POJ 3102 Bee Garden [解题报告] Java
2013
11-12

POJ 3102 Bee Garden [解题报告] Java

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.

  4. Hello Web Admin, I noticed that your On-Page SEO is is missing a few factors, for one you do not use all three H tags in your post, also I notice that you are not using bold or italics properly in your SEO optimization. On-Page SEO means more now than ever since the new Google update: Panda. No longer are backlinks and simply pinging or sending out a RSS feed the key to getting Google PageRank or Alexa Rankings, You now NEED On-Page SEO. So what is good On-Page SEO?First your keyword must appear in the title.Then it must appear in the URL.You have to optimize your keyword and make sure that it has a nice keyword density of 3-5% in your article with relevant LSI (Latent Semantic Indexing). Then you should spread all H1,H2,H3 tags in your article.Your Keyword should appear in your first paragraph and in the last sentence of the page. You should have relevant usage of Bold and italics of your keyword.There should be one internal link to a page on your blog and you should have one image with an alt tag that has your keyword….wait there's even more Now what if i told you there was a simple WordPress plugin that does all the On-Page SEO, and automatically for you? That's right AUTOMATICALLY, just watch this 4minute video for more information at.

  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