zoukankan      html  css  js  c++  java
  • hdu5253最小生成树

    题意:(中文题,直接粘过来吧)
                                                                                 连接的管道
        老 Jack 有一片农田,以往几年都是靠天吃饭的。但是今年老天格外的不开眼,大旱。所以老 Jack 决定用管道将他的所有相邻的农田全部都串联起来,这样他就可以从远处引水过来进行灌溉了。当老 Jack 买完所有铺设在每块农田内部的管道的时候,老 Jack 遇到了新的难题,因为每一块农田的地势高度都不同,所以要想将两块农田的管道链接,老 Jack 就需要额外再购进跟这两块农田高度差相等长度的管道。

        现在给出老 Jack农田的数据,你需要告诉老 Jack 在保证所有农田全部可连通灌溉的情况下,最少还需要再购进多长的管道。另外,每块农田都是方形等大的,一块农田只能跟它上下左右四块相邻的农田相连通。
     
    Input
    第一行输入一个数字T(T≤10),代表输入的样例组数


    输入包含若干组测试数据,处理到文件结束。每组测试数据占若干行,第一行两个正整数 N,M(1≤N,M≤1000),代表老 Jack 有N行*M列个农田。接下来 N 行,每行 M 个数字,代表每块农田的高度,农田的高度不会超过100。数字之间用空格分隔。


    Output
    对于每组测试数据输出两行:


    第一行输出:"Case #i:"。i代表第i组测试数据。


    第二行输出 1 个正整数,代表老 Jack 额外最少购进管道的长度。
     

    Sample Input
    2
    4  3
    9 12 4
    7 8 56
    32 32 43
    21 12 12
    2  3
    34 56 56
    12 23 4


    Sample Output
    Case #1:
    82
    Case #2:
    74


    思路:

          把所有点连接在一起的最下费用,直接最小生成树就行了,一共是1000*1000*2条边,时间复杂度没啥问题,其实总感觉这个题目有点别扭,就是水流的方向问题,感觉是最小树形图,哎!想多了。


    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    
    #define N_node 1000*1000+10
    #define N_edge 1000 * 1000 * 2 + 10
    
    using namespace std;
    
    typedef struct
    {
        int a ,b ,c;
    }EDGE;
    
    EDGE E[N_edge];
    int map[1005][1005];
    
    bool camp(EDGE a ,EDGE b)
    {
        return a.c < b.c;
    }
    
    int mer[N_node];
    
    int finds(int x)
    {
        return x == mer[x] ? x : mer[x] = finds(mer[x]);
    }
    
    int abss(int x)
    {
        return x > 0 ? x : -x;
    }
    
    int main ()
    {
        int t ,n ,m ,i ,j ,cas = 1;
        scanf("%d" ,&t);
        while(t--)
        {
            scanf("%d %d" ,&n ,&m);
    
            for(i = 1 ;i <= n ;i ++)
            for(j = 1 ;j <= m ;j ++)
            scanf("%d" ,&map[i][j]);
            int nowid = 0;
            for(i = 1 ;i <= n ;i ++)
            for(j = 1 ;j <= m ;j ++)
            {
                int now = (i - 1) * m + j;
                mer[now] = now;
                if(j < m)
                {
                    nowid ++;
                    E[nowid].a = now;
                    E[nowid].b = (i - 1) * m + j + 1;
                    E[nowid].c = abss(map[i][j] - map[i][j+1]);
                }
                if(i < n)
                {
                    nowid ++;
                    E[nowid].a = now;
                    E[nowid].b = (i - 1 + 1) * m + j;
                    E[nowid].c = abss(map[i][j] - map[i+1][j]);
                }
            }
            sort(E + 1 ,E + nowid + 1 ,camp);
            int sum = 0;
            for(i = 1 ;i <= nowid ;i ++)
            {
                int a = finds(E[i].a);
                int b = finds(E[i].b);
                if(a != b)
                {
                    mer[a] = b;
                    sum += E[i].c;
                }
            }
            printf("Case #%d:
    " ,cas ++);
            printf("%d
    " ,sum);
    
        }
        return 0;
    }
    
    
    
    


  • 相关阅读:
    oracle中Blob和Clob类型的区别
    为什么要分库分表
    Enable file editing in Visual Studio's debug mode
    SQL Server Dead Lock Log
    Debug .NET Framework Source
    SQL Server text field里面有换行符的时候copy到excel数据会散乱
    诊断和修复Web测试记录器(Web Test Recorder)问题
    Can't load Microsoft.ReportViewer.ProcessingObjectModel.dll
    'telnet' is not recognized as an internal or external command
    Linq to XML
  • 原文地址:https://www.cnblogs.com/csnd/p/12062398.html
Copyright © 2011-2022 走看看