"Bzoj" 1821: [Jsoi2010]group Tribe Division Group (minimum spanning tree + greedy)

Source: Internet
Author: User

http://www.lydsy.com:808/JudgeOnline/problem.php?id=1821

The naked question.

The shortest distance of the subject requires the longest, and it is obvious that we sort.

There is greed here, that is, we give the smallest of the edge to the interior of the N tribe, and then the rest of the smallest is the answer.

Divide the edge of the edge to the K tribe, use and check the set to generate the smallest tree, so that the inner edge is always less than the edge connected to the outside. Then the remaining K points, the remaining K points of the edge must be the smallest and longest side of the tribe.

#include <cstdio> #include <cstring> #include <string> #include <iostream> #include <cmath > #include <algorithm>using namespace std; #define REP (i, n) for (int i=0; i< (n); ++i) #define FOR1 (I,a,n) for (in T i= (a); i<= (n) ++i) #define FOR2 (i,a,n) for (int i= (a);i< (n), ++i) #define FOR3 (i,a,n) for (int i= (a); i>= (n);-I.) #define FOR4 (I,a,n) for (int i= (a);i> (n); i) #define CC (i,a) memset (i,a,sizeof (i)) #define MAX (A, B) ((a) > A):(b) #define MIN (a) < (b) ( A):(B)) #define READ (a) a=getnum () #define PRINT (a) printf ("%d", a) inline int getnum () {int ret=0; char c; int k=1; for (c=g Etchar (); c< ' 0 ' | | C> ' 9 '; C=getchar ()) if (c== '-') k=-1; for (; c>= ' 0 ' && c<= ' 9 '; C=getchar ()) ret=ret*10+c-' 0 '; return k*ret; }const int N=1005;int x[n], y[n];struct edge {int u, V, w;bool operator < (const edge &a) Const {return w<a.w; }}e[n*n];int cnt;int f[n];int ifind (const int &x) {return x==f[x]?x:f[x]=ifind (f[x]);} int main () {int n, M;read (n); Read (m); For1 (i, 1, N) read (X[i]), read (Y[i]), For1 (i, 1, N) f[i]=i;for1 (i, 1, N) for1 (j, 1, N) if (i!=j) e[cnt].u=i, e[cnt]. V=j, e[cnt++].w= (X[i]-x[j]) * (X[i]-x[j]) + (Y[i]-y[j]) * (Y[i]-y[j]); sort (e, e+cnt); int fu, Fv;for2 (i, 0, cnt) {Fu=ifind (E [I].U]; Fv=ifind (E[I].V), if (FU!=FV) {if (n>m)--n, F[fu]=fv;else {printf ("%.2lf\n", sqrt (E[I].W)); return 0;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.