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 ...
随机推荐
- 深入探索C++对象模型-语义
有三种情况,这将是一个object的内容,以及一class object早期值: class X { ... }; X x; X xx = x; // 情况1,赋值对象 e ...
- Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.StringUtils
1.错误叙述性说明 2014-7-10 23:12:23 org.apache.catalina.core.StandardContext filterStart 严重: Exception star ...
- Mvc后台接收 参数
@Html.TextAreaFor(m => m.Emps, new { @class = "easyui-validatebox", @style = "heig ...
- SQL集合运算 差集 并集 交
SQL-3标准中提供了三种对检索结果进行集合运算的命令:并集UNION:交集INTERSECT:差集EXCEPT(在Oracle中叫做 MINUS).在有些数据库中对此的支持不够充分,如MySql中只 ...
- iOS Foundation 框架基类
iOS Foundation 框架基类 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转 ...
- cheese desktop内容
#!/usr/bin/env xdg-open [Desktop Entry] Encoding=UTF- Version=1.0 Type=Application Terminal=false Na ...
- [Windows Phone]解锁、注册Windows Phone实体手机为开发机(Windows 8)
原文:[Windows Phone]解锁.注册Windows Phone实体手机为开发机(Windows 8) 前言 ? ? 最近要开发Windows Phone(以下简称WP)的手机游戏,由於使用模 ...
- 制作简易计算器处理过程Servlet
CalculationServlet.java: package com.you.servlet; import java.io.IOException; import java.io.PrintWr ...
- 注意事项: Solr设备 Hello World
试用 Solr-4.10.2 一 shards, 这两款机器 一是垃圾 rm -r example/solr/collection1/data/* 启动一个 node cd example java ...
- 【C语言探索之旅】 第一部分第五课:运算那点事
内容简介 1.课程大纲 2.第一部分第五课:运算那点事 3.第一部分第六课预告:条件表达式 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答案.还会带大家用C语言编写三个游戏 ...