题解:

简单最小生成树

x,y都要double

我也不知道为什么

代码:

#include<bits/stdc++.h>
using namespace std;
const int N=1005;
int n,m,f[N],xx,yy;
double x[N],y[N],a[N][N],dis[N],ans;
int main()
{
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;i++)scanf("%lf%lf",&x[i],&y[i]);
    for (int i=1;i<=n;i++)
     for (int j=1;j<=n;j++)a[i][j]=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
    while (m--)
     {
         scanf("%d%d",&xx,&yy);
         a[xx][yy]=a[yy][xx]=0;
     } 
    f[1]=1;
    for (int i=1;i<=n;i++)dis[i]=a[1][i];
    for (int i=1;i<n;i++)
     {
         int l=-1;
         for (int j=1;j<=n;j++)
          if (!f[j]&&(l==-1||dis[j]<dis[l]))l=j;
         ans+=dis[l];
        f[l]=1;
        for (int j=1;j<=n;j++)
         if (!f[j]&&dis[j]>a[l][j])dis[j]=a[l][j]; 
     }
    printf("%.2lf",ans); 
}

 

posted on 2018-02-26 12:31  宣毅鸣  阅读(93)  评论(0编辑  收藏  举报