zoukankan      html  css  js  c++  java
  • SSLZYC POJ 2560 Freckles

    题目大意:
    求最小生成树。


    思路:
    emm。。。
    这道题跟 ->戳我<- 不是基本一样吗?
    但是还是有区别的:
    (1)要用勾股定理求两点长度
    (2)答案是小数
    其他都一样吧。。。


    代码:

    #include <cstdio>
    #include <iostream>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    
    int father[1001],n,k,o;
    double x[1001],y[1001],sum;
    
    struct N
    {
        int x,y;
        double f;
    }a[100001];
    
    bool cmp(N x,N y)
    {
        return x.f<y.f;
    }
    
    double gg(int a,int b)  //勾股定理
    {
        return sqrt((x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b]));
    }
    
    int find(int x)  //并查集
    { 
        return x==father[x]?x:father[x]=find(father[x]);
    }
    
    int main()
    {
        scanf("%d",&n);
        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++)
          if (i!=j) 
          {
             k++;
             a[k].f=gg(i,j);
             a[k].x=i;
             a[k].y=j; 
          }
        sort(a+1,a+1+k,cmp);
        for (int i=1;i<=n;i++)
         father[i]=i;  //初始化
        for (int j=1;j<=k;j++)
        {
            if (find(a[j].x)==find(a[j].y)) continue;
            father[find(a[j].y)]=find(a[j].x);
            sum+=a[j].f;
            if (++o==n-1) break;
        }
        printf("%0.2lf\n",sum);
        return 0;
    }
  • 相关阅读:
    变量与基本数据类型的练习
    04-各数据类型的常用操作
    常用函数
    03-python流程控制
    02-Python输入输出及运算符
    02-补充:逻辑运算符
    线程
    tcp实现并发效果
    生产消费者作业
    作业4.22
  • 原文地址:https://www.cnblogs.com/hello-tomorrow/p/9313090.html
Copyright © 2011-2022 走看看