Cow Ski Area

Time Limit: 1000ms
Memory Limit: 65536KB

This problem will be judged on PKU. Original ID: 2375
64-bit integer IO format: %lld      Java class name: Main

 
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

Hint

This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.

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.

 

Source

 
解题:强连通缩点求max(入度为0的点数,出度为0的点数)
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct arc {
int to,next;
arc(int x = ,int y = -) {
to = x;
next = y;
}
};
arc e[];
int head[maxn],dfn[maxn],belong[maxn],low[maxn],in[maxn],out[maxn];
int tot,scc,idx,n,W,L;
bool instack[maxn];
int mystack[maxn],top;
void add(int u,int v) {
e[tot] = arc(v,head[u]);
head[u] = tot++;
}
void tarjan(int u) {
dfn[u] = low[u] = ++idx;
mystack[top++] = u;
instack[u] = true;
for(int i = head[u]; ~i; i = e[i].next) {
if(!dfn[e[i].to]) {
tarjan(e[i].to);
low[u] = min(low[u],low[e[i].to]);
} else if(instack[e[i].to]) low[u] = min(low[u],dfn[e[i].to]);
}
if(dfn[u] == low[u]) {
scc++;
int v;
do {
v = mystack[--top];
instack[v] = false;
belong[v] = scc;
} while(v != u);
}
}
void init() {
for(int i = ; i < maxn; ++i) {
dfn[i] = low[i] = belong[i] = ;
instack[i] = false;
in[i] = out[i] = ;
}
top = tot = idx = scc = ;
memset(head,-,sizeof(head));
}
int mp[][];
int main() {
const int dir[][] = {-,,,,,,,-};
while(~scanf("%d %d",&W,&L)) {
n = W*L;
init();
for(int i = ; i < L; ++i)
for(int j = ; j < W; ++j)
scanf("%d",mp[i]+j); for(int i = ; i < L; ++i)
for(int j = ; j < W; ++j)
for(int k = ; k < ; ++k) {
int ti = i + dir[k][];
int tj = j + dir[k][];
if(ti < || ti >= L || tj < || tj >= W) continue;
if(mp[ti][tj] <= mp[i][j]) add(i*W+j,ti*W+tj);
}
for(int i = ; i < n; ++i) if(!dfn[i]) tarjan(i);
if(scc < ) puts("");
else{
int x = ,y = ;
for(int i = ; i < n; ++i){
for(int j = head[i]; ~j; j = e[j].next){
if(belong[i] == belong[e[j].to]) continue;
in[belong[e[j].to]]++;
out[belong[i]]++;
}
}
for(int i = ; i <= scc; ++i){
if(!in[i]) x++;
if(!out[i]) y++;
}
printf("%d\n",max(x,y));
}
}
return ;
}

POJ 2375 Cow Ski Area的更多相关文章

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

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

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

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

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

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

  4. poj 2375 Cow Ski Area bfs

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

  5. POJ 2375 Cow Ski Area【tarjan】

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

  6. POJ2375 Cow Ski Area (强连通)(缩点)

                                        Cow Ski Area Time Limit: 1000MS   Memory Limit: 65536K Total Sub ...

  7. D - Cow Ski Area

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

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

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

  9. POJ 3045 Cow Acrobats (贪心)

    POJ 3045 Cow Acrobats 这是个贪心的题目,和网上的很多题解略有不同,我的贪心是从最下层开始,每次找到能使该层的牛的风险最小的方案, 记录风险值,上移一层,继续贪心. 最后从遍历每一 ...

随机推荐

  1. 利用请求的JSON数据创建图形图层

    先看效果图: 包含三个部分:信息窗口(标题是要素的某个属性信息,其余是感兴趣的属性信息).图上图形按照某一属性大小不一显示,图例 1.创建底图用于存放以上三部分: "esri/Map&quo ...

  2. docker mysql pxc集群(percona-xtradb-cluster)

    docker pull percona/percona-xtradb-cluster docker tag percona/percona-xtradb-cluster pxc docker netw ...

  3. HDU 1222 Wolf and Rabbit( 简单拓欧 )

    链接:传送门 题意:狼抓兔子,狼从 0 出发沿逆时针寻找兔子,每走一步的距离为 m ,所有洞窟的编号为 0 - n-1 ,问是否存在一个洞窟使得兔子能够安全躲过无数次狼的搜捕. 思路:简单的拓展欧几里 ...

  4. [读书笔记] Python数据分析 (一) 准备工作

    1. python中数据结构:矩阵,数组,数据框,通过关键列相互联系的多个表(SQL主键,外键),时间序列 2. python 解释型语言,程序员时间和CPU时间衡量,高频交易系统 3. 全局解释器锁 ...

  5. Linux系统串口接收数据编

    http://blog.csdn.net/bg2bkk/article/details/8668576 之前基于IBM deveplopworks社区的代码,做了串口初始化和发送的程序,今天在此基础上 ...

  6. Vue框架Element UI教程-axios请求数据

    Element UI手册:https://cloud.tencent.com/developer/doc/1270 中文文档:http://element-cn.eleme.io/#/zh-CN gi ...

  7. 【 【henuacm2016级暑期训练】动态规划专题 G】 Palindrome pairs

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 先用枚举回文串中点的方法. 得到这个字符串中出现的所有的回文. 得到他们的左端点以及右端点. 整理成一个pair<int,in ...

  8. ubuntu 休眠之后蓝牙鼠标无效果。

    ubuntu链接蓝牙鼠标之后.左上角蓝牙标志左下角应该有一个锁的标志. 可是休眠之后,蓝牙鼠标失效,锁没有了,点击按键,出来锁之后,立即消失. 运行两次例如以下命令能够解决: sudo hciconf ...

  9. Android用canvas画哆啦A梦

    先上图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/diss ...

  10. angular与angularjs常用指令的不同写法整理

    angularjs与angular 常用的指令写法的区别; 一:angularjs指令 1.ng-bind 使用给定的变量或表达式的值来替换 HTML 元素的内容 <p ng-bind=&quo ...