畅通工程再续hdu1875



#include<iostream>

#include<cmath>

#include<string.h>

#include<algorithm>

using namespace std;

int inf=0x3f3f3f3f;

const int maxn=105;

int n,m;

int father[maxn];

struct MP

{

   
int u,v;

   
double w;

}mp[maxn];

struct P

{

   
int x,y;

}p[maxn];

bool cmp(const MP a,const MP c)

{

   
return a.w<c.w;

}

void init()

{

   
for(int x=1;x<=n;x++)

    {

        father[x]=x;

    }

}

int Find(int x)

{

   
while(x!=father[x])

       
x=father[x];

   
return x;

}

void combine(int a,int c)

{

   
int temp=Find(a);

   
int temp1=Find(c);

   
if(temp!=temp1)

    {

       
father[temp]=temp1;

    }

}

double ccl(const P a,const P c)

{

   
return sqrt((c.x-a.x)*(c.x-a.x)+(c.y-a.y)*(c.y-a.y));

}

double kruskal()

{

   
sort(mp,mp+m,cmp);

   
init();

   
double ans=0;

   
for(int i=1;i<=m;i++)

    {

       
MP e=mp[i];

        if(Find(e.u)!=Find(e.v))

       
{combine(e.u,e.v);

       
ans+=mp[i].w;}

    }

   
return ans;

}

int main()

{

   
int t;

   
cin>>t;

   
while(t--)

    {

       
cin>>n;

       
m=n*(n-1)/2;

       
for(int i=1;i<=m;i++)

           
mp[i].w=inf;

           
for(int i=1;i<=n;i++)

           
{

               
cin>>p[i].x>>p[i].y;

           
}

       
int u,v;

       
double w;

       
int i=1;

       
for(int u=1;u<=n;u++)

           
for(int v=u;v<=n;v++)

       
{

           
w=ccl(p[u],p[v]);

           
if(w<10||w>1000)continue;

           
if(mp[i].w>w)

           
{

          
mp[i].u=u;

          
mp[i].v=v;

          
mp[i].w=w;

           
}

           
i++;

       
}

       
//for(int i=1;i<=m;i++)

           
//cout<<"?"<<mp[i].u<<"
"<<mp[i].v<<" "<<mp[i].w<<endl;

       
//以下为判断是否全部连接

   
int flag=true;

    
double tep=kruskal();for(int i=2;i<=n;i++)

    
if(Find(1)!=Find(i))flag=false;

    
if(flag)cout<<tep*100<<endl;

    
else cout<<"oh!"<<endl;

    }

}


样例过了,但是超时,不知道为什么,暂且放下

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值