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< ...
随机推荐
- Winsock IOCP模型(四篇)
http://blog.csdn.net/visualeleven/article/details/6041893 http://blog.csdn.net/visualeleven/article/ ...
- JBOSS集群技术升级版解决方案分享(图示篇)
JBOSS集群技术升级版解决方案分享(实现篇) 前段时间,由于阿堂一直较忙,没有写点什么了,有空时一直在关注"web架构和性能,高并发,Cache层"技术领域的 ...
- Android技术面试整理
前19题为常考题目!! 1.Android的四大组件是哪些,它们的作用? 2.请介绍下Android中常用的五种布局. 3.android中的动画有哪几类,它们的特点和区别是什么 4.android ...
- Maven概要[转]
1. Maven介绍 1.1. 简介 java编写的用于构建系统的自动化工具. 目前版本是2.0.9,注意maven2和maven1有很大区别,阅读第三方文档时需要区分版本. 1.2. Maven资源 ...
- MyBaits的各种基本查询方式
<?xml version="1.0" encoding="gbk"?> <!DOCTYPE mapper PUBLIC "-//m ...
- 寻找最小的k个数
1. 能想到的最直接的办法,就是对数组进行排序,最好的排序算法的时间复杂性为O(n*logn),这一个方法请参照各种排序算法. 2. 另外申请一个k空间数组,依次更改里面的最大值,每做一次最多要扫描一 ...
- MVC列表页通过CheckBox进行批量选择删除
1.Html代码,将所有CheckBox包含于删除表单,并且所有列表项的CheckBox使用相同的Name,并且Value设置为数据项主键ID @using (Html.BeginForm(" ...
- MTK6515 android打版软件配置(DrvGen.exe 使用)
1 一.配置GPIO 2 二.配置emmc 3 三.配置LCM 3.1 1.增加LCM驱动文件 3.2 2.配置驱动文件 3.3 3.配置背光 4 四.配置touch panel 4.1 1.通过dc ...
- SQL Server Object Explorer in VS
菜单栏View-->SQL Server Object Explorer 默认有几个连接,可以根据需要自己再另外添加 比如添加127.0.0.1 建立连接之后,剩下的操作和sql server中 ...
- How to: Run Tests from Microsoft Visual Studio
https://msdn.microsoft.com/en-us/library/ms182470.aspx Running Automated Tests in Visual Studio Visu ...