Cow Ski Area
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2376 | Accepted: 668 |
Description
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.
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.
* 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.
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
USACO 2004 December Gold
这题模型就是问你最少加多少条边,整张图就是强连通图了
做的时候用G++提交,一直RE,换成C++的就过了,问了下九爷,原来是g++的堆栈比较浅造成的
这题模型就是问你最少加多少条边,整张图就是强连通图了
做的时候用G++提交,一直RE,换成C++的就过了,问了下九爷,原来是g++的堆栈比较浅造成的
#include<map>
#include<set>
#include<list>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 260000;
int dir[4][2] = { {1, 0}, {-1, 0}, {0, 1}, {0, -1} };
int mat[550][550];
int DFN[N];
int low[N];
int block[N];
int Stack[N];
int out[N];
int in[N];
bool instack[N];
int head[N];
int tot, sccnum, index, top, n, m;
struct node
{
int next;
int to;
}edge[N << 2];
void addedge(int from, int to)
{
edge[tot].to = to;
edge[tot].next = head[from];
head[from] = tot++;
}
void tarjan(int u)
{
DFN[u] = low[u] = ++index;
Stack[top++] = u;
instack[u] = 1;
for (int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if (DFN[v] == 0)
{
tarjan(v);
if (low[u] > low[v])
{
low[u] = low[v];
}
}
else if (instack[v])
{
if (low[u] > DFN[v])
{
low[u] = DFN[v];
}
}
}
if (DFN[u] == low[u])
{
sccnum++;
do
{
top--;
block[Stack[top]] = sccnum;
instack[Stack[top]] = 0;
}while (Stack[top] != u);
}
}
void solve(int ret)
{
memset( instack, 0, sizeof(instack) );
memset( DFN, 0, sizeof(DFN) );
memset( low, 0, sizeof(low) );
memset( in, 0, sizeof(in) );
memset( out, 0, sizeof(out) );
sccnum = index = top = 0;
for (int i = 0; i < ret; i++)
{
if (DFN[i] == 0)
{
tarjan(i);
}
}
if(sccnum == 1)
{
printf("0\n");
return ;
}
for (int i = 0; i < ret; i++)
{
for (int j = head[i]; j != -1; j = edge[j].next)
{
if (block[i] != block[edge[j].to])
{
out[block[i]]++;
in[block[edge[j].to]]++;
}
}
}
int a = 0, b = 0;
for (int i = 1; i <= sccnum; i++)
{
if (in[i] == 0)
{
a++;
}
if (out[i] == 0)
{
b++;
}
}
printf("%d\n", max(a, b));
}
bool is_legal(int x, int y)
{
if (x < 0 || x >= m || y < 0 || y >= n)
{
return false;
}
return true;
}
int main()
{
while (~scanf("%d%d", &n, &m))
{
memset( head, -1, sizeof(head) );
tot = 0;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
scanf("%d", &mat[i][j]);
}
}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
for (int k = 0; k < 4; k++)
{
int ii = i + dir[k][0];
int jj = j + dir[k][1];
if ( !is_legal(ii, jj) )
{
continue;
}
if(mat[i][j] < mat[ii][jj])
{
continue;
}
else
{
addedge(i * n + j, ii * n + jj);
}
}
}
}
solve(n * m);
}
return 0;
}