HDU 5253 连接的管道 (最小生成树)
连接的管道
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 653 Accepted Submission(s): 268
现在给出老 Jack农田的数据,你需要告诉老 Jack 在保证所有农田全部可连通灌溉的情况下,最少还需要再购进多长的管道。另外,每块农田都是方形等大的,一块农田只能跟它上下左右四块相邻的农田相连通。
输入包含若干组测试数据,处理到文件结束。每组测试数据占若干行,第一行两个正整数 N,M(1≤N,M≤1000),代表老 Jack 有N行*M列个农田。接下来 N 行,每行 M 个数字,代表每块农田的高度,农田的高度不会超过100。数字之间用空格分隔。
第一行输出:"Case #i:"。i代表第i组测试数据。
第二行输出 1 个正整数,代表老 Jack 额外最少购进管道的长度。
4 3
9 12 4
7 8 56
32 32 43
21 12 12
2 3
34 56 56
12 23 4
82
Case #2:
74
#include <iostream>
#include <cstdio>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std; const int SIZE = ;
struct Node
{
int from,to,cost;
}G[SIZE * SIZE * ];
int MAP[SIZE][SIZE],FATHER[SIZE * SIZE];
int N,M,NUM; void ini(void);
int find_father(int);
void unite(int,int);
bool same(int,int);
bool comp(const Node &,const Node &);
long long kruskal(void);
int main(void)
{
int t;
int count = ; scanf("%d",&t);
while(t --)
{
count ++;
scanf("%d%d",&N,&M);
ini();
for(int i = ;i <= N;i ++)
for(int j = ;j <= M;j ++)
scanf("%d",&MAP[i][j]);
for(int i = ;i <= N;i ++)
for(int j = ;j <= M;j ++)
{
if(j + <= M)
{
G[NUM].from = (i - ) * M + j;
G[NUM].to = (i - ) * M + j + ;
G[NUM].cost = abs(MAP[i][j] - MAP[i][j + ]);
NUM ++;
}
if(i + <= N)
{
G[NUM].from = (i - ) * M + j;
G[NUM].to = (i) * M + j;
G[NUM].cost = abs(MAP[i][j] - MAP[i + ][j]);
NUM ++;
}
}
printf("Case #%d:\n%lld\n",count,kruskal());
} return ;
} void ini(void)
{
NUM = ;
for(int i = ;i <= N * M;i ++)
FATHER[i] = i;
} int find_father(int n)
{
if(n == FATHER[n])
return n;
return FATHER[n] = find_father(FATHER[n]);
} void unite(int x,int y)
{
x = find_father(x);
y = find_father(y); if(x == y)
return ;
FATHER[x] = y;
} bool same(int x,int y)
{
return find_father(x) == find_father(y);
} bool comp(const Node & a,const Node & b)
{
return a.cost < b.cost;
} long long kruskal(void)
{
long long ans = ;
int count = ; sort(G,G + NUM,comp);
for(int i = ;i < NUM;i ++)
if(!same(G[i].from,G[i].to))
{
ans += G[i].cost;
unite(G[i].from,G[i].to);
if(count == N * M - )
break;
}
return ans;
}
HDU 5253 连接的管道 (最小生成树)的更多相关文章
- hdu 5253 连接的管道
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5253 连接的管道 Description 老 Jack 有一片农田,以往几年都是靠天吃饭的.但是今年老 ...
- hdu 5253 连接的管道(kruskal)(2015年百度之星程序设计大赛 - 初赛(2))
连接的管道 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- HDU 5253 连接的管道(Kruskal算法求解MST)
题目: 老 Jack 有一片农田,以往几年都是靠天吃饭的.但是今年老天格外的不开眼,大旱.所以老 Jack 决定用管道将他的所有相邻的农田全部都串联起来,这样他就可以从远处引水过来进行灌溉了.当老 J ...
- Kruskal 2015百度之星初赛2 HDOJ 5253 连接的管道
题目传送门 /* 最小生成树(Kruskal):以权值为头,带入两个端点,自然的排序;感觉结构体的并查集很好看 注意:题目老头要的是两个农田的高度差,中文水平不好,题意理解成和平均值的高度差! */ ...
- 2015baidu复赛2 连接的管道(mst && 优先队列prim)
连接的管道 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- HTTP协议----URI,URL,持久连接,管道与Cookie
URI与URL有什么不同呢? URI:Universal Resource Identifier统一资源标志符 URL:Universal Resource Locator统一资源定位器 URI是用来 ...
- HDU 5253 最小生成树 kruscal
Description 老 Jack 有一片农田,以往几年都是靠天吃饭的.但是今年老天格外的不开眼,大旱.所以老 Jack 决定用管道将他的所有相邻的农田全部都串联起来,这样他就可以从远处引水过来进行 ...
- hdu 5253 最小生成树
赤裸裸最小生成树,没啥说的,我用kruskal过的 /* * Author : ben */ #include <cstdio> #include <cstdlib> #inc ...
- HDU 5253 最小生成树(kruskal)+ 并查集
题目链接 #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> ...
随机推荐
- Spring REST实践之Spring Boot
Spring Boot基本描述 可以利用http://start.spring.io网站的进行Spring Boot的初始化构建.这个初始化构建器允许你输入工程基本信息.挑选工程支持的功能,最后会生成 ...
- ASP.NET MVC- ActionFilter的使用
ActionFilter是穿插在Action执行过程,在Action执行前后提供扩展的功能.ActionFilter用途非常的广,用在页面压缩.缓存.错误处理,登陆验证. ActionFilter的实 ...
- iOS开发-UIColor转UIIamge方法
只能说太神奇了,完美应用到我的毕业设计 - (UIImage*) createImageWithColor: (UIColor*) color { CGRect rect=CGRectMake(,,s ...
- 让AllocateHwnd接受一般函数地址作参数
http://www.xuebuyuan.com/1889769.html Classes单元的AllocateHWnd函数是需要传入一个处理消息的类的方法的作为参数的,原型: function Al ...
- Android Volley框架的使用(二)
此博文源码下载地址 https://github.com/Javen205/VolleyDemo.git 使用请求队列RequestQueue Volley中的Request都需要添加到Reque ...
- C#反射深入学习
C#反射 反射(Reflection)是.NET中的重要机制,通过放射,可以在运行时获得.NET中每一个类型(包括类.结构.委托.接口和枚举等)的成员,包括方法.属性.事件,以及构造函数等.还可以获得 ...
- Linux makefile 教程 很具体,且易懂
近期在学习Linux下的C编程,买了一本叫<Linux环境下的C编程指南>读到makefile就越看越迷糊,可能是我的理解能不行. 于是google到了下面这篇文章.通俗易懂.然后把它贴出 ...
- 【转】android开源项目和框架
特效: http://www.androidviews.net/ http://www.theultimateandroidlibrary.com/ 常用效果: 1. https://github ...
- Android应用增量更新
Original:https://github.com/cundong/SmartAppUpdates Backup:https://github.com/eltld/SmartAppUpdates
- PHP apache2.2 mysql 的安装
1.安装准备 我的系统为win7 旗舰版 php:http://windows.php.net/downloads/releases/php-5.3.6-Win32-VC9-x86.zip mysql ...