• HDU 1102 Constructing Roads


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

    最简单的最小生成树

    View Code
    #include <stdio.h>
    #include <stdlib.h> 
    #include <math.h> 
    int p[110];
    int cnt,n; 
    typedef struct L
    {
        int a,b,d; 
    }L;
    L r[10000];     
    int cmp(const void*a,const void*b)
    {
        return (*(struct L*)a).d-(*(struct L*)b).d; 
    } 
    int find(int x){return p[x]==x?x:p[x]=find(p[x]);} 
    int Kruskal()
    {
        int ans=0; 
        int x,y,i; 
        for(i=0;i<n;i++)p[i]=i; 
        qsort(r,cnt,sizeof(L),cmp);
        for(i=0;i<cnt;i++)
        {
            x=find(r[i].a);
            y=find(r[i].b);
            if(x!=y){ans+=r[i].d;p[x]=y;} 
        }
        return ans; 
    }
    int main()
    {
        int i,j;
        int Q,a,b; 
        int x[110][110]; 
        while(~scanf("%d",&n))
        {
            cnt=0; 
            for(i=0;i<n;i++)
                for(j=0;j<n;j++) 
                    scanf("%d",&x[i][j]);
            for(i=0;i<n;i++)
                for(j=i+1;j<n;j++)
                { 
                    r[cnt].a=i;
                    r[cnt].b=j;
                    r[cnt++].d=x[i][j];
                } 
            scanf("%d",&Q); 
            for(i=0;i<Q;i++)
            {
                scanf("%d%d",&a,&b);
                for(j=0;j<cnt;j++)
                    if((r[j].a==a-1&&r[j].b==b-1)) 
                    {
                        r[j].d=0;
                        break;
                    }
            } 
            printf("%d\n",Kruskal()); 
        }
        return 0;
    }  
  • 相关阅读:
    【概念】构造函数和析构函数
    【概念】使用Fixed创建固定大小的缓冲区
    Sqoop
    Flume组件
    Hive节点及原理
    Yarn
    Hive数据倾斜
    单例
    工厂设计模式
    JVM对象创建
  • 原文地址:https://www.cnblogs.com/xiaohongmao/p/2489817.html
Copyright © 2020-2023  润新知