赤裸裸最小生成树,没啥说的,我用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. MongoDB的SSL实现分析

    1. OPENSSL接口封装 MongoDB封装了OPENSSL的SSL通信接口,代码在mongo/util/net目录.主要包括以下几个方面: 1) SSL配置参数,在ssl_options(.cp ...

  2. QScrollArea可以帮助我们实现让一个widget的内容带有滚动条(QWidget里内置QScrollArea,QScrollArea里再内置其它QWidget)

    使用QScrollArea可以帮助我们实现让一个widget的内容带有滚动条,用户可以通过拖动滚动条来查看更多内容, 代码示例如下: 1.带有滚动条的widget列表 #include "w ...

  3. PHP中global全局变量的使用

    在方法里面想用外面的变量,可以声明这个变量为全局变量. $a=1; $b=2; test_global(); function test_global() { global $a,$b; echo $ ...

  4. Geoprocessor 使用

    在AO中使用Geoprocessor(ESRI.ArcGIS.Geoprocessor) 1.观察arcmap中的使用方法,明确各参数意义. 2.arctoolbox中参数对应为features/fe ...

  5. 使用grep查找文件中指定字符出现的次数

    grep -o ‘好' 文件名.txt | wc -l -o 指示grep显示所有匹配的地方,并且每一个匹配单独一行输出.这样只要统计输出的行数就可以知道这个字符出现的次数了.

  6. USACO Section 4.2: The Perfect Stall

    这题关键就在将题转换成最大流模板题.首先有一个原始点,N个cow个点, M个barn点和一个终点,原始点到cow点和barn点到终点的流都为1,而cow对应的barn就是cow点到对应barn点的流, ...

  7. 【Cocos2d实例教程一】xcode5下Cocos2d环境的搭建

    (转载请注明出处:http://blog.csdn.net/buptgshengod) 第一步,现在要安装集成环境xcode5,安装xcode5需要系统至少是os x 10.8.5. 第二步,下载co ...

  8. Spring 异常 —— cvc-elt.1: Cannot find the declaration of element 'beans'

    有个使用 Spring 的项目,运行时报错: org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 5 ...

  9. HDU 4726 Kia's Calculation(贪心构造)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4726 题意:给出两个n位的数字,均无前缀0.重新排列两个数字中的各个数,重新排列后也无前缀0.得到的两 ...

  10. 函数mem_pool_create

    /********************************************************************//** Creates a memory pool. @re ...