• hdu 1875 Krustal最小生成树


    http://acm.hdu.edu.cn/showproblem.php?pid=1875

    题意:求链接所有岛屿的最小生成树。中文题就不说了。

    分析:kruskal算法。

    用sqrt的时候ce了一次。然后数组开小了re了一次。不应该犯这种低级错误了。反思己过。

    View Code
    // I'm lanjiangzhou
    //C
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <math.h>
    #include <time.h>
    //C++
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <cstring>
    #include <cctype>
    #include <stack>
    #include <string>
    #include <list>
    #include <queue>
    #include <map>
    #include <vector>
    #include <deque>
    #include <set>
    using namespace std;
    
    //*************************OUTPUT*************************
    #ifdef WIN32
    #define INT64 "%I64d"
    #define UINT64 "%I64u"
    #else
    #define INT64 "%lld"
    #define UINT64 "%llu"
    #endif
    
    //**************************CONSTANT***********************
    #define INF 0x3f3f3f3f
    
    // aply for the memory of the stack
    //#pragma comment (linker, "/STACK:1024000000,1024000000")
    //end
    
    const int maxn = 2010;
    struct node{
        int u,v;
        double w;
    }edges[maxn*4];
    int pa[maxn];
    int n,m;
    double x[maxn],y[maxn];
    double sumweight=0;
    
    //初始化
    void UFset(){
        for(int i=0;i<n;i++){
            pa[i]=-1;
        }
    }
    
    //查找
    int findset(int x){
        int s;
        for(s=x;pa[s]>=0;s=pa[s]);
        while(s!=x){
            int tmp=pa[x];
            pa[x]=s;
            x=tmp;
        }
        return s;
    }
    
    //合并
    void Union(int R1,int R2){
        int r1=findset(R1),  r2=findset(R2);
        int tmp=pa[r1]+pa[r2];
        if(pa[r1]>pa[r2]){
            pa[r1]=r2;
            pa[r2]=tmp;
        }
        else {
            pa[r2]=r1;
            pa[r1]=tmp;
        }
    }
    
    int cmp(const void*a,const void*b){
        node aa=*(const node*)a;
        node bb=*(const node*)b;
        if(aa.w>bb.w) return 1;
        else return -1;
    }
    
    void Kruskal(){
        int num=0;
        int u,v;
        UFset();
        for(int i=0;i<m;i++){
            u=edges[i].u;  v=edges[i].v;
            if(findset(u)!=findset(v)){
                sumweight+=edges[i].w; num++;
                Union(u,v);
            }
            if(num>=n-1) break;
        }
    }
    
    int main(){
        int t;
        scanf("%d",&t);
        while(t--){
            scanf("%d",&n);
            for(int i=0;i<n;i++){
                scanf("%lf%lf",&x[i],&y[i]);
            }
            int mi=0;
            for(int i=0;i<n;i++){
                for(int j=i+1;j<n;j++){
                    double d=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
                    if(d>=10.0&&d<=1000.0){
                        edges[mi].u=i;  edges[mi].v=j;  edges[mi].w=d;
                        mi++;
                       // printf("edges[mi]=%lf\n",d);
                    }
                    else continue;
                }
            }
            m=mi;
            qsort(edges,m,sizeof(edges[0]),cmp);
            sumweight=0.0;
            Kruskal();
            if(sumweight==0.0) printf("oh!\n");
            else
                printf("%.1lf\n",sumweight*100);
        }
        return 0;
    }
  • 相关阅读:
    从0开始学习 GitHub 系列之「02.加入 GitHub」
    从0开始学习 GitHub 系列之「01.初识 GitHub
    用Redis轻松实现秒杀系统
    算法之美
    Android窗口管理服务WindowManagerService显示Activity组件的启动窗口(Starting Window)的过程分析
    6)django-示例(fbv)
    5)django-模板
    4)django-视图view
    3)django-路由系统url
    2)django-请求生命周期
  • 原文地址:https://www.cnblogs.com/lanjiangzhou/p/3006329.html
Copyright © 2020-2023  润新知