HDOJ-----5253连接的管道(最小生成树)

连接的管道

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


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

现在给出老 Jack农田的数据,你需要告诉老 Jack 在保证所有农田全部可连通灌溉的情况下,最少还需要再购进多长的管道。另外,每块农田都是方形等大的,一块农田只能跟它上下左右四块相邻的农田相连通。
 

Input
第一行输入一个数字 T(T10) ,代表输入的样例组数

输入包含若干组测试数据,处理到文件结束。每组测试数据占若干行,第一行两个正整数 N,M(1N,M1000) ,代表老 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
任意两个相邻的点之间都有通道,两点的差值即是权值,问所有点连接在一起,最小的权值,对所有的边进行处理,任意相邻的两点都是边,克鲁斯卡尔求最小生成树,卡时间卡的太死,卡到将近1s,差一点点就是超时

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn = 1e6 + 5;
const int M = 1e3 + 5;
int s[M][M];
int n, m, cnt, ans;
int pre[maxn];
struct node{
    int x, y, val;
}tre[maxn*2];
bool cmp(node a, node b){
    return a.val < b.val;
}
int find(int a){
    return a == pre[a] ? a : pre[a] = find(pre[a]);
}
int main(){
    int t, kcase = 1;
    scanf("%d", &t);
    while(t--){
        scanf("%d%d", &n, &m);
        ans = cnt = 0;
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                scanf("%d", &s[i][j]);
            }
        }
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                int c = (i-1)*m+j;
                pre[c] = c;
                if(j > 1){
                    tre[cnt].x = (i-1)*m+j-1;
                    tre[cnt].y = c;
                    tre[cnt++].val = abs(s[i][j-1] - s[i][j]);
                }
                if(i > 1){
                    tre[cnt].x = (i-2)*m+j;
                    tre[cnt].y = c;
                    tre[cnt++].val = abs(s[i][j] - s[i-1][j]);
                }
            }
        }
        sort(tre, tre+cnt, cmp);
        for(int i = 0; i < cnt; i++){
            int fx = find(tre[i].x), fy = find(tre[i].y);
            if(fx != fy){
                ans += tre[i].val;
                pre[fx] = fy;
            }
        }
        printf("Case #%d:\n", kcase++);
        printf("%d\n", ans);
    }
    return 0;
}


原来用const node& 进行引用会提高效率,受教了

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn = 1e6 + 5;
const int M = 1e3 + 5;
int s[M][M];
int n, m, cnt, ans;
int pre[maxn];
struct node{
    int x, y, val;
    bool operator < (const node& a) const{//这里
        return val < a.val;
    }
}tre[maxn*2];
/*struct node{
    int x, y, val;
}tre[maxn*2];
bool cmp(const node& a, const node& b){//也等价于这里,貌似比上边的规范一些,不过不影响什么
    return a.val < b.val;
}*/
int find(int a){
    return a == pre[a] ? a : pre[a] = find(pre[a]);
}
int main(){
    int t, kcase = 1;
    scanf("%d", &t);
    while(t--){
        scanf("%d%d", &n, &m);
        ans = cnt = 0;
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                scanf("%d", &s[i][j]);
            }
        }
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                int c = (i-1)*m+j;
                pre[c] = c;
                if(j > 1){
                    tre[cnt].x = (i-1)*m+j-1;
                    tre[cnt].y = c;
                    tre[cnt++].val = abs(s[i][j-1] - s[i][j]);
                }
                if(i > 1){
                    tre[cnt].x = (i-2)*m+j;
                    tre[cnt].y = c;
                    tre[cnt++].val = abs(s[i][j] - s[i-1][j]);
                }
            }
        }
        sort(tre, tre+cnt);
        //sort(tre, tre+cnt, cmp);
        for(int i = 0; i < cnt; i++){
            int fx = find(tre[i].x), fy = find(tre[i].y);
            if(fx != fy){
                ans += tre[i].val;
                pre[fx] = fy;
            }
        }
        printf("Case #%d:\n", kcase++);
        printf("%d\n", ans);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值