Bzoj 1821 [Jsoi2010]group Tribe Division (Kruskal)

Source: Internet
Author: User

Solution: At first didn't think how to write, but slowly feel that the distance between the same tribe is as short as possible, and then, like the picture in the title, the distance between the two tribes will be very long, this length is actually by the tribe inside a person to go out and another tribe of the distance. We run the smallest spanning tree directly, so that the distance of the people of their tribe is as short as possible, then this spanning tree the longest several sides inside, directly output the penultimate k-1 bar.

The code is as follows:

#include <iostream> #include <cstdio> #include <vector> #include <queue> #include <utility > #include <stack> #include <algorithm> #include <cstring> #include <string> #include <
cmath> #include <ctime> #include <set> using namespace std;
typedef pair<int, int> PII;
const int MAXN = 1e5 + 5;
const int MAXM = 1e6 + 5;
int N, K, PPP;
PII P[MAXN];

vector<double>len;
    struct Node {int u, v;
    Double C;
        BOOL operator < (CONST node& TMP) const {if (C < tmp.c) return 1;
    return 0;

}}EDGE[MAXM];
int PAR[MAXN];
    int find (int x) {int root, TMP = x;
    while (x!= par[x]) x = par[x];
    root = x;
    x = tmp;
        while (x!= par[x]) {tmp = par[x];
        PAR[X] = root;
    x = tmp;
return root;

} void Union (int x, int y) {par[y] = x;}
        void Kruskal () {for (int i = 0; i < PPP; i++) {int x = find (EDGE[I].U); InT y = find (EDGE[I].V);
            if (x!= y) {Union (x, y);
        Len.push_back (EDGE[I].C);
    Double Count (int i, int j) {int x1 = p[i].first, x2 = P[j].first;
    int y1 = p[i].second, y2 = P[j].second;
return sqrt (1.0 * (x1-x2) * (X1-X2) + (y1-y2) * (Y1-y2)); int main () {#ifndef Online_judge freopen ("D:\\in.txt", "R", stdin); #endif//Online_judge scanf ("%d%d" &
    n, &k);
        for (int i = 1, x, y; i <= n; i++) {scanf ("%d%d", &x, &y);
        P[i].first = x;
    P[i].second = y;
            for (int i = 1; I <= n; i++) {for (int j = i + 1; j <= N; j +) {Double d = Count (i, j);
            EDGE[PPP].U = i;
            EDGE[PPP].V = j;
            EDGE[PPP].C = D;
        ppp++;
    } sort (Edge, Edge + PPP);
    for (int j = 1; j < Maxn; J + +) par[j] = j;
    Kruskal ();
	printf ("%.2f\n", Len[len.size ()-K + 1]);
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.