• zoj 1203 Swordfish


    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    using namespace std;
    
    struct data
    {
        int u,v;
        double w;
    }e[5000];
    
    bool cmp(data a, data b)
    {
        return a.w<b.w;
    }
    
    double x[100+5],y[100+5];
    int n,m,bin[5000];
    
    int Find(int x)
    {
        int s;
        for(s=x;bin[s]>=0;s=bin[s]);
    
        while(s!=x)
        {
            int t=bin[x];
            bin[x]=s;
            x=t;
        }
        return s;
    }
    
    void Union(int x1,int x2)
    {
        int f1=Find(x1),f2=Find(x2);
        int t=bin[f1]+bin[f2];
        if(bin[f1]>bin[f2])
        {
            bin[f1]=f2;
            bin[f2]=t;
        }
        else
        {
            bin[f2]=f1;
            bin[f1]=t;
        }
    
    }
    
    int main()
    {
        //freopen("in.txt","r",stdin);
        //freopen("out.txit","w",stdout);
        int i,j,cas=0,num,u,v;
        double t,sum;
        while(~scanf("%d",&n))
        {
            if(n==0) break;
            for(i=0;i<=n;i++)
                bin[i]=-1;
            for(i=0;i<n;i++)
                scanf("%lf%lf",&x[i],&y[i]);
    
            m=0;
            for(i=0;i<n;i++)
                for(j=i+1;j<n;j++)
                {
                    t=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
                    e[m].u=i;
                    e[m].v=j;
                    e[m].w=t;
                    m++;
                }
            sort(e,e+m,cmp);
            sum=0;
            num=0;
            for(i=0;i<m;i++)
            {
                u=e[i].u;
                v=e[i].v;
                if(Find(u)!=Find(v))
                {
                    sum+=e[i].w;
                    Union(u,v);
                    num++;
                }
                if(num>=n-1) break;
            }
            if(cas) printf("
    ");
            printf("Case #%d:
    ",++cas);
            printf("The minimal distance is: %.2f
    ",sum);
        }
        return 0;
    }
    
  • 相关阅读:
    check datagurad scripts using python
    nagios check_oracle plugin (add check temp tablespace)
    monitor tomcat sctips
    ATM 练习
    socket 练习 ftp
    socket 练习 sendcommand
    带你走进虚拟化世界之kvm(转载)
    luogu2732商店购物
    uva1625颜色的长度
    luogu3147 [USACO16OPEN]262144
  • 原文地址:https://www.cnblogs.com/zhchoutai/p/8594822.html
Copyright © 2020-2023  润新知