Arctic Network POJ - 2349 of Kruskal minimum spanning tree algorithm

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.

Input

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

Output

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.

Sample Input

1
2 4
0 100
0 300
0 600
150 750

Sample Output

212.13 

meaning of the questions: There are about 500 sites, sites can be directly connected to the signal receiver, you can also use a satellite connection, a satellite connection is not required but only the cost but can connect to both sites have satellite channels, signal receiver needs spend money to buy, and the radius of the signal receiver signals have different signal
input to the first behavioral testing the number of samples, each test sample has a first number of satellites behavior s channels, the number of sites p, p-down line, each row represents the coordinates (x, y) of a site, 0 <= x, y < = 10000;
output: in the case of Q can be connected to ensure that all sites, the number of the maximum radius of the need to purchase a signal receiver.

Ideas: First, you can see that this problem is equivalent to asking the maximum distance between the point and the point after the minimum spanning tree, but due to the presence of the satellite when the tree so the distance is not necessarily the maximum distance,
first of all we want between sites distance determined and placed in a container and then determine the minimum spanning tree in accordance with Kruskal's algorithm, then each of the branches of the tree is tree edge is traversed by the presence of the satellite can be deleted branches s-1 th , the remaining branches of the maximum distance that is the answer
to note that all need to be cleared in the container after each test;

Code:
  1 #include <cstdio>
  2 #include <fstream>
  3 #include <algorithm>
  4 #include <cmath>
  5 #include <deque>
  6 #include <vector>
  7 #include <queue>
  8 #include <string>
  9 #include <cstring>
 10 #include <map>
 11 #include <stack>
 12 #include <set>
 13 #include <sstream>
 14 #include <iostream>
 15 #define mod 998244353
 16 #define eps 1e-6
. 17  #define LL Long Long
 18 is  #define INF 0x3f3f3f3f
 . 19  the using  namespace STD;
 20 is  
21 is  // U represents a starting point, v represents the end point, cost expense represents 
22 is  struct Node
 23 is  {
 24      int U, v;
 25      Double cost;
 26 is  };
 27  // sort ascending 
28  BOOL CMP (Node a, Node B)
 29  {
 30      return a.cost < b.cost;
 31 is  }
 32  // information is stored edge 
33 is Vector <Node>VE;
 34 is  
35  // FA represents the current i is the furthest ancestor 
36  int FA [ 505 ];
 37 [  // initialize fa, they are beginning their ancestors 
38 is  void the init ( int qwq)
 39  {
 40      for ( int i = 0 ; I <= qwq; I ++ )
 41 is      {
 42 is          FA [I] = I;
 43 is      }
 44 is  }
 45  // find the furthest ancestor, simultaneously compressing the path 
46 is  int Find ( int X)
 47  {
 48      IF(FA [X] == X)
 49      {
 50          return X;
 51 is      }
 52 is      return FA [X] = Find (FA [X]);
 53 is  }
 54 is  // determines whether the same furthest ancestor 
55  BOOL Che ( int X, int Y)
 56 is  {
 57 is      return Find (X) == Find (Y);
 58  }
 59  // combined x, y, put them in the same family 
60  void Mer ( int X, int Y)
 61 is  {
 62 is      IF (!che(x,y)) 
 63     {
 64         fa[fa[x]]=fa[y];
 65     }
 66     return ;
 67 }
 68 int n,m;
 69 struct dian
 70 {
 71     double x,y;
 72 };
 73 dian di[505];
 74 vector<node> aggre;
 75 double dist(dian a,dian b)
 76 {
 77     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
 78 }
 79 int main()
 80 {
 81     scanf("%d",&n);
 82     while(n--)
 83     {
 84         int s,p;
 85         scanf("%d %d",&s,&p);
 86         //初始化
 87         init(p);
 88         for(int i=0;i<p;i++)
 89         {
 90             scanf("%lf %lf",&di[i].x,&DI [I] .y);
 91 is          }
 92          Node NO;
 93          // distance between the location information into the dots to the site and placed in a container 
94          for ( int I = 0 ; I <p- . 1 ; I ++ )
 95          {
 96              for ( int J = I + . 1 ; J <P; J ++ )
 97              {
 98                  no.u = I;
 99                  No. V = J;
 100                  no.cost = dist (DI [I], DI [ J]);
 101                  ve.push_back (NO);
 102             }
 103          }
 104          // Sort 
105          Sort (ve.begin (), ve.end (), CMP);
 106          // algorithm derived tree branches and placed in another container 
107          for ( int I = 0 ; I <ve.size (); I ++ )
 108          {
 109              IF (! Che (VE [I] .u, VE [I] .v))
 110              {
 111                  Mer (VE [I] .u, VE [I] .v );
 112                  NO = VE [I];
 113                  aggre.push_back (NO);
 114              }
 115          }
 1 16          //Sort 
117          Sort (aggre.begin (), aggre.end (), CMP);
 1 18          int ANS = aggre.size () - . 1 ;
 119          // small to large, minus branches satellite links, 
120          IF (S - . 1 > 0 )
 121          {
 122              ANS - = (S- . 1 );
 123          }
 124          the printf ( " % .2lf \ n- " , AGGRE [ANS] .cost);
 125          // clear the contents of the container 
126          ve.clear ( );
 127          aggre.clear ();
 128      }
 129 }

 

Guess you like

Origin www.cnblogs.com/mzchuan/p/11736532.html