这道题wa了一次,让我重新意识到了G++提交和C++的不同。
主要为:
因为g++和c++的区别主要在库的不同上,其它的语法之类的倒是基本一致,没有太多区别的。对于c和c++混用的影响不大。
一般的OJ都是GCC的编译器。。建议用G++,更加符合标准。比如 G++标准的浮点型输出用%f 而不是%lf(这里有时会wa)
//POJ 2031
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
using namespace std;
const int SIZE=101;
double x[SIZE], y[SIZE], z[SIZE], r[SIZE];
typedef struct edge
{
int u,v;
double w;
}edge;
edge e[SIZE*SIZE];
int tot;
int n, parent[SIZE];
void cal()
{
double dd;
tot=0;
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
{
dd=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])+(z[i]-z[j])*(z[i]-z[j]));
if(dd<=r[i]+r[j]) {e[tot].u=i; e[tot].v=j; e[tot++].w=0;}
else {e[tot].u=i; e[tot].v=j; e[tot++].w=(dd-r[i]-r[j]);}
}
}
int find(int x)
{
if(parent[x]<0) return x;
parent[x]=find(parent[x]);
return parent[x];
}
void union_set(int x, int y)
{
int px=find(x), py=find(y);
int temp=parent[px]+parent[py];
if(parent[px]>parent[py])
{
parent[px]=py;
parent[py]=temp;
}
else
{
parent[py]=px;
parent[px]=temp;
}
}
void kruskal()
{
int u,v,num=0;
double w,sum=0;
for(int i=0;i<SIZE;i++) parent[i]=-1;
for(int i=0;i<tot;i++)
{
u=e[i].u; v=e[i].v; w=e[i].w;
if(find(u)==find(v)) continue;
union_set(u,v);
num++;
sum+=w;
if(num==n-1) break;
}
printf("%.3f\n",sum);
}
bool cmp(edge a, edge b)
{
return a.w<b.w;
}
int main()
{
while(cin>>n&&n)
{
for(int i=0;i<n;i++)
cin>>x[i]>>y[i]>>z[i]>>r[i];
cal();
sort(e,e+tot,cmp);
kruskal();
}
return 0;
}