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 ...
随机推荐
- [网络]_[0基础]_[使用putty备份远程数据]
场景: 1. putty是windows上訪问linux服务的免费client之中的一个.用它来ssh到远程server备份数据是常见的做法(在没做好自己主动备份机制前), 通过putty界面尽管也不 ...
- Hbase0.96源码之HMaster(二)Hmaster主要循环becomeActiveMaster
1,Hmaster主循环主要这里主要有: 1,1 becomeActiveMaster(startupStatus); 1.2 finishInitialization 1.3 loop() beco ...
- play framework2.5.
play framework2 的学习笔记 https://github.com/playframework/playframework https://github.com/playframewor ...
- 采用ACE登录设施(一)HelloWorld
(1)开始使用日志设施 使用日志设施,总是要包括头文件: #include "ace/Log_Msg.h" ACE日志的Hello World #ifdef _DEBUG #pra ...
- linux、hdfs、hive、hbase经常使用的命令
linux经常使用命令 pwd 查看当前工作文件夹的绝对路径 cat input.txt 查看input.txt文件的内容 ls 显示当前文件夹下全部的文件及子文件夹 rm recommender-d ...
- Java 大数类
划分结果存在数组.供应商下标0 在剩下的标记1 import java.math.BigInteger; import java.util.Scanner; public class Main { p ...
- ASIHTTPRequest 对GET POST 请求简包
1.ASIHTTPRequest一个简短的引论 github下载链接https://github.com/pokeb/asi-http-request 2.ASIHTTPRequest 对GET和PO ...
- 数据收集程序一般建筑(C++ ACE达到)
数据收集程序一般功能 经socket数据的接收的另一侧.端方能够访问智能电表采集器,你可曾与计算机之间的通信的通信协议(你良好的一致性是谁client,谁是服务端,即数据流) 为收集程序要求 可扩展: ...
- FZU1669 Right-angled Triangle【毕达哥拉斯三元组】
主题链接: pid=1669">http://acm.fzu.edu.cn/problem.php?pid=1669 题目大意: 求满足以a.b为直角边,c为斜边,而且满足a + b ...
- 在java代码中获取JVM参数(转)
近日关注性能调优,关注JMX,发现java.lang.management.*之强大.同时查阅了资料,整合一版关于JVM参数获取的note,仅供参考: MemoryMXBean memorymbean ...