Title Description
Cong research found that the Desert Island Savage always live a gregarious life, but not the whole island of all savages belong to the same tribe, the wild people always cliques form belong to their own tribe, different tribes are often fighting. It's just that it's all a mystery-Cong doesn't know how the tribe is distributed. But the good news is that Cong got a map of the desert island. The map marks the place where N Savages Live (which can be seen as coordinates on the plane). We know that savages of the same tribe always live nearby. We defined the distance of the two tribes as the distance between the two closest settlements in the tribe. Cong also got a meaningful message-the savages were divided into K-tribes in total! That's a good news. Cong hopes to dig out the details of all the tribes from this information. He was experimenting with an algorithm that could find the distance between two tribes for any one tribe division, and Cong hoped to find a way to divide the tribe so that the nearest two tribes could be kept as far away as possible. For example, the diagram on the left shows a good division, while the right is not. Please program to help Cong solve this problem.
Input
The first line contains two integers n and K (1< = n < = 1000,1< K < = n), respectively, representing the number of inhabitants of the savage and the number of tribes. next n rows, each line containing two positive integers x, y, describes the coordinates of a dwelling point (0 < =x, y < =10000)
Output
Output line, for the best division, the nearest two tribes distance, accurate to two digits after the decimal point.
Sample input
4 2
0 0
0 1
1 1
1 0
Sample output
1.00
Exercises
Greedy + and check set
Because the topic requires the nearest two points of the minimum distance, so we can first the distance of any two points from small to large order, and then determine whether two points can be added to the same tribe.
Use and check sets to maintain each tribe.
Since there are n points, K-connected blocks, and no ring, so the number of sides is n-k, then when the number of sides is already n-k, if we find two additional points, then this distance is the smallest answer.
#include <cstdio> #include <cmath> #include <algorithm>using namespace std;struct data{int x, Y, Z;} A[1000010];int dx[1010], dy[1010], CNT, F[1010];bool CMP (data A, data b) {return a.z < b.z;} int find (int x) {return x = = F[x]? x:f[x] = Find (F[x]);} int main () {int n, K, I, j, Cedge = 0, TX, ty;scanf ("%d%d", &n, &k), for (i = 1; I <= n; i + +) scanf ("%d %d ", &dx[i], &dy[i]); for (i = 1; I <= n; i + +) for (j = i + 1; j <= N; j + +) a[++cnt].x = i, a[cnt].y = J, a[cnt].z = (Dx[i]-dx[j]) * (Dx[i]-dx[j]) + (Dy[i]-dy[j]) * (Dy[i]-dy[j]); sort (A + 1, a + CNT + 1, CMP); for ( i = 1; I <= N; i + +) f[i] = i;for (i = 1; I <= cnt; i + +) {tx = find (a[i].x), Ty = Find (A[I].Y), if (tx! = Ty) {if (Cedge = = n-k) {Prin TF ("%.2lf\n", sqrt (a[i].z)); return 0;} else F[tx] = ty, Cedge + +;}} return 0;}
"bzoj1821" [Jsoi2010]group Tribe Division Group