标签:
4 2 3 0 0 1 0 0 -1 1 -1 0
3.41
#include <iostream> #include<cstdio> #include<cstring> #include<climits> #include<cmath> using namespace std; double sum; int i,j,n; bool vis[55]; int x[55],y[55]; double dis[55],mp[55][55]; void prim() { for(int i=1;i<n-1;i++) { double minn=INT_MAX*1.0; int k; for(int j=1;j<=n;j++) if (!vis[j] && dis[j]<minn) { k=j; minn=dis[j]; } vis[k]=1; sum+=minn; for(int j=1;j<=n;j++) if (!vis[j] && mp[k][j]<dis[j]) dis[j]=mp[k][j]; } return; } int main() { while(scanf("%d",&n) && n) { int u,v; memset(vis,0,sizeof(vis)); scanf("%d%d",&u,&v); for(i=1;i<=n;i++) scanf("%d%d",&x[i],&y[i]); for(i=1;i<=n;i++) for(j=i+1;j<=n;j++) { double d=sqrt(pow(x[i]-x[j],2)+pow(y[i]-y[j],2)); mp[i][j]=d; mp[j][i]=d; } sum=mp[u][v];//先把那两家店连起来 vis[u]=1; vis[v]=1; //全部标记走过 for(i=1;i<=n;i++) dis[i]=mp[u][i]; for(i=1;i<=n;i++) dis[i]=min(dis[i],mp[v][i]);//预处理到其他点的距离 prim(); printf("%.2lf\n",sum); } return 0; }
标签:
原文地址:http://www.cnblogs.com/stepping/p/5723152.html