2 2 10 10 20 20 3 1 1 2 2 1000 1000
1414.2 oh!
水过,代码如下:
#include <cstdio> #include <algorithm> #include <cstring> #include <cmath> using namespace std; int f[111]; struct node1 { int st,end; double cost; }road[5111]; struct node2 { double x,y; }data[111]; int find(int x) { if (x!=f[x]) f[x]=find(f[x]); return f[x]; } int join (int x,int y) { int fx,fy; fx=find (x); fy=find (y); if (fx!=fy) { f[fx]=fy; return 1; } return 0; } bool cmp(node1 a,node1 b) { return a.cost<b.cost; } double dis(node2 a,node2 b) { double ans; ans=sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); return ans; } int main() { int n; int u; double ans; scanf ("%d",&u); while (u--) { scanf ("%d",&n); ans=0; for (int i=1;i<=n;i++) f[i]=i; //别忘了初始化 for (int i=1;i<=n;i++) scanf ("%lf %lf",&data[i].x,&data[i].y); int num=-1; for (int i=1;i<n;i++) { for (int j=i+1;j<=n;j++) { double t; t=dis(data[i],data[j]); if (t>=10 && t<=1000) { num++; road[num].st=i; road[num].end=j; road[num].cost=t; } } } sort (road,road+num+1,cmp); //这里注意num要 +1 int ach=0; //已经建成的桥数 for (int i=0;i<=num;i++) { if (join(road[i].st,road[i].end)) { ans+=road[i].cost; ach++; } } ans*=100; if (ach==n-1) printf ("%.1lf\n",ans); else printf ("oh!\n"); } return 0; }