本文主要是介绍BZOJ 1821 [JSOI2010]Group 部落划分 Group 题解与分析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1821: [JSOI2010]Group 部落划分 Group
Submit: 825 Solved: 386
![](http://www.lydsy.com/JudgeOnline/images/1821.jpg)
Input
Output
Sample Input
0 0
0 1
1 1
1 0
Sample Output
HINT
Source
JSOI2010第二轮Contest1
【分析】:
将各点间连一条边,对这些边从小到大排序,然后将前N-K条边归为一个集合<加边的条件为边的起始点不在同一集合>,保证它们不参与答案贡献,剩下K个直接单独放同一集合,这样的贪心就保证了答案尽可能的大
【代码】:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
using namespace std;
#define MAX 1001
#define MAXM 1000001
struct POINT{int x,y;};
struct EDGE{double v;int f,t;};
POINT a[MAX];
EDGE b[MAXM];
int N,K,tot=0,now=0,fa[MAX];
double dist(int x,int y)
{
double x1=(double)a[x].x*1.0,y1=(double)a[x].y*1.0,x2=(double)a[y].x*1.0,y2=(double)a[y].y*1.0;
return (sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)));
}
bool cmp(EDGE x,EDGE y){return x.v<y.v;}
int get(int x){return (fa[x]==x ? x : (fa[x]=get(fa[x])));}
int main()
{
//freopen("input.in","r",stdin);
//freopen("output.out","w",stdout);
scanf("%d%d",&N,&K);
for(int i=1;i<=N;i++)
scanf("%d%d",&a[i].x,&a[i].y);
for(int i=1;i<=N;i++)
fa[i]=i;
//now=N;
for(int i=1;i<=N;i++)
for(int j=i+1;j<=N;j++)
{
b[++tot].f=i;
b[tot].t=j;
b[tot].v=dist(i,j);
}
sort(b+1,b+1+tot,cmp);
for(int i=1;i<=tot;i++)
{
if(get(b[i].f)!=get(b[i].t))
{
fa[get(b[i].f)]=get(b[i].t);
now++;
}
if(now==N-K+1)
{
printf("%.2lf\n",b[i].v);
return 0;
}
}
//system("pause");
return 0;
}
转载注明出处:http://blog.csdn.net/u011400953
这篇关于BZOJ 1821 [JSOI2010]Group 部落划分 Group 题解与分析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!