POJ 2375 Cow Ski Area
Cow Ski Area
This problem will be judged on PKU. Original ID: 2375
64-bit integer IO format: %lld Java class name: Main
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.
Source
#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的更多相关文章
- POJ 2375 Cow Ski Area(强连通)
POJ 2375 Cow Ski Area id=2375" target="_blank" style="">题目链接 题意:给定一个滑雪场, ...
- POJ 2375 Cow Ski Area (强连通分量)
题目地址:POJ 2375 对每一个点向与之相邻并h小于该点的点加有向边. 然后强连通缩点.问题就转化成了最少加几条边使得图为强连通图,取入度为0和出度为0的点数的较大者就可以.注意,当强连通分量仅仅 ...
- 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【tarjan】
题目大意:一个W*L的山,每个山有个高度,当且仅当一个山不比它相邻(有公共边的格子)的山矮时能够滑过去,现在可以装化学电梯来无视山的高度滑雪,问最少装多少电梯使得任意两点都可到达 思路:最后一句话已经 ...
- POJ2375 Cow Ski Area (强连通)(缩点)
Cow Ski Area Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- D - Cow Ski Area
Description Farmer John's cousin, Farmer Ron, who lives in the mountains of Colorado, has recently t ...
- [USACO2004][poj2375]Cow Ski Area(在特殊图上用floodfill代替强联通算法)
http://poj.org/problem?id=2375 题意:一个500*500的矩形,每个格子都有一个高度,不能从高度低的格子滑到高度高的格子(但相等高度可以滑),已知可以在2个相邻格子上加桥 ...
- POJ 3045 Cow Acrobats (贪心)
POJ 3045 Cow Acrobats 这是个贪心的题目,和网上的很多题解略有不同,我的贪心是从最下层开始,每次找到能使该层的牛的风险最小的方案, 记录风险值,上移一层,继续贪心. 最后从遍历每一 ...
随机推荐
- windows下Word使用-快捷键
1.word全屏显示——Alt+U+V 2.上标——Ctrl+Shift+= 3.下标——Ctrl+=
- hive初体验
--创建表 create table t_order(id int,name string,phone string) row format delimited fields terminated b ...
- vue懒加载
vue懒加载(白屏或者加载慢的解决方法) 懒加载:也叫延迟加载,即在需要的时候进行加载,随用随载. 为什么需要懒加载? 像vue这种单页面应用,如果没有应用懒加载,运用webpack打包后的文件将会异 ...
- 常用的ES6方法
常用的ES6方法 ES6之后,新增了定义变量的两个关键字,分别是let和const. let和const都能够声明块级作用域,用法和var是类似的,let的特点是不会变量提升,而是被锁在当前块中. 实 ...
- MyBatis学习总结(4)——解决字段名与实体类属性名不相同的冲突
一.准备演示需要使用的表和数据 CREATE TABLE orders( order_id INT PRIMARY KEY AUTO_INCREMENT, order_no VARCHAR(20), ...
- 2016 10 28考试 dp 乱搞 树状数组
2016 10 28 考试 时间 7:50 AM to 11:15 AM 下载链接: 试题 考试包 这次考试对自己的表现非常不满意!! T1看出来是dp题目,但是在考试过程中并没有推出转移方程,考虑了 ...
- Java实现把两个数组合并为一个的方法总结
本文实例讲述了Java实现把两个数组合并为一个的方法.分享给大家供大家参考,具体如下: 在Java中,如何把两个String[]合并为一个? 看起来是一个很简单的问题.但是如何才能把代码写得高效简洁, ...
- jquery-fakeloader插件的使用
jquery-fakeloader插件示例代码 link rel="stylesheet" href="../../Content/fakeLoader.css" ...
- 极路由4pro(HC5962)设置阿里云DDNS
v2ex有个帖子说用Dnspod的API可以一行搞定,不过我既然买的是阿里云的域名还是想尽量用阿里云的API,感觉比较安全,另外修改解析记录后也会自动发邮件通知,所以还是调用阿里云的API吧.阿里云的 ...
- hdu 4882 ZCC Loves Codefires(贪心)
# include<stdio.h> # include <algorithm> # include <string.h> using namespace std; ...