首页 > 专题系列 > Java解POJ > POJ 2349 Arctic Network [解题报告] Java
2013
11-11

POJ 2349 Arctic Network [解题报告] Java

Arctic Network

问题描述 :

The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication technologies are to be used in establishing the network: every outpost will have a radio transceiver and some outposts will in addition have a satellite channel.

Any two outposts with a satellite channel can communicate via the satellite, regardless of their location. Otherwise, two outposts can communicate by radio only if the distance between them does not exceed D, which depends of the power of the transceivers. Higher power yields higher D but costs more. Due to purchasing and maintenance considerations, the transceivers at the outposts must be identical; that is, the value of D is the same for every pair of outposts.

Your job is to determine the minimum D required for the transceivers. There must be at least one communication path (direct or indirect) between every pair of outposts.

输入:

The first line of input contains N, the number of test cases. The first line of each test case contains 1 <= S <= 100, the number of satellite channels, and S < P <= 500, the number of outposts. P lines follow, giving the (x,y) coordinates of each outpost in km (coordinates are integers between 0 and 10,000).

输出:

For each case, output should consist of a single line giving the minimum D required to connect the network. Output should be specified to 2 decimal points.

样例输入:

1
2 4
0 100
0 300
0 600
150 750

样例输出:

212.13

解题代码:

//* @author: [email protected]
import java.io.*;
import java.util.Arrays;
public class Main
{
 static int[] p;
 public static void main(String[] args) throws NumberFormatException, IOException
 {
  InputStreamReader is=new InputStreamReader(System.in);
  BufferedReader in=new BufferedReader(is);
  int n=Integer.parseInt(in.readLine());
  while((n--)!=0)
  {
   String[] ss=in.readLine().split(" ");
   int a=Integer.parseInt(ss[0]);
   int b=Integer.parseInt(ss[1]);
   p=new int[b];
   int[] ax=new int[b];
   int[] ay=new int[b];
   mymy[] gg=new mymy[b*b/2];
   int ll=0;
   for(int i=0;i< b;i++)
    p[i]=i;
   for(int i=0;i< b;i++)
   {
    ss=in.readLine().split(" ");
    ax[i]=Integer.parseInt(ss[0]);
    ay[i]=Integer.parseInt(ss[1]);
   }
   for(int i=0;i< b;i++)
   {
    for(int j=i+1;j< b;j++)
     {
	int juli=(ax[i]-ax[j])*(ax[i]-ax[j])+(ay[i]-ay[j])*(ay[i]-ay[j]);
	gg[ll++]=new mymy(i,j,juli);
     }
    }
   Arrays.sort(gg,0,ll);
   int cnt=0;
   for(int i=0;i< ll;i++)
   {
    if(un(gg[i].x,gg[i].y))
     {
	cnt++;
	if(cnt==b-a)
	{
	  double hehe=Math.sqrt(gg[i].juli);
	  System.out.printf("%.2f\n",hehe);
	  break;
	}
      }
    }
  }
 }

  static int root(int x)
   {
   int b=x;
   while(p[b]!=b)
	b=p[b];
   return p[x]=b;
   }

 static boolean un(int a,int b)
 {
  int aa=root(a);
  int bb=root(b);
  if(aa==bb)return false;
  if(aa>bb)
   p[aa]=bb;
  else p[bb]=aa;
   return true;
 }

}
class mymy implements Comparable< mymy>
{
	int x,y,juli;
	public mymy(int xx,int yy,int j)
	{
		x=xx;y=yy;juli=j;
	}
	@Override
	public int compareTo(mymy o) {
		return juli-o.juli;
	}
}

  1. Good task for the group. Hold it up for every yeara??s winner. This is a excellent oppotunity for a lot more enhancement. Indeed, obtaining far better and much better is constantly the crucial. Just like my pal suggests on the truth about ab muscles, he just keeps obtaining much better.

  2. “再把所有不和该节点相邻的节点着相同的颜色”,程序中没有进行不和该节点相邻的其他节点是否相邻进行判断。再说求出来的也不一样是颜色数最少的

  3. Thanks for using the time to examine this, I truly feel strongly about it and enjoy finding out far more on this subject matter. If achievable, as you achieve knowledge