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 这是个贪心的题目,和网上的很多题解略有不同,我的贪心是从最下层开始,每次找到能使该层的牛的风险最小的方案, 记录风险值,上移一层,继续贪心. 最后从遍历每一 ...
随机推荐
- 基于better-scroll封装一个上拉加载下拉刷新组件
1.起因 上拉加载和下拉刷新在移动端项目中是很常见的需求,遂自己便基于better-scroll封装了一个下拉刷新上拉加载组件. 2.过程 better-scroll是目前比较好用的开源滚动库,提供很 ...
- CF508E (贪心+搜索+构造)
题目大意:让你构造一个括号序列,括号匹配的方式类似于栈,给出从左数每个括号 到和它匹配的右括号的 最小和最大距离,让你输出一个合法括号序列 看错题了以为是二分图,然后写了搜索 贪心发现如果距离往小了填 ...
- pat(A) 1066. Root of AVL Tree
代码: #include<iostream> #include<cstdio> #include<cmath> #include<stdlib.h> # ...
- xBIM 高级03 更改日志创建
系列目录 [已更新最新开发文章,点击查看详细] 模型中发生的每一个变化都是事务的一部分,这是我们设计的核心.所有事务都是由 IModel 的实现创建的,并且从中被弱引用,因此当使用 using ...
- sicily 题目分类
为了方便刷题,直接把分类保存下来方便来找. 转自:http://dengbaoleng.iteye.com/blog/1505083 [数据结构/图论] 1310Right-HeavyTree笛卡尔树 ...
- Windows 10 秋季更新错误
uefi 启动丢失修复 bcdboot c:\windows /s y: /f uefi /l zh-cn 0x80240034 你尝试安装的内部版本是有签名的外部测试版.若要继续安装,请启用外部测试 ...
- MyBatis数据持久化(六)resultMap使用
resultMap是MyBatis最强大也是最重要的特性,使用它我们可以將查询结果或者sql输入条件和一个HashMap的key进行映射,大大简化了sql查询条件的输入,而且使得数据的查询变得非常灵活 ...
- 你不知道的JavaScript(三)字符串
JavaScript字符串很容易被认为本质就是字符数组,ECMAScript规范中字符串作为一种单独的string类型,它的底层实现可能是数组,也可能是其他数据结构,因不同的JavaScript引擎而 ...
- Android 自定义View 之利用ViewPager 实现画廊效果(滑动放大缩小)
http://www.2cto.com/kf/201608/542107.html
- 【原创】JMS发布者订阅者【异步接收消息】
发布订阅模式和PTP方式不同之处为后者依赖于一个Topic话题: package com.thunisoft.jms.mine.topic; import java.util.HashMap; imp ...