Cow Ski Area
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3323   Accepted: 919

Description

Farmer John's cousin, Farmer Ron, who lives in the mountains of Colorado, has recently taught his cows to ski. Unfortunately, his cows are somewhat timid and are afraid to ski among crowds of people at the local resorts, so FR has decided to construct his own private ski area behind his farm.

FR's ski area is a rectangle of width W and length L of 'land
squares' (1 <= W <= 500; 1 <= L <= 500). Each land square
is an integral height H above sea level (0 <= H <= 9,999). Cows
can ski horizontally and vertically between any two adjacent land
squares, but never diagonally. Cows can ski from a higher square to a
lower square but not the other way and they can ski either direction
between two adjacent squares of the same height.

FR wants to build his ski area so that his cows can travel between
any two squares by a combination of skiing (as described above) and ski
lifts. A ski lift can be built between any two squares of the ski area,
regardless of height. Ski lifts are bidirectional. Ski lifts can cross
over each other since they can be built at varying heights above the
ground, and multiple ski lifts can begin or end at the same square.
Since ski lifts are expensive to build, FR wants to minimize the number
of ski lifts he has to build to allow his cows to travel between all
squares of his ski area.

Find the minimum number of ski lifts required to ensure the cows can
travel from any square to any other square via a combination of skiing
and lifts.

Input

* Line 1: Two space-separated integers: W and L

* Lines 2..L+1: L lines, each with W space-separated integers corresponding to the height of each square of land.

Output

*
Line 1: A single integer equal to the minimal number of ski lifts FR
needs to build to ensure that his cows can travel from any square to any
other square via a combination of skiing and ski lifts

Sample Input

9 3
1 1 1 2 2 2 1 1 1
1 2 1 2 3 2 1 2 1
1 1 1 2 2 2 1 1 1

Sample Output

3
SB题,还花了好长时间,不开心,不写题解了。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define inf 0x3f3f3f3f
#define mod 10000
typedef long long ll;
using namespace std;
const int N=;
const int M=;
int s,t,n,m,cnt,tim,top,cut,k;
int head[M],dfn[M],low[M],stack1[M];
int num[M],in[M],out[M],vis[M],w[N][N];
int dis[][]= {,,,,-,,,-};
bool flag=false;
struct man {
int to,nxt;
} edg[M*];
void addedg(int u,int v) {
edg[cnt].to=v;
edg[cnt].nxt=head[u];
head[u]=cnt++;//printf("!!!%d %d\n",u,v);system("pause");
}
void init() {
cnt=;
tim=;
top=cut=k=;
memset(head,-,sizeof head);
memset(dfn,,sizeof dfn);
memset(low,,sizeof low);
memset(stack1,,sizeof stack1);
memset(num,,sizeof num);
memset(in,,sizeof in);
memset(out,,sizeof out);
memset(vis,,sizeof vis);
memset(edg,,sizeof edg);
memset(w,,sizeof w);
}
void Tarjan(int u) {
int v;
low[u] = dfn[u] = ++tim;
stack1[top++] = u;
vis[u] = ;
for(int e = head[u]; e != -; e = edg[e].nxt)
{
v = edg[e].to;
if(!dfn[v])
{
Tarjan(v);
low[u] = min(low[u], low[v]);
}
else if(vis[v])
{
low[u] = min(low[u], dfn[v]);
}
}
if(low[u] == dfn[u])
{
cut++;
do
{
v = stack1[--top];
num[v] = cut;
vis[v] = ;
}while(u != v);
}
}
void build(int i,int j,int d)
{
int xx=i+dis[d][];
int yy=j+dis[d][];
int u=i*m+j,v=xx*m+yy;
if(xx>=&&yy<m&&yy>=&&xx<n){
if(w[i][j]>=w[xx][yy])addedg(u,v);
if(w[i][j]<=w[xx][yy])addedg(v,u);
}
return;
}
int main() {
while(~scanf("%d%d",&m,&n)) {
init();
for(int i=; i<n; i++) {
for(int j=; j<m; j++) {
scanf("%d",&w[i][j]);
}
}
for(int i = ; i < n; i++) {
for(int j = ; j < m; j++) {
for(int d = ; d < ; d++) {
build(i,j,d);
}
}
}
for(int i=; i<n*m; i++)if(!dfn[i])Tarjan(i);
for(int i=; i<n*m; i++) {
for(int j=head[i]; j!=-; j=edg[j].nxt) {
int v=edg[j].to;
if(num[i]!=num[v])out[num[i]]++,in[num[v]]++;
}
}
int father=,son=;
for(int i=; i<=cut; i++) {
if(in[i]==)father++;
if(out[i]==)son++;
}
if(cut==)printf("0\n");
else printf("%d\n",max(father,son));
}
return ;
}

