【HDU】5253-连接的管道(并查集)
来源:互联网 发布:98印尼排华知乎 编辑:程序博客网 时间:2024/05/18 21:07
十分简单的并查集问题,注意建图的方式
#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 0
- 【HDU】5253-连接的管道(并查集)
- HDU 5253 连接的管道 —— 并查集
- HDU ACM 5253 连接的管道->最小生成树(并查集)
- HDU 5253 连接的管道(最小生成树-Kruskal+并查集)
- hdu 5253连接的管道(kruskal)
- HDU 5253 连接的管道
- HDU 5253 连接的管道
- hdu 5253 连接的管道
- HDU 5253 连接的管道
- HDU 5253 连接的管道
- 【HDU 5253 连接的管道】
- HDU 5253:连接的管道
- HDU 5253 连接的管道
- HDU 5253-连接的管道
- hdu 1875 序号连接并查集
- 连接的管道-HDU
- 【HDU】5253 - 连接的管道(最小生成树)
- HDU 5253 连接的管道(最小生成树)
- ogre渲染流程
- 基于openlayers的最短路径规划
- Fragment 的生命周期及使用方法详解
- Android底层开发(二)之HAL层 第二步
- 海马汽车经销商管理系统技术解析(十二)工单核准
- 【HDU】5253-连接的管道(并查集)
- Python核心编程 读书笔记
- 物流运交管理系统 配载发车
- 华为推出物联网系统LiteOS
- 十六进制转十进制
- 计算机组成原理复习提纲
- Dive into Python(深入Python) 读书笔记
- week14---6月3日
- 烟大课表PC端-不仅仅是浏览器和手机APP