D - Cow Ski Area
Description
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
* Lines 2..L+1: L lines, each with W space-separated integers corresponding to the height of each square of land.
Output
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
Hint
OUTPUT DETAILS:
FR builds the three lifts. Using (1, 1) as the lower-left corner,
the lifts are (3, 1) <-> (8, 2), (7, 3) <-> (5, 2), and (1, 3) <->
(2, 2). All locations are now connected. For example, a cow wishing
to travel from (9, 1) to (2, 2) would ski (9, 1) -> (8, 1) -> (7,
1) -> (7, 2) -> (7, 3), take the lift from (7, 3) -> (5, 2), ski
(5, 2) -> (4, 2) -> (3, 2) -> (3, 3) -> (2, 3) -> (1, 3), and then
take the lift from (1, 3) - > (2, 2). There is no solution using
fewer than three lifts.
- #include<stdio.h>
- #include<string.h>
- #include<stack>
- #define INF 1000010
- #define P 261010
- using namespace std;
- stack<int>S;
- int a[550][550],map[P][10],low[P],dfn[P];
- int instack[P];
- int bnt,be[P],n,m;
- int index,in[P],out[P];
- void build(){
- for(int i=1;i<=n;i++)
- for(int j=1;j<=m;j++){
- if(a[i][j]>=a[i-1][j])map[j+m*i-m][++map[j+m*i-m][0]] = j+(i-2)*m;
- if(a[i][j]>=a[i+1][j])map[j+m*i-m][++map[j+m*i-m][0]] = j+i*m;
- if(a[i][j]>=a[i][j+1])map[j+m*i-m][++map[j+m*i-m][0]] = j+(i-1)*m+1;
- if(a[i][j]>=a[i][j-1])map[j+m*i-m][++map[j+m*i-m][0]] = j+(i-1)*m-1;
- }
- }
- void tarjan(int i){
- dfn[i] = low[i] = ++index;
- S.push(i);
- instack[i] = 1;
- for(int j = 1;j<=map[i][0];j++){
- int k = map[i][j];
- if(!dfn[k]){
- tarjan(k);
- low[i] = min(low[i],low[k]);
- }
- else if(instack[k]){
- low[i] = min(low[i],dfn[k]);
- }
- }
- if(dfn[i]==low[i]){
- bnt++;
- int kk;
- do{
- kk = S.top();
- S.pop();
- instack[kk] = 0;
- be[kk] = bnt;
- }while(i!=kk);
- }
- }
- int main(){
- while(~scanf("%d%d",&m,&n)){
- int ans,ans1;
- memset(dfn,0,sizeof(dfn));
- memset(in,0,sizeof(in));
- memset(out,0,sizeof(out));
- memset(instack,0,sizeof(instack));
- bnt = ans = ans1 = index = 0;
- for(int i=0;i<=n+1;i++)a[i][0] = a[i][m+1] = INF;
- for(int j=0;j<=m+1;j++)a[0][j] = a[n+1][j] = INF;
- for(int i=1;i<=n;i++){
- for(int j=1;j<=m;j++){
- scanf("%d",&a[i][j]);
- map[j+(i-1)*m][0] = 0;
- }
- }
- build();
- for(int i=1;i<=n*m;i++)
- if(!dfn[i])
- tarjan(i);
- for(int i=1;i<=n*m;i++){
- for(int j =1;j<=map[i][0];j++)
- if(be[i]!=be[map[i][j]]){
- out[be[i]]++;
- in[be[map[i][j]]]++;
- }
- }
- for(int i=1;i<=bnt;i++){
- if(out[i]==0)ans++;
- if(in[i]==0)ans1++;
- }
- if(bnt==1)printf("0\n");
- else printf("%d\n",max(ans,ans1));
- }
- }
- 在POJ能过,在杭电就过不了,是什么原因啊,。。。。。。??!坑爹啊
D - Cow Ski Area的更多相关文章
- POJ2375 Cow Ski Area (强连通)(缩点)
Cow Ski Area Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- POJ 2375 Cow Ski Area(强连通)
POJ 2375 Cow Ski Area id=2375" target="_blank" style="">题目链接 题意:给定一个滑雪场, ...
- POJ 2375 Cow Ski Area
Cow Ski Area Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original I ...
- [USACO2004][poj2375]Cow Ski Area(在特殊图上用floodfill代替强联通算法)
http://poj.org/problem?id=2375 题意:一个500*500的矩形,每个格子都有一个高度,不能从高度低的格子滑到高度高的格子(但相等高度可以滑),已知可以在2个相邻格子上加桥 ...
- POJ 2375 Cow Ski Area[连通分量]
题目链接:http://poj.org/problem?id=2375题目大意:一片滑雪场,奶牛只能向相邻的并且不高于他当前高度的地方走.想加上缆车是的奶牛能从低的地方走向高的地方,求最少加的缆车数, ...
- poj 2375 Cow Ski Area bfs
这个题目用tarjan找联通块,缩点,然后统计出入度为0的点理论上是可行的,但问题是会暴栈.考虑到这个题目的特殊性,可以直接用一次bfs找到数字相同且联通的块,这就是一个联通块,然后缩点,统计出入度即 ...
- POJ 2375 Cow Ski Area (强连通分量)
题目地址:POJ 2375 对每一个点向与之相邻并h小于该点的点加有向边. 然后强连通缩点.问题就转化成了最少加几条边使得图为强连通图,取入度为0和出度为0的点数的较大者就可以.注意,当强连通分量仅仅 ...
- POJ 2375 Cow Ski Area【tarjan】
题目大意:一个W*L的山,每个山有个高度,当且仅当一个山不比它相邻(有公共边的格子)的山矮时能够滑过去,现在可以装化学电梯来无视山的高度滑雪,问最少装多少电梯使得任意两点都可到达 思路:最后一句话已经 ...
- 杭电ACM分类
杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...
随机推荐
- 小米2S TWRP 3.0.2-0 最新版Recovery
主界面 使用了我最新修改的内核 下载地址: 链接: http://pan.baidu.com/s/1i5xwddb 密码: 7dyb 验证信息: md5sum: dca410f33020eb87986 ...
- Echache整合Spring缓存实例讲解(转)
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主要介绍了EhCache,并通过整合Spring给出了一个使用实例. 一.EhCac ...
- c++爱问的面试问题
1.static_cast,dynamic_cast,reinterpret_cast,const_cast四种转换. 2.const行为 3.malloc/free, new/delete差额 4. ...
- Linux下/proc目录简介(转)
1. /proc目录Linux 内核提供了一种通过 /proc 文件系统,在运行时访问内核内部数据结构.改变内核设置的机制.proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间.它以文 ...
- Mybatis简单的入门之增删改查
一般的过程例如以下 1.加入Mybatis所须要的包,和连接数据库所需的包 2.配置mybatis-config.xml文件 3.配置与pojo相应的映射文件 mybatis-config,xml & ...
- 使用form的target属性屏蔽url跳
target: 指定公开赛, action URL. 关键点: 让target指向隐藏的iframe demo: form.jsp <%@ page language="java&qu ...
- 用PowerDesigner生成自定义建表语句
原文:用PowerDesigner生成自定义建表语句 我们经常用PowerDesigner来进行数据库表结构的设计,并且设计出来的表比较直观的看出之间的相互关系,方便理解:但其自动生成的脚本并不一定符 ...
- hbase开放lzo压缩
hbase仅仅支持对gzip的压缩,对lzo压缩支持不好. 在io成为系统瓶颈的情况下,一般开启lzo压缩会提高系统的吞吐量. 但这须要參考详细的应用场景,即是否值得进行压缩.压缩率是否足够等等. ...
- 【Java GUI】Java面板基础:JPanel
有两个面板,常见的面板(JPanel)和滚动面板(JScrollPane) Jpanel 面板是一种常见的容器,JPanel的作用是实现接口层次结构,面放入一些组件.也能够在上面绘画,将放有组件和有画 ...
- net搭建热插拔式web框架(沙箱的构建)
net搭建热插拔式web框架(沙箱的构建) 上周五写了一个实现原理篇,在评论中看到有朋友也遇到了我的问题,真的是有种他乡遇知己的感觉,整个系列我一定会坚持写完,并在最后把代码开源到git中.上一篇文章 ...