赤裸裸最小生成树,没啥说的,我用kruskal过的

/*
* Author : ben
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <functional>
#include <numeric>
#include <cctype>
using namespace std;
/*
* 输入非负整数
* 支持short、int、long、long long等类型(修改typec即可)。
* 用法typec a = get_int();返回-1表示输入结束
*/
typedef int typec;
typec get_int() {
typec res = , ch;
while (!((ch = getchar()) >= '' && ch <= '')) {
if (ch == EOF)
return -;
}
res = ch - '';
while ((ch = getchar()) >= '' && ch <= '')
res = res * + (ch - '');
return res;
}
//输入整数(包括负整数,故不能通过返回值判断是否输入到EOF,本函数当输入到EOF时,返回-1),用法int a = get_int2();
int get_int2() {
int res = , ch, flag = ;
while (!((ch = getchar()) >= '' && ch <= '')) {
if (ch == '-')
flag = ;
if (ch == EOF)
return -;
}
res = ch - '';
while ((ch = getchar()) >= '' && ch <= '')
res = res * + (ch - '');
if (flag == )
res = -res;
return res;
} const int MAXM = ;
const int MAXN = ;
typedef struct {
int s, e;
typec len;
} MyEdge;
int myset[MAXM], myheight[MAXM];
MyEdge edges[MAXN];
int N, M;
inline bool operator<(const MyEdge &e1, const MyEdge &e2) {
return e1.len < e2.len;
}
void initset() {
for (int i = ; i <= M; i++) {
myset[i] = i;
myheight[i] = ;
}
}
int myfind(int x) {
while (myset[x] != x) {
x = myset[x];
}
return x;
}
void mymerge(int a, int b) {
if (myheight[a] == myheight[b]) {
myheight[a]++;
myset[b] = a;
} else if (myheight[a] < myheight[b]) {
myset[a] = b;
} else {
myset[b] = a;
}
} int kruskal() {
int ret = , x, y, z;
sort(edges, edges + N);
initset();
for (int i = ; i < N; i++) {
x = edges[i].s;
y = edges[i].e;
z = edges[i].len;
if (myfind(x) == myfind(y)) {
continue;
}
mymerge(myfind(x), myfind(y));
ret += z;
}
return ret;
} int graph[][]; void buildgraph() {
int nn = get_int(), mm = get_int();
for (int i = ; i < nn; i++) {
for (int j = ; j < mm; j++) {
graph[i][j] = get_int();
}
}
M = nn * mm;
N = ;
for (int i = ; i < nn - ; i++) {
for (int j = ; j < mm - ; j++) {
edges[N].s = i * mm + j;
edges[N].e = i * mm + j + ;
edges[N].len = abs(graph[i][j] - graph[i][j + ]);
N++;
edges[N].s = i * mm + j;
edges[N].e = i * mm + mm + j;
edges[N].len = abs(graph[i][j] - graph[i + ][j]);
N++;
}
edges[N].s = i * mm + mm - ;
edges[N].e = i * mm + mm + mm - ;
edges[N].len = abs(graph[i][mm - ] - graph[i + ][mm - ]);
N++;
}
for (int j = ; j < mm - ; j++) {
edges[N].s = (nn - ) * mm + j;
edges[N].e = (nn - ) * mm + j + ;
edges[N].len = abs(graph[nn - ][j] - graph[nn - ][j + ]);
N++;
}
// printf("N = %d, M = %d\n", N, M);
} int main() {
// freopen("test.in.txt", "r", stdin);
int T = get_int();
for (int t = ; t <= T; t++) {
buildgraph();
printf("Case #%d:\n%d\n", t, kruskal());
}
return ;
}

