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 这是个贪心的题目,和网上的很多题解略有不同,我的贪心是从最下层开始,每次找到能使该层的牛的风险最小的方案, 记录风险值,上移一层,继续贪心. 最后从遍历每一 ...
随机推荐
- 一些AngularJs
# AngularJs部分 # 详情可参考文档----依赖注入--不是主动地获取而是被动的接收,需要什么就要什么,这样灵活较高,如:$scope ----指令--内部:ng- 如:ng- ...
- [读书笔记] R语言实战 (一) R语言介绍
典型数据分析的步骤: R语言:为统计计算和绘图而生的语言和环境 数据分析:统计学,机器学习 R的使用 1. 区分大小写的解释型语言 2. R语句赋值:<- 3. R注释: # 4. 创建向量 c ...
- python3 将两个列表生成一个字典
需求: 存在两个list如下 list1 = ["one", "two", "three"] list2 = ["1", ...
- 《你又怎么了我错了行了吧》【Beta】Scrum Meeting 2
第二天 日期:2019/6/25 前言: 第2次会议在女生宿舍召开 确认编码阶段已经完成,继续测试项目 1.1 今日完成任务情况以及明日任务安排 姓名 当前阶段任务 下一阶段任务 刘 佳 完善了未开发 ...
- [luogu]P4365[九省联考]秘密袭击coat(非官方正解)
题目背景 警告:滥用本题评测者将被封号 We could have had it all. . . . . . 我们本该,拥有一切 Counting on a tree. . . . . . 何至于此 ...
- 题解 P3377 【【模板】左偏树(可并堆)】
所谓的左偏树,是一种可并堆的实现. 这种数据结构能够支持高效的堆合并,但是不支持查询节点等操作,因此不同于平衡树,它的结构是不平衡的. 左偏树满足如下两条基本性质: 1. 堆的性质 这也就是说左偏树每 ...
- CSS3特效——六面体
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 楼宇自控-BA系统流程总图
总结一下过程中的节点和技能,希望能对其他人有所帮助
- what's new in vc2015
1. 变量和函数的注解提示非常实用.象C#了. 2.CStdioFile升级了,不再须要象 vc2013中,用CStdioFileEx来修复错误了. 3. 发现再写.
- 【MFC设置静态文本框背景为透明】
视图类中加入OnCtlColor()函数: IDC_STATIC1为静态文本框ID HBRUSH CAngleView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT n ...