hdoj 5253 连接的管道 【MST】

来源:互联网 发布:linux查看物理网卡数 编辑:程序博客网 时间:2024/06/15 02:49

题目链接:hdoj 5253 连接的管道

连接的管道

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1955 Accepted Submission(s): 685

Problem Description
老 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

SB死了,存边数组开小。。。。。。

AC代码:

#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#include <queue>#include <cmath>#define fi first#define se second#define ll o<<1#define rr o<<1|1#define CLR(a, b) memset(a, (b), sizeof(a))using namespace std;typedef long long LL;typedef pair<int, int> pii;const int MOD = 1e4 + 7;const int MAXN = 1e3 + 10;void add(LL &x, LL y) { x += y; x %= MOD; }int father[MAXN*MAXN];int Find(int p) {    int child = p, t;    while(p != father[p]) {        p = father[p];    }    while(child != p ) {        t = father[child];        father[child] = p;        child = t;    }    return p;}struct Edge {    int from, to, val;};Edge edge[MAXN*MAXN*4];bool cmp(Edge a, Edge b) {    return a.val < b.val;}int Map[MAXN][MAXN];int Move[2][2] = {0,1, 1,0};int n, m;bool judge(int x, int y) {    return x >= 0 && x < n && y >= 0 && y < m;}int main(){    int t, kcase = 1; scanf("%d", &t);    while(t--) {        scanf("%d%d", &n, &m);        for(int i = 0; i < n; i++) {            for(int j = 0; j < m; j++) {                scanf("%d", &Map[i][j]);                father[i*m+j] = i*m+j;            }        }        int top = 0;        for(int i = 0; i < n-1; i++) {            for(int j = 0; j < m-1; j++) {                    int x = i + Move[0][0];                    int y = j + Move[0][1];                    edge[top].from = i*m+j;                    edge[top].to = x*m+y;                    edge[top++].val = abs(Map[i][j]-Map[x][y]);            }        }        for(int i = 0; i < n-1; i++) {            for(int j = 0; j < m-1; j++) {                    int x = i + Move[1][0];                    int y = j + Move[1][1];                    edge[top].from = i*m+j;                    edge[top].to = x*m+y;                    edge[top++].val = abs(Map[i][j]-Map[x][y]);            }        }        for(int i=0;i<n-1;i++)        {            int x = i + Move[1][0];            int y = m-1;            edge[top].from = i*m+m-1;            edge[top].to = x*m+y;            edge[top++].val = abs(Map[i][m-1]-Map[x][y]);        }         for(int i=0;i<m-1;i++)        {                int x = n-1;                int y = i + Move[0][1];                edge[top].from = (n-1)*m+i;                edge[top].to = x*m+y;                edge[top++].val = abs(Map[(n-1)][i]-Map[x][y]);        }        sort(edge, edge+top, cmp);        LL ans = 0; int cnt = 0;        for(int i = 0; i < top; i++) {            int u = Find(edge[i].from);            int v = Find(edge[i].to);            if(u != v) {                father[u]=v;                ans += edge[i].val;                cnt++;            }            if(cnt == n*m-1) {                break;            }        }        printf("Case #%d:\n%lld\n", kcase++, ans);    }    return 0;}
0 0
原创粉丝点击