hdu 5253 最小生成树的更多相关文章

  1. HDU 5253 最小生成树 kruscal

    Description 老 Jack 有一片农田,以往几年都是靠天吃饭的.但是今年老天格外的不开眼,大旱.所以老 Jack 决定用管道将他的所有相邻的农田全部都串联起来,这样他就可以从远处引水过来进行 ...

  2. HDU 5253 最小生成树(kruskal)+ 并查集

    题目链接 #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> ...

  3. HDU 1233(最小生成树)

    HDU 1233(最小生成树 模板) #include <iostream> #include <algorithm> #include <cstdio> usin ...

  4. HDU 5253 连接的管道 (最小生成树)

    连接的管道 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  5. HDU 1102 最小生成树裸题,kruskal,prim

    1.HDU  1102  Constructing Roads    最小生成树 2.总结: 题意:修路,裸题 (1)kruskal //kruskal #include<iostream> ...

  6. hdu 5253 连接的管道

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5253 连接的管道 Description 老 Jack 有一片农田,以往几年都是靠天吃饭的.但是今年老 ...

  7. poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题

    poj 1251  && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...

  8. Hdu 4081 最小生成树

    Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/3 ...

  9. hdu 4081 最小生成树+树形dp

    思路:直接先求一下最小生成树,然后用树形dp来求最优值.也就是两遍dfs. #include<iostream> #include<algorithm> #include< ...

随机推荐

  1. 一个简单的以User权限启动外部应用程序(用NetUserAdd函数和USER_INFO_1结构体动态添加用户,然后用CreateProcessWithLogonW启动程序)

    版权声明:本文为博主原创文章,未经博主允许不得转载. BOOL ExecuteAsUser(LPCWSTR lpszUserName, LPCWSTR lpszPassword, LPCWSTR lp ...

  2. Action的执行

    异步Action的定义 两种异步Action方法的定义 xxxAsync/xxxCompleted 这种形式的异步只能定义在实现了AsyncController的Controller中.针对Task的 ...

  3. 关于JavaScript的思考

    像apply这种函数,只有动态语言才能完成,动态语言既编译器/解释器这类代码生成器完成自己职责时只能在运行时完成,例如函数参数的压栈.仔细想想可能不对,也可以通过编译来完成 apply和call的使用 ...

  4. http 服务

    今天把一个功能模块做成了http服务,这还是第一次写http服务,纪录下来. package com.chuntent.itemsearch; import java.io.BufferedReade ...

  5. android ProgressBar 样式讲解

    转载自:eoe社区,可惜没找到源地址... 多式样ProgressBar 普通圆形ProgressBar 该类型进度条也就是一个表示运转的过程,例如发送短信,连接网络等等,表示一个过程正在执行中. 一 ...

  6. aopalliance.jar —— 下载地址

    下载地址:http://sourceforge.net/projects/aopalliance/files/aopalliance/1.0/aopalliance.zip/download TIPS ...

  7. 7.cadence原理图后续[原创]

    一.网表输出 1.自动编号 输出网表前,不能有问号 -- 效果: ---- -- 效果: 2.DRC检查 输出网表前需要DRC检查 3.网表输出 二.生成BOM表 法1: 法2: --- 点击OK: ...

  8. Hadoop集群(第9期)_MapReduce初级案例

    1.数据去重  "数据去重"主要是为了掌握和利用并行化思想来对数据进行有意义的筛选.统计大数据集上的数据种类个数.从网站日志中计算访问地等这些看似庞杂的任务都会涉及数据去重.下面就 ...

  9. SQL注入实验,PHP连接数据库,Mysql查看binlog,PreparedStatement,mysqli, PDO

    看到有人说了判断能否sql注入的方法: 简单的在参数后边加一个单引号,就可以快速判断是否可以进行SQL注入,这个百试百灵,如果有漏洞的话,一般会报错. 下面内容参考了这两篇文章 http://blog ...

  10. 从svn检出的项目如何编译

    从svn检出的项目如何编译   svn检查项目后,不能构建编译 工程右键,bulid path -->No actions available   问题:svn检查项目后,发现没有class文件 ...