N - 畅通工程再续
思路:zz
#include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define MAXN 310 using namespace std; double ans; int fa[MAXN]; int t,n,tot,sum; double x[MAXN],y[MAXN]; struct nond{ int x,y;double z; }v[MAXN*MAXN]; int cmp(nond a,nond b){ return a.z<b.z; } int find(int x){ if(fa[x]==x) return x; else return fa[x]=find(fa[x]); } int main(){ scanf("%d",&t); while(t--){ scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%lf%lf",&x[i],&y[i]); for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++){ double tmp=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])); if(tmp>=10&&tmp<=1000){ v[++tot].x=i;v[tot].y=j;v[tot].z=tmp; } } for(int i=1;i<=n;i++) fa[i]=i; sort(v+1,v+1+tot,cmp); for(int i=1;i<=tot;i++){ int dx=find(v[i].x); int dy=find(v[i].y); if(dx==dy) continue; fa[dy]=dx;sum++; ans+=v[i].z; if(sum==n-1) break; } if(sum<n-1) cout<<"oh!"<<endl; else printf("%.1lf\n",ans*100); ans=0;tot=0;sum=0; } }
细雨斜风作晓寒。淡烟疏柳媚晴滩。入淮清洛渐漫漫。
雪沫乳花浮午盏,蓼茸蒿笋试春盘。人间有味是清欢。