当前位置: 代码迷 >> 综合 >> HDU 1875 畅通工程再续(MST)
  详细解决方案

HDU 1875 畅通工程再续(MST)

热度:97   发布时间:2023-12-08 11:14:51.0

题目链接:
HDU 1875 畅通工程再续
分析:
把不符合条件的边忽略再检查下所有点都连通就行了。

//1676K 62MS
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;const int maxn=110;
const int INF=0x3f3f3f3f;int T,n,tot;
int pre[maxn];
double ans;struct Point{int x,y;
}point[maxn];struct Edge{int u,v;double w;
}edge[maxn*maxn];void init()
{for(int i=0;i<maxn;i++)pre[i]=i;
}int find(int x)
{return pre[x]==x?x:pre[x]=find(pre[x]);
}double Distance(struct Point a,struct Point b)
{int xx=a.x-b.x;int yy=a.y-b.y;return sqrt(xx*xx+yy*yy);
}bool cmp(struct Edge a,struct Edge b)
{return a.w<b.w;
}int main()
{
#ifdef LOCALfreopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);
#endifscanf("%d",&T);while(T--){init();scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%d%d",&point[i].x,&point[i].y);tot=0;for(int i=1;i<=n;i++){for(int j=i+1;j<=n;j++){edge[tot].u=i;edge[tot].v=j;edge[tot].w=Distance(point[i],point[j]);tot++;}}sort(edge,edge+tot,cmp);ans=0;for(int i=0;i<tot;i++){if(edge[i].w>1000||edge[i].w<10) continue;int u=edge[i].u;int v=edge[i].v;int fu=find(u);int fv=find(v);if(fu==fv) continue;pre[fu]=fv;ans+=edge[i].w;}int com=find(1);int flag=0;for(int i=2;i<=n;i++){if(find(i)!=com){flag=1;break;}}if(flag) printf("oh!\n");else printf("%.1f\n",ans*100);}return 0;
}