水题大法好。。
裸MST直接做就好了。。5min1A赞。。
#include<bits/stdc++.h>
using namespace std;
int n,m,tot,cnt,ans,monkey[505],f[1005],x[1005],y[1005];
struct node {int x,y; double z;} e[1000005];
double now;
inline int read()
{
int a=0,f=1; char c=getchar();
while (c<'0'||c>'9') {if (c=='-') f=-1; c=getchar();}
while (c>='0'&&c<='9') {a=a*10+c-'0'; c=getchar();}
return a*f;
}
inline double calc(int i,int j)
{
return (double)(sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])));
}
inline bool cmp(node a,node b)
{
return a.z<b.z;
}
int find(int i)
{
return f[i]==i?i:f[i]=find(f[i]);
}
int main()
{
m=read();
for (int i=1;i<=m;i++) monkey[i]=read();
n=read();
for (int i=1;i<=n;i++) f[i]=i;
for (int i=1;i<=n;i++) x[i]=read(),y[i]=read();
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
if (i!=j) e[++cnt].x=i,e[cnt].y=j,e[cnt].z=calc(i,j);
sort(e+1,e+cnt+1,cmp);
for (int i=1;i<=cnt;i++)
{
int p=find(e[i].x),q=find(e[i].y);
if (p!=q)
{
f[p]=q;
tot++;
now=e[i].z;
if (tot==n-1) break;
}
}
for (int i=1;i<=m;i++) if ((double)monkey[i]>=now) ans++;
cout << ans;
return 0;
}
本文介绍了一种基于裸MST算法解决特定问题的方法。通过详细解释代码实现过程,包括节点结构定义、距离计算、排序及并查集使用等步骤,展示了如何快速有效地完成算法任务。
1274

被折叠的 条评论
为什么被折叠?



