Test instructions
There are a total of n villages in a certain area, the coordinates of each village are represented by a pair of integers (x, y), and now a communication network is established between villages.
There are two types of communication tools, namely the common line and the wireless communication satellite equipment that need to be laid.
Only the K villages are equipped with satellite equipment, and the villages with satellite equipment communicate directly with each other.
Communication can also be made between villages that have paved lines. However, for technical reasons, the length of the line between two villages cannot exceed D, otherwise the communication will be unreliable because of signal attenuation. Increasing the D value will result in more devices (costs) being invested.
The coordinates of all villages are known (X, y), number of satellite devices K.
Q: How can I allocate satellite equipment to allow direct or indirect communication between villages, and the value of D is minimized? Find the minimum value of D.
Data size: 0 <= k <= n<= 500
Ideas:
Simply find a minimum of D, so that the number of connected branches is less than or equal to the number of satellite devices. Consider the whole problem as a complete picture, just the minimum spanning tree on the graph, and the minimum value of D is the K long edge. Because: the longest k-1 in the smallest spanning tree is removed, the original tree is divided into K-connected branches, and a satellite device can be placed on each connected branch.
Realize:
1#include <iostream>2#include <cstdio>3#include <vector>4#include <algorithm>5#include <cmath>6 using namespacestd;7 structnode8 {9 intx, y;Ten }; One structEdge A { - intA, B; - DoubleCost ; the }; -Vector<node>NS; -Vector<edge>es; - intran[505]; + intpar[505]; - intT, K, n, x, y; + A intSquareintx) at { - returnX *x; - } - - BOOLCMP1 (Const Double& X,Const Double&y) - { in returnX >y; - } to + voidInitintN) - { the for(inti =0; I < n; i++) * { $Par[i] =i;Panax NotoginsengRan[i] =0; - } the } + intFindintx) A { the if(Par[x] = =x) + returnx; - returnPAR[X] =find (Par[x]); $ } $ voidUniteintXinty) - { -x =find (x); they =find (y); - if(x = =y)Wuyi return; the if(Ran[x] <Ran[y]) - { WuPAR[X] =y; - } About Else $ { -Par[y] =x; - if(Ran[x] = =Ran[y]) - { ARAN[X] + +; + } the } - } $ BOOLSame (intXinty) the { the returnFind (x) = =find (y); the } the - BOOLCMP (Edge A, edge B) in { the returnA.cost <B.cost; the } About the voidKru (vector<Double> & Res,intm) the { the init (n); + sort (Es.begin (), Es.end (), CMP); - for(inti =0; I < m; i++) the {Bayi if(!Same (ES[I].A, es[i].b)) the { the Unite (ES[I].A, es[i].b); - Res.push_back (es[i].cost); - } the } the } the the intMain () - { theCIN >>T; the while(t--) the {94 ns.clear (); the es.clear (); theCin >> K >>N; the for(inti =0; I < n; i++)98 { AboutCIN >> x >>y; - node tmp;101tmp.x =x;102TMP.Y =y;103 Ns.push_back (TMP);104 } the for(inti =0; I < ns.size (); i++)106 {107 for(intj = i +1; J < Ns.size (); J + +)108 {109 Edge E; theE.A =i;111e.b =J; theE.cost = sqrt (square (ns[i].x-ns[j].x) + Square (NS[I].Y-ns[j].y));113 Es.push_back (e); theE.A =J; thee.b =i; the Es.push_back (e);117 }118 }119vector<Double>Res; - Kru (res, es.size ());121 sort (Res.begin (), Res.end (), CMP1);122printf"%.2f\n", Res[k-1]);123 }124 return 0; the}
Summarize:
And a second niche into a tree, a maximum spanning tree, etc.
poj2349 Arctic Network