【HDU】5253-连接的管道(并查集)

十分简单的并查集问题,注意建图的方式

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1005;
const int maxd = 5000005;
const int dir[2][2] = {{0,1},{1,0}};
int n,m,mat[maxn][maxn],cnt,fa[maxd];
struct Node{
    int u,v,d;
    friend bool operator < (Node p,Node q){
        return p.d < q.d;
    }
}edge[maxd];
void init(){
    for(int x = 0; x < n; x++)
        for(int y = 0; y < m; y++){
            for(int d = 0; d < 2; d ++){
                int xx = x + dir[d][0];
                int yy = y + dir[d][1];
                if(xx >= 0 && xx < n && yy >= 0 && yy < m){
                    edge[cnt].u = x * m + y;
                    edge[cnt].v = xx * m + yy;
                    edge[cnt].d = abs(mat[xx][yy] - mat[x][y]);
                    cnt ++;
                }
            }
        }
}
int find_father(int u){
    return fa[u] == u ? u : fa[u] = find_father(fa[u]);
}
int main(){
    int T,Case = 1;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        cnt = 0;
        int t = n * m;
        for(int i = 0; i <= t; i ++) fa[i] = i;
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
                scanf("%d",&mat[i][j]);
        init();
        sort(edge,edge + cnt);
        int ans = 0;
        printf("Case #%d:\n",Case++);
        for(int i = 0; i < cnt; i++){
            int f1 = find_father(edge[i].u);
            int f2 = find_father(edge[i].v);
            if(f1 != f2){
                //printf("%d %d %d\n",edge[i].u,edge[i].v,edge[i].d);
                ans += edge[i].d;
                fa[f1] = f2;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值