bzoj2429- 聪明的猴子
题意其实就是说有很多个点,求一组边把它们都连接起来,并且最大的那条边最小。很明显这就是一个最小生成树,是一颗保证最长边最短的树。
代码
刚刚学了个Borůvka算法,于是写了两个。
Borůvka
#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
#include<cmath>
#define Pow(x) ((x)*(x))
using namespace std;
int read() {int x=0,f=1;char c=getchar();for (;!isdigit(c);c=getchar()) if (c=='-') f=-1;for (;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;
}
const int maxh=505;
const int maxn=1e3+10;
const int maxm=maxn*maxn;
int jp[maxh],n,m,all,f[maxn],close[maxn];
struct node {double x,y;
} a[maxn];
struct bian {int u,v;double w;
} e[maxm];
double dist(node &a,node &b) {return sqrt(Pow(a.x-b.x)+Pow(a.y-b.y));
}
int find(int x) {return f[x]==x?x:f[x]=find(f[x]);
}
double boruvka() {for (int i=1;i<=n;++i) f[i]=i;e[0].w=1e300;double ret=0;for (int t=n;t>1;) {memset(close,0,sizeof close);for (int i=1;i<=all;++i) if (find(e[i].u)!=find(e[i].v)) {int fx=find(e[i].u),fy=find(e[i].v);if (e[i].w<e[close[fx]].w) close[fx]=i;if (e[i].w<e[close[fy]].w) close[fy]=i;}for (int i=1;i<=n;++i) if (find(i)==i && close[i]) {int x=find(e[close[i]].u),y=find(e[close[i]].v);if (x!=y) f[x]=y,ret=max(ret,e[close[i]].w),--t;}}return ret;
}
int main() {
#ifndef ONLINE_JUDGEfreopen("test.in","r",stdin);freopen("my.out","w",stdout);
#endifm=read();for (int i=1;i<=m;++i) jp[i]=read();n=read(),all=0;for (int i=1;i<=n;++i) a[i].x=read(),a[i].y=read();for (int i=1;i<=n;++i) for (int j=i+1;j<=n;++j) e[++all]=(bian){i,j,dist(a[i],a[j])};double mst=boruvka();int ans=0;for (int i=1;i<=m;++i) ans+=(jp[i]>=mst);printf("%d\n",ans);return 0;
}
Kruskal
#include<cstdio>
#include<cctype>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxh=505;
const int maxn=1e3+10;
const int maxm=maxn*maxn;
struct bian {int u,v;double w;inline bool operator < (const bian &a) const {return w<a.w;}
} e[maxm];
struct P {double x,y;
} a[maxn];
double jp[maxn];
int f[maxn];
int find(int x) {return f[x]==x?f[x]:f[x]=find(f[x]);
}
double dist(P &a,P &b) {return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int main() {
#ifndef ONLINE_JUDGEfreopen("test.in","r",stdin);freopen("std.out","w",stdout);
#endifint n,m;scanf("%d",&m);for (int i=1;i<=m;++i) scanf("%lf",jp+i);scanf("%d",&n);for (int i=1;i<=n;++i) scanf("%lf%lf",&a[i].x,&a[i].y);int all=0;double mst=0;for (int i=1;i<=n;++i) for (int j=i+1;j<=n;++j) e[++all]=(bian){i,j,dist(a[i],a[j])};sort(e+1,e+all+1);for (int i=1;i<=n;++i) f[i]=i;for (int i=1,j=0;i<=all && j<n;++i) {int u=e[i].u,v=e[i].v,fx=find(u),fy=find(v);double w=e[i].w;if (fx!=fy) {f[fx]=fy;mst=max(mst,w);++j;}}int ans=0;for (int i=1;i<=m;++i) if (jp[i]>=mst) ++ans;printf("%d\n",ans);return 0;
}
转载于:https://www.cnblogs.com/owenyu/p/6861071.html
bzoj2429- 聪明的猴子相关推荐
- BZOJ 2429: [HAOI2006]聪明的猴子【最小生成树】
2429: [HAOI2006]聪明的猴子 Time Limit: 10 Sec Memory Limit: 128 MB Description 在一个热带雨林中生存着一群猴子,它们以树上的果子为生 ...
- bzoj 2429: [HAOI2006]聪明的猴子(最小生成树)
2429: [HAOI2006]聪明的猴子 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 1570 Solved: 902 [Submit][St ...
- P2504 [HAOI2006]聪明的猴子 题解
P2504 [HAOI2006]聪明的猴子 题解 题目 链接 字面描述 题目描述 输入格式 输出格式 样例 #1 样例输入 #1 样例输出 #1 提示 思路 代码实现 题目 链接 https://ww ...
- [BZOJ2429][HAOI2006]聪明的猴子(最小生成树)
性质:最小生成树上任意两点间的最大边权,一定是这两点间所有路径的最大边权中最小的.证明显然. 1 #include<cstdio> 2 #include<cstring> 3 ...
- 洛谷p2504 HAOI2006 聪明的猴子
[题目描述] 在一个热带雨林中生存着一群猴子,它们以树上的果子为生.昨天下了一场大雨,现在雨过天晴,但整个雨林的地表还是被大水淹没着, 猴子不会游泳,但跳跃能力比较强,它们仍然可以在露出水面的部分植物 ...
- 有多少游客被峨眉山的猴子亲过脸?
来源|不相及研究所 ID|buuuxiangji 作者|发财金刚 不少游客都是去过峨眉山之后,才发现网上的传言是真的,那里的灵猴比山顶的普贤能更快还愿. 单身的去祈求姻缘,还没等下山,初吻就没了. 即 ...
- python猴子分桃问题_用python实现【五猴分桃】问题
转载链接:https://blog.csdn.net/cy309173854/article/details/78296839 据说"五猴分桃"问题最先是由大物理学家狄拉克提出来的 ...
- 用ChatGPT讲一个关于猴子的故事
今天让ChatGPT编了一个小猴子的故事,效果还不错. 首先设置目标: Goal 1: 讲一个关于猴子的故事 Goal 2: 童话故事 Goal 3: 寓言 Goal 4: 讲给小朋友听的 Goal ...
- python猴子分桃_用python实现【五猴分桃】问题
转载链接:https://blog.csdn.net/cy309173854/article/details/78296839 据说"五猴分桃"问题最先是由大物理学家狄拉克提出来的 ...
- 2019.4.summary
2019.4.1 BZOJ1061: [Noi2008]志愿者招募 真心有点难QAQ https://www.byvoid.com/zhs/blog/noi-2008-employee 看void爷的 ...
最新文章
- python bindings_OpenCV-Python Bindings 如何工作 | 六十四
- jQuery日期和时间插件(jquery-ui-timepicker-addon.js)中文破解版使用
- 20145303 《信息安全系统设计基础》第3周学习总结
- Seek the Name, Seek the Fame POJ - 2752 (理解KMP函数的失配)既是S的前缀又是S的后缀的子串
- 【HYSBZ - 1192】鬼谷子的钱袋(水题,二进制)
- 科目三道路考试技巧流程详解
- ACM PKU 2559 Largest Rectangle in a Histogram http://acm.pku.edu.cn/JudgeOnline/problem?id=2559
- safari 浏览器提示添加到主屏幕_Safari浏览器的几个小技巧你掌握了吗?
- 阿里面试官必问21 个刁钻的HashMap 面试题,这次让你彻底搞懂
- Spark性能优化指南——高级篇【2】
- HTML入门标签汇总
- windows 7 下,如何统计某文件夹下 视频总时长
- vue,下载导出文件,开新窗口导出
- 落花响应html,史上最“脑残”的“抢火车票”程序(node.js版) - 落花落雨不落叶...
- 企业如何选择物联网卡以及流量套餐?
- 东北大学软件学院操作系统v实验报告
- layui富文本编辑器的使用
- ESP8266 CH420
- Failed to shutdown DBConsole Gracefully
- python基于sklearn的SVM和留一法(LOOCV)进行二分类