首页 > 其他 > 详细

hdu1162

时间:2015-07-30 09:32:02      阅读:179      评论:0      收藏:0      [点我收藏+]
#include<cstdio>
#include<cmath>
#include<climits>
#include<algorithm>
#define INF 1000000000
using namespace std;
struct p
{
    double x,y;
}spot[110];
double cost[110][110];
double mincost[110];
bool used[110];
int n;

double prim()
{
    for(int i=0;i<n;i++)
    {
        mincost[i]=INF;
        used[i]=false;
    }
    mincost[0]=0;
    double res=0;

    while(true){
        int v=-1;
        for(int i=0;i<n;i++)
            if(!used[i]&&(v==-1||mincost[i]<mincost[v])) v=i;
        if(v==-1) break;
        used[v]=true;
        res+=mincost[v];
        for(int i=0;i<n;i++)
            mincost[i] = min(mincost[i], cost[v][i]);

    }
    return res;
}
double dis(p a,p b)
{
    double fa = (a.x-b.x);
    double fb = (a.y-b.y);
    return sqrt(fa*fa+fb*fb);
}
int main()
{
//    int n;
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++)
            scanf("%lf%lf",&spot[i].x,&spot[i].y);
        for(int i=0;i<n;i++)
        for(int j=i;j<n;j++){
            if(i==j) cost[i][j]=INF;
            else { cost[i][j]=cost[j][i]=dis(spot[i],spot[j]); }
        }
//        for(int i=0;i<n;i++){
//        for(int j=0;j<n;j++){
//            printf("%lf ",cost[i][j]);
//        }
//        printf("\n");
//        }

        printf("%.2lf\n",prim());
    }
//    printf("%lf",dis(spot[0],spot[1]));
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

hdu1162

原文:http://blog.csdn.net/a197p/article/details/47143845

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!