zoukankan      html  css  js  c++  java
  • hdu 1162 Eddy's picture(最小生成树,基础)

    #define  _CRT_SECURE_NO_WARNINGS
    
    #include <stdio.h>
    #include<string.h>
    #include <malloc.h>
    #include<stdlib.h>
    #include<algorithm>
    #include<iostream>
    #include<math.h>
    using namespace std;
    
    #define M 110
    #define inf 999999999
    double mat[M][M];
    struct tt
    {
        double x,y;
    }dian[M];
    
    double prim(int n,int sta)
    {
        double minn,sum=0,dis[M];
        int mark[M],i,j,flag;
        for(i=0;i<n;i++)
        {
            dis[i]=mat[sta][i];
            mark[i]=0;
        }
        mark[sta]=1;
        for(i=1;i<n;i++)
        {
            minn=inf;
            for(j=0;j<n;j++)
            {
                if(mark[j]==0&&dis[j]<minn)
                {
                    minn=dis[j];
                    flag=j;
                }
            }
            mark[flag]=1;
            sum+=dis[flag];
            for(j=0;j<n;j++)
            {
                if(dis[j]>mat[flag][j])
                    dis[j]=mat[flag][j];
            }
        }
        return sum;
    }
    
    int main()
    {
        int n,m,i,j;
        while(scanf("%d",&n)!=EOF)
        {
            for(i=0;i<n;i++)
            {
                scanf("%lf%lf",&dian[i].x,&dian[i].y);
                for(j=0;j<i;j++)
                {
                    mat[i][j]=mat[j][i]=sqrt((dian[i].x-dian[j].x)*(dian[i].x-dian[j].x)+(dian[i].y-dian[j].y)*(dian[i].y-dian[j].y));
                }
            }
            printf("%.2lf
    ",prim(n,0));
        }
        return 0;
    }
    View Code
    一道又一道,好高兴!
  • 相关阅读:
    kindle--瓦尔登湖
    8051
    c++
    Linux安装目录的选择
    Redis键值数据类型之散列类型
    Redis键值数据类型之字符串
    redis基本使用
    Ubuntu18.04 Redis安装
    Java String和int转换
    mysql获得自增主码的值
  • 原文地址:https://www.cnblogs.com/laiba2004/p/3521005.html
Copyright © 2011-2022 走看看