http://acm.hdu.edu.cn/showproblem.php?pid=1875
#include<iostream>//2235167 2010-03-23 16:21:03 Accepted 1875 312MS 352K 1567 B C++ 悔惜晟
#include<algorithm> //效率真的不高,算法还是有待给进,耗时主要在哪里呢??
#include<cstdio>
#include<cmath>
int aa[4951];
using namespace std;
struct stu1
{
int x1;
int y1;
}df1[103];
struct stu2
{
int x2;
int y2;
double distance ;
}df2[4951];
int cmp(stu2 a, stu2 b)
{
return a.distance < b.distance;
}
int find(int x)
{
int r = x;
while(r !=aa[r])
r = aa[r];
return r;
}
void mergy(int a, int b)
{
if(a > b)
aa[b] = a;
else
aa[a] = b;
}
int main()
{
int t, n, i, j, count, ww;
double sum, dis;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
for(i = 0; i < n; i++)
scanf("%d%d", &df1[i].x1, &df1[i].y1);
count = 0;
for(i = 0 ; i < n-1; i++)
for(j = i+1; j < n; j++)
{
dis = sqrt(1.0*((df1[i].x1 - df1[j].x1) * (df1[i].x1 - df1[j].x1) + (df1[i].y1 - df1[j].y1) * (df1[i].y1 - df1[j].y1)));
if(dis < 10 || dis > 1000)
continue;
else
{
df2[count].x2 = i + 1;
df2[count].y2 = j + 1;
df2[count].distance = dis;
count++;
}
}
sort(df2,df2+count,cmp);
for(i = 1 ; i <= n*(n-1)/2; i++)
aa[i] = i;
ww = 0;
sum = 0;//忘记了初始话
for(i = 0 ;i < count ; i++)
{
int a = find(df2[i].x2);
int b = find(df2[i].y2);
if(a != b)
{
sum += df2[i].distance;
mergy(a, b);
ww++;
}
}
if(ww == n-1)
printf("%0.1lf/n", 100*sum);
else
printf("oh!/n");
}
}
/*
#include<iostream>//2235212 2010-03-23 16:25:50 Time Limit Exceeded 1875 1000MS 408K 1648 B C++ 悔惜晟
#include<algorithm>
#include<cstdio>
#include<cmath>
int aa[9910];
using namespace std;
struct stu1
{
int x1;
int y1;
}df1[103];
struct stu2
{
int x2;
int y2;
double distance ;
}df2[9910];
int cmp(stu2 a, stu2 b)
{
return a.distance < b.distance;
}
int find(int x)
{
int r = x;
while(r !=aa[r])
r = aa[r];
return r;
}
void mergy(int a, int b)
{
if(a > b)
aa[b] = a;
else
aa[a] = b;
}
int main()
{
int t, n, i, j, num, count, flag;
double sum, dis;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
for(i = 0; i < n; i++)
scanf("%d%d", &df1[i].x1, &df1[i].y1);
count = 0;
flag = 0;
for(i = 0 ; i < n - 1; i++)
{
int tt = 0;
for(j = 0; j < n; j++)
{
if(i != j)//这里处理的很不好,多做很多没有用的判断
{
dis = sqrt(1.0*((df1[i].x1 - df1[j].x1) * (df1[i].x1 - df1[j].x1) + (df1[i].y1 - df1[j].y1) * (df1[i].y1 - df1[j].y1)));
if(dis < 10 || dis > 1000)
t++;
else
{
df2[count].x2 = i + 1;
df2[count].y2 = j + 1;
df2[count].distance = dis;
count++;
}
}
}
if(tt == n-1)
{
flag = 1;
break;
}
}
if( flag == 1)
printf("oh!/n");
else
{
sort(df2,df2+count,cmp);
for(i = 1 ; i <= n*(n-1)/2; i++)
aa[i] = i;
sum = 0;
for(i = 0 ;i < count ; i++)
{
int a = find(df2[i].x2);
int b = find(df2[i].y2);
if(a != b)
{
sum += df2[i].distance;
mergy(a, b);
}
}
printf("%0.1lf/n", 100*sum);
}
}
}