hdu 5253 最小生成树
赤裸裸最小生成树,没啥说的,我用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 最小生成树的更多相关文章
- HDU 5253 最小生成树 kruscal
Description 老 Jack 有一片农田,以往几年都是靠天吃饭的.但是今年老天格外的不开眼,大旱.所以老 Jack 决定用管道将他的所有相邻的农田全部都串联起来,这样他就可以从远处引水过来进行 ...
- HDU 5253 最小生成树(kruskal)+ 并查集
题目链接 #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> ...
- HDU 1233(最小生成树)
HDU 1233(最小生成树 模板) #include <iostream> #include <algorithm> #include <cstdio> usin ...
- HDU 5253 连接的管道 (最小生成树)
连接的管道 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- HDU 1102 最小生成树裸题,kruskal,prim
1.HDU 1102 Constructing Roads 最小生成树 2.总结: 题意:修路,裸题 (1)kruskal //kruskal #include<iostream> ...
- hdu 5253 连接的管道
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5253 连接的管道 Description 老 Jack 有一片农田,以往几年都是靠天吃饭的.但是今年老 ...
- 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 ...
- Hdu 4081 最小生成树
Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3 ...
- hdu 4081 最小生成树+树形dp
思路:直接先求一下最小生成树,然后用树形dp来求最优值.也就是两遍dfs. #include<iostream> #include<algorithm> #include< ...
随机推荐
- Centos挂载windows共享文件夹
1.windows7共享一个文件夹(1)新建一个用户:devin,密码:admin123(2)在E盘新建一个文件夹,share,并设置共享 对用户devin共享,并让其权限为:读取和写入. 2.lin ...
- Data Base sqlServer基础知识
sqlServer 基础知识 大纲 创建数据库 1 创建表 2 备份表 3 删除表 4 修改表 5 查询出重复的数据 6 增删改查 7 添加约束 8 分页存储过程 9 排序 10 类型转换 11 ...
- poj 1182 食物链 (并查集)
http://poj.org/problem?id=1182 关于并查集 很好的一道题,开始也看了一直没懂.这次是因为<挑战程序设计竞赛>书上有讲解看了几遍终于懂了.是一种很好的思路,跟网 ...
- python3字符串格式化
print('Hello World')print('%s',55)print('%6.2f' % 1.235)print('%06.2f' % 1.235)print('-%06.2f' % 1.2 ...
- [lintcode the-smallest-difference]最小差(python)
题目链接:http://www.lintcode.com/zh-cn/problem/the-smallest-difference/ 给定两个整数数组(第一个是数组 A,第二个是数组 B),在数组 ...
- hdu 1520 Anniversary party || codevs 1380 树形dp
Anniversary party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- Remember that ordinal parameters are 1-based!
问题发生的原因是:hql语句里不需要参数,却添加了一个参数,删掉添加参数的语句就可以了!
- 二维线性表 list实现
class Coordinate{ private int x; private int y; } List<Coordinate> list=new ArrayList<Coord ...
- Machine Learning for hackers读书笔记(十二)模型比较
library('ggplot2')df <- read.csv('G:\\dataguru\\ML_for_Hackers\\ML_for_Hackers-master\\12-Model_C ...
- Qt之QHeaderView自定义排序(获取正确的QModelIndex)
简述 前几节中分享过关于自定义排序的功能,貌似我们之前的内容已经可以很好地解决排序问题了,但是,会由此引发一些很难发现的问题...比如:获取QModelIndex索引错误. 下面,我们先来实现一个整行 ...