首页 > 其他 > 详细

BZOJ 1821 部落划分

时间:2016-06-03 12:40:27      阅读:236      评论:0      收藏:0      [点我收藏+]

kruskal.第k-1大的边。

其实prim会更快。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define maxv 1050
#define maxe 1005000
using namespace std; 
struct edge
{
    int u,v,w;
}e[maxe];
int n,k,tot=0,w[maxv],x[maxv],y[maxv],father[maxv],cnt=0;
bool cmp(edge x,edge y)
{
    return x.w<y.w;
}
void addedge(int u,int v)
{
    e[++tot].u=u;
    e[tot].v=v;
    e[tot].w=(x[u]-x[v])*(x[u]-x[v])+(y[u]-y[v])*(y[u]-y[v]);
}
int getfather(int x)
{
    if (x!=father[x])
        father[x]=getfather(father[x]);
    return father[x];
}
void kruskal()
{
    sort(e+1,e+tot+1,cmp);
    for (int i=1;i<=n;i++) father[i]=i;
    for (int i=1;i<=tot;i++)
    {
        int f1,f2;
        f1=getfather(e[i].u);f2=getfather(e[i].v);
        if (f1!=f2)
        {
            father[f1]=f2;
            w[++cnt]=e[i].w;
        }
        if (cnt==n-1) break;
    }
}
int main()
{
    scanf("%d%d",&n,&k);
    for (int i=1;i<=n;i++)
        scanf("%d%d",&x[i],&y[i]);
    for (int i=1;i<=n;i++)
        for (int j=i+1;j<=n;j++)
            addedge(i,j);
    kruskal();
    sort(w+1,w+cnt+1);
    printf("%.2lf\n",sqrt(w[n-k+1]));
    return 0;
}

 

BZOJ 1821 部落划分

原文:http://www.cnblogs.com/ziliuziliu/p/5555718.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!