hdu 5253

没想到百度之星跪这么惨...


本以为很简单的最小生成树 结果爆内存 超时 re 各种错 连一个wa都没有 归根究底 居然只是以为一个Find 函数写错了  因为抄的以前的代码 所以一直t到死 都不知道怎么回事


#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
#include <cstdio>
#include <vector>
#include <string>
#include <iterator>
#include <cmath>
#include <deque>
#include <stack>
#include <cctype>
#include <iomanip>
using namespace std;

typedef long long ll;
typedef long double ld;

const int N = 1100;
const int INF = 0xfffffff;
const double EPS = 1e-8;
const ll MOD = 1e9 + 7;
const ld PI = acos (-1.0);

#define INFL 0x7fffffffffffffffLL
#define met(a, b) memset(a, b, sizeof(a))
#define put(a) cout << setiosflags(ios::fixed) << setprecision(a)

struct node
{
    int x, y, len;
    bool operator < (const node &a) const
    {
        return len < a.len;
    }
};

int Find (int x);
int Union (int x, int y);
int kruskal ();

int x[N][N], m, n, k;

int father[N*N];
node ve[N*N*4];

int main ()
{
    int na, ca = 1;
    scanf ("%d", &na);
    while (na--)
    {
        k = 0;
        scanf ("%d%d", &m, &n);

        for (int i=0; i<m; i++)
            for (int j=0; j<n; j++)
            {
                node q;

                father[i*n+j] = i * n + j;

                scanf ("%d", &x[i][j]);

                if (j > 0)
                {
                    q.x = i*n+j, q.y = i*n+j-1;
                    q.len = abs (x[i][j] - x[i][j-1]);
                    ve[k++] = q;
                }
                if (i > 0)
                {
                    q.x = i*n+j, q.y = i*n+j-n;
                    q.len = abs (x[i][j] - x[i-1][j]);
                    ve[k++] = q;
                }
            }

        sort (ve, ve + k);

        printf ("Case #%d:\n", ca++);
        int xx = kruskal();
        printf ("%d\n", xx);
    }
    return 0;
}


int Union (int x, int y)
{
    int a = Find (x), b = Find (y);

    if (a != b)
    {
        father[a] = b;
        return 1;
    }
    return 0;
}

int kruskal ()
{
    node s;
    int i;
    int sum = 0;

    for (int i=0; i<k; i++)
    {
        if (Union (ve[i].x, ve[i].y))
            sum += ve[i].len;
    }
    return sum;
}


int Find(int x)
{
   return x == father[x] ? x : father[x] = Find (father[x]);

}


int Find(int x) // 原先的写法 不知道以前是怎么过的 t到死
{
   return x == father[x] ?  father[x] : Find (father[x]);

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值