315人阅读 评论(0)

# 畅通工程再续

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 20997    Accepted Submission(s): 6633

Problem Description

Input

Output

Sample Input
2 2 10 10 20 20 3 1 1 2 2 1000 1000

Sample Output
1414.2 oh!

Author
8600

Source

Recommend
lcy   |   We have carefully selected several similar problems for you:  1879 1863 1874 1301 1162

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;

const int maxn=100;
int f[maxn+10];
int x[maxn+10],y[maxn+10];
struct edge{
int u,v;
double w;
}e[maxn*maxn+10];

double get(int i,int j)
{
int a=x[i]-x[j],b=y[i]-y[j];
return sqrt((a*a+b*b)*1.0);
}

int find(int x)
{
if(f[x]==x)return x;
return f[x]=find(f[x]);
}

bool cmp_e(edge a,edge b)
{
return a.w<b.w;
}

int main()
{
//freopen("1.in","r",stdin);

int t,n,i,j,s,a,b;double ans;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=1;i<=n;i++)f[i]=i;
for(i=1;i<=n;i++)scanf("%d%d",&x[i],&y[i]);
for(s=0,i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
e[++s].u=i,e[s].v=j,e[s].w=get(i,j);
sort(e+1,e+s+1,cmp_e);
for(ans=0,i=1;i<=s;i++)
{
if(e[i].w<10)continue;
if(e[i].w>1000)break;
a=find(e[i].u),b=find(e[i].v);
if(a!=b)f[a]=b,ans+=e[i].w;
}
for(j=0,i=1;i<=n;i++)
if(f[i]==i)j++;
if(j>1)printf("oh!\n");
else printf("%.1lf\n",ans*100);
}
return 0;
}

个人资料
等级：
访问量： 29万+
积分： 6539
排名： 4481
最新评论