求助dalao

P1991 无线通讯网

@[qiuhanlin](/user/691315) 跑一遍 Kruskal 就好了,附代码 O v O ```c #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<cmath> #include<ctime> #include<queue> #include<string> #include<bitset> #include<cctype> #include<cstdlib> #include<functional> #include<istream> #include<sstream> #include<streambuf> #define ll long long using namespace std; const int N=200000,inf=0x3f3f3f; struct edge { double head,to,w; bool operator < (const edge &x) const { return w<x.w; } }e[N]; int x[N],y[N],f[N],s,p,cnt=0,n=0; double ans=0; //bool cmp(edge a,edge b) //{ // return a.w<b.w; //} double dis(int i,int j) { return sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])); } int find1(int x) { return f[x]==x?x:f[x]=find1(f[x]); } void Kruskal() { for(int i=1;i<=p;i++) f[i]=i; sort(e+1,e+1+n); for(int i=1;i<=n;i++) { // printf("%d--%d--%.2lf\n",i,cnt,e[i].w); double x=e[i].head,y=e[i].to,z=e[i].w; int tx=find1(x),ty=find1(y); if(tx!=ty) { f[tx]=ty; ans=max(ans,z); cnt++; if(cnt>=p-s) break; } } } int main() { scanf("%d%d",&s,&p); for(int i=1;i<=p;i++) { scanf("%d%d",&x[i],&y[i]); for(int j=1;j<i;j++) { n++; e[n]={i,j,dis(i,j)}; } } Kruskal(); printf("%.2f",ans); return 0; } ```
by wunaidedanjuan @ 2023-08-27 17:14:27


回复必关!!!
by SNXL @ 2023-08-27 17:14:53


感谢, 已关
by SNXL @ 2023-08-27 17:15:39


不用那么长 ```cpp #include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <bitset> using namespace std; const long long maxn = 501; const long long maxm = 250001; long long fa[maxn]; long long find(long long x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } struct node { long long first, second; double value; }edge[maxm]; struct point { long long x, y; }p[maxn]; bool operator < (node a, node b) { return a.value < b.value; } long long out[maxm], cnt; bitset<maxn> vist; long long n, m, s; int main() { scanf("%lld%lld", &s, &n); for(long long i = 1 ; i<= n ; i++) { scanf("%lld%lld", &p[i].x, &p[i].y); fa[i] = i; } for(long long i = 1 ; i<= n ; i++) { for(long long j = i+1 ; j<= n ; j++) { edge[++m].first = i; edge[m].second = j; edge[m].value = (double)sqrt((1.0*p[i].x-p[j].x)*(p[i].x-p[j].x)+(1.0*p[i].y-p[j].y)*(p[i].y-p[j].y)); } } sort(edge+1, edge+m+1); for(long long i = 1 ; i<= m ; i++) { if(find(edge[i].first) == find(edge[i].second)) { continue; } fa[find(edge[i].first)] = find(edge[i].second); out[++cnt] = i; if(cnt == n-s) { printf("%.2lf\n", edge[i].value); } } return 0; }
by AlexSong @ 2023-08-27 17:22:58


|