首页 > 专题系列 > Java解POJ > POJ 2560 Freckles [解题报告] Java
2013
11-11

POJ 2560 Freckles [解题报告] Java

Freckles

问题描述 :

In an episode of the Dick Van Dyke show, little Richie connects the freckles on his Dad’s back to form a picture of the Liberty Bell. Alas, one of the freckles turns out to be a scar, so his Ripley’s engagement falls through.

Consider Dick’s back to be a plane with freckles at various (x,y) locations. Your job is to tell Richie how to connect the dots so as to minimize the amount of ink used. Richie connects the dots by drawing straight lines between pairs, possibly lifting the pen between lines. When Richie is done there must be a sequence of connected lines from any freckle to any other freckle.

输入:

The first line contains 0 < n <= 100, the number of freckles on Dick's back. For each freckle, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the freckle.

输出:

Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the freckles.

样例输入:

3
1.0 1.0
2.0 2.0
2.0 4.0

样例输出:

3.41

解题代码:

/* @author: */
import java.util.Scanner;
import java.util.Arrays;
public class Main{
  static double INF=99999999.0;

 public static void main(String args[])
{
 Scanner sc=new Scanner(System.in);
  
  double p[][]=new double[101][101];
  double ax[]=new double[101];
  double ay[]=new double[101];
  double dis[]=new double[101];
  boolean used[]=new boolean[101];
	int n,i,j;
      n=sc.nextInt();
	for(i=0;i< n;i++){
          ax[i]=sc.nextDouble();
          ay[i]=sc.nextDouble();
        }
	for(i=0;i< n;i++)
		for(j=0;j< n;j++) p[i][j]=INF;
	for(i=0;i< n;i++)
	 for(j=i+1;j< n;j++)
	 {
	  double v=(ax[i]-ax[j])*(ax[i]-ax[j])+(ay[i]-ay[j])*(ay[i]-ay[j]);
	  v=Math.sqrt(v);
	  if(v< p[i][j])p[i][j]=p[j][i]=v;
	 }
	for(i=0;i< n;i++)
		dis[i]=p[0][i];
       Arrays.fill(used,false);
	dis[0]=0;
	used[0]=true;
	double ans=0;
	for(i=0;i< n-1;i++)
	{
	  double min=INF;
	  int tag=-1;
	  for(j=1;j< n;j++)
	  {
	    if(!used[j]&&dis[j]< min)
	    {
		min=dis[j];
		tag=j;
	     }
	    }
	    used[tag]=true;
	    ans+=min;
	    for(j=1;j< n;j++)
	    {
		if(!used[j]&&dis[j]>p[tag][j])
			dis[j]=p[tag][j];
	    }
	 }
	System.out.printf("%3.2f\n",ans);
    }
}

  1. #include <stdio.h>
    int main(void)
    {
    int arr[] = {10,20,30,40,50,60};
    int *p=arr;
    printf("%d,%d,",*p++,*++p);
    printf("%d,%d,%d",*p,*p++,*++p);
    return 0;
    }

    为什么是 20,20,50,40,50. 我觉得的应该是 20,20,40,40,50 . 谁能解释下?