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> ...
随机推荐
- DateTable与List<T>相互转换 及JSON与DataTable(DataSet)相互转化
http://www.360doc.com/content/13/0712/09/10504424_299336674.shtml Linq处理List数据 http://blog.163.com/l ...
- c# implicit explicit关键字(隐式和显式数据类型转换)
implicit关键字用于声明隐式的用户定义类型转换运算符.(explicit反之)explicit则用于显示转换用户自定义类型.static implicit operator target_typ ...
- MPIO配置
设置好两块网卡的IP(实用同一段IP,或者不同网段IP均可以配置多路径)iSCSI发起程序配置:1.添加发现(默认设置即可)2.目标-连接-高级:分别配置 本地适配器.发起程序IP.目标门户IP(此处 ...
- c++训练营--重载
// demo1.cpp : 定义控制台应用程序的入口点. //通过此例程了解重载 #include "stdafx.h" #include <iostream> us ...
- python学习进阶一
map()函数 def format_name(s): return s.capitalize() print map(format_name, ['adam', 'LISA', 'barT']) R ...
- Codeforces Round #310 (Div. 2) A. Case of the Zeros and Ones 水题
A. Case of the Zeros and Ones Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/con ...
- VK Cup 2012 Qualification Round 2 C. String Manipulation 1.0 字符串模拟
C. String Manipulation 1.0 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 codeforces.com/problemset/pr ...
- [Jobdu] 题目1527:首尾相连数组的最大子数组和
题目描述: 给定一个由N个整数元素组成的数组arr,数组中有正数也有负数,这个数组不是一般的数组,其首尾是相连的.数组中一个或多个连续元素可以组成一个子数组,其中存在这样的子数组arr[i],…arr ...
- SharePoint 2013 中使用 JavaScript Like 和Unlike list item/page/document
SharePoint 2013中新增了很多社交功能,比如用户可以like/unlike 任何一个 list item/page/document,这是一个非常不错的功能. 但有时觉得like/unli ...
- [AngularJS] Best Practise - Minification and annotation
Annotation Order: It's considered good practice to dependency inject Angular's providers in before o ...