标签:
Description
Input
Output
Sample Input
3 1.0 1.0 2.0 2.0 2.0 4.0
Sample Output
3.41
prim:
#include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<cmath> using namespace std; int n; double map[110][110],sum; int v[210]; void prm() { int i,k,p; double max; k=n-1; while (k--) { for (i=2;i<=n;i++) if (!v[i]) {max=map[1][i];p=i;break;} for (i=i+1;i<=n;i++) if (!v[i]&&max>map[1][i]) {max=map[1][i];p=i;} v[p]=1; sum+=max; for (i=2;i<=n;i++) if (map[1][i]>map[p][i]) map[1][i]=map[p][i]; } return ; } int main() { double a[110],b[110]; int i,j; sum=0; scanf("%d",&n); for (i=1;i<=n;i++) scanf("%lf%lf",&a[i],&b[i]); for (i=1;i<=n;i++) for (j=i;j<=n;j++) map[i][j]=map[j][i]=sqrt((a[i]-a[j])*(a[i]-a[j])+(b[i]-b[j])*(b[i]-b[j])); prm(); printf("%0.2f\n",sum); return 0; }
标签:
原文地址:http://www.cnblogs.com/pblr/p/4711409.html