POJ2375 Cow Ski Area (强连通)(缩点)的更多相关文章

  1. POJ 2375 Cow Ski Area (强连通分量)

    题目地址:POJ 2375 对每一个点向与之相邻并h小于该点的点加有向边. 然后强连通缩点.问题就转化成了最少加几条边使得图为强连通图,取入度为0和出度为0的点数的较大者就可以.注意,当强连通分量仅仅 ...

  2. [USACO2004][poj2375]Cow Ski Area(在特殊图上用floodfill代替强联通算法)

    http://poj.org/problem?id=2375 题意:一个500*500的矩形,每个格子都有一个高度,不能从高度低的格子滑到高度高的格子(但相等高度可以滑),已知可以在2个相邻格子上加桥 ...

  3. POJ 2375 Cow Ski Area(强连通)

    POJ 2375 Cow Ski Area id=2375" target="_blank" style="">题目链接 题意:给定一个滑雪场, ...

  4. POJ 2375 Cow Ski Area

    Cow Ski Area Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original I ...

  5. D - Cow Ski Area

    Description Farmer John's cousin, Farmer Ron, who lives in the mountains of Colorado, has recently t ...

  6. poj 2375 Cow Ski Area bfs

    这个题目用tarjan找联通块,缩点,然后统计出入度为0的点理论上是可行的,但问题是会暴栈.考虑到这个题目的特殊性,可以直接用一次bfs找到数字相同且联通的块,这就是一个联通块,然后缩点,统计出入度即 ...

  7. POJ 2375 Cow Ski Area【tarjan】

    题目大意:一个W*L的山,每个山有个高度,当且仅当一个山不比它相邻(有公共边的格子)的山矮时能够滑过去,现在可以装化学电梯来无视山的高度滑雪,问最少装多少电梯使得任意两点都可到达 思路:最后一句话已经 ...

  8. POJ 2375 Cow Ski Area[连通分量]

    题目链接:http://poj.org/problem?id=2375题目大意:一片滑雪场,奶牛只能向相邻的并且不高于他当前高度的地方走.想加上缆车是的奶牛能从低的地方走向高的地方,求最少加的缆车数, ...

  9. BZOJ1051 [HAOI2006]受欢迎的牛 Tarjan 强连通缩点

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1051 题意概括 有n只牛,有m个羡慕关系. 羡慕关系具有传递性. 如果A羡慕B,B羡慕C,那么我们 ...

随机推荐

  1. 踏着前人的脚印学Hadoop——序列化,Writerable

    package org.apache.hadoop.io; import java.io.DataOutput;import java.io.DataInput;import java.io.IOEx ...

  2. Oracle中varchar,varchar2,nvarchar,nvarchar2的区别

    --varchar,varchar2 联系:1.varchar/varchar2用于存储可变长度的字符串比如varchar(20),存入字符串'abc',则数据库中该字段只占3个字节,而不是20个字节 ...

  3. C语言与MATLAB接口 编程与实例 李传军编着

    罗列一下以前自己学习C语言与MATLAB混编的笔记,顺便复习一遍. <C语言与MATLAB接口 编程与实例 李传军编着>(未看完,目前看到P106) 目录P4-8 ************ ...

  4. eclipse 提交作业到JobTracker Hadoop的数据类型要求必须实现Writable接口

    问:在eclipse中的写的代码如何提交作业到JobTracker中的哪?答:(1)在eclipse中调用的job.waitForCompletion(true)实际上执行如下方法 connect() ...

  5. 配置navigation bar外观

    /* 配置navigation bar外观开始 */ self.navigationBar.translucent = YES; self.navigationBar.titleTextAttribu ...

  6. rcc

    一.在STM32中,有五个时钟源,为HSI.HSE.LSI.LSE.PLL.   全名: high  speed  external ①HSI是高速内部时钟,RC振荡器,频率为8MHz. ②HSE是高 ...

  7. mysql 批量创建表,利用存储过程

    最近根据需求,需要提前创建一批日志表,以日期结尾,每天创建一张,例如XXX20160530,请参考如下: BEGIN    DECLARE `sName` VARCHAR(128);   DECLAR ...

  8. (转) function与感叹号

    原文:http://blog.sina.com.cn/s/blog_775f158f01016j12.html function与感叹号(转)(2012-08-29 12:29:12) 最近有空可以让 ...

  9. Hadoop MRUnit使用(一)

    之前在写MR job的时候,由于要在云梯,或者一淘的开发集群上运行:所以处理方法是,在本地打成jar包,然后scp到客户端网关机上,然后在提交job运行.这样的问题时,有时候如果遇到一些逻辑上的问题, ...

  10. Ubuntu虚机中SVN连接出错,虚机本机可正常CO,CIN,解决方法

    Ubuntu虚机中SVN连接出错,虚机本机可正常CO,CIN,外面机器无法正常连接. 解决: 虚机换个IP即可正常连接,原因不明,有可能为公司网管对该IP做了某些限制. PS:VMware中只需将网络 ...