POJ 2375 Cow Ski Area

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的点数)
 
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <algorithm>
  6 #include <climits>
  7 #include <vector>
  8 #include <queue>
  9 #include <cstdlib>
 10 #include <string>
 11 #include <set>
 12 #include <stack>
 13 #define LL long long
 14 #define pii pair<int,int>
 15 #define INF 0x3f3f3f3f
 16 using namespace std;
 17 const int maxn = 250000;
 18 struct arc {
 19     int to,next;
 20     arc(int x = 0,int y = -1) {
 21         to = x;
 22         next = y;
 23     }
 24 };
 25 arc e[1001000];
 26 int head[maxn],dfn[maxn],belong[maxn],low[maxn],in[maxn],out[maxn];
 27 int tot,scc,idx,n,W,L;
 28 bool instack[maxn];
 29 int mystack[maxn],top;
 30 void add(int u,int v) {
 31     e[tot] = arc(v,head[u]);
 32     head[u] = tot++;
 33 }
 34 void tarjan(int u) {
 35     dfn[u] = low[u] = ++idx;
 36     mystack[top++] = u;
 37     instack[u] = true;
 38     for(int i = head[u]; ~i; i = e[i].next) {
 39         if(!dfn[e[i].to]) {
 40             tarjan(e[i].to);
 41             low[u] = min(low[u],low[e[i].to]);
 42         } else if(instack[e[i].to]) low[u] = min(low[u],dfn[e[i].to]);
 43     }
 44     if(dfn[u] == low[u]) {
 45         scc++;
 46         int v;
 47         do {
 48             v = mystack[--top];
 49             instack[v] = false;
 50             belong[v] = scc;
 51         } while(v != u);
 52     }
 53 }
 54 void init() {
 55     for(int i = 0; i < maxn; ++i) {
 56         dfn[i] = low[i] = belong[i] = 0;
 57         instack[i] = false;
 58         in[i] = out[i] = 0;
 59     }
 60     top = tot = idx = scc = 0;
 61     memset(head,-1,sizeof(head));
 62 }
 63 int mp[800][800];
 64 int main() {
 65     const int dir[4][2] = {-1,0,1,0,0,1,0,-1};
 66     while(~scanf("%d %d",&W,&L)) {
 67         n = W*L;
 68         init();
 69         for(int i = 0; i < L; ++i)
 70             for(int j = 0; j < W; ++j)
 71                 scanf("%d",mp[i]+j);
 72 
 73         for(int i = 0; i < L; ++i)
 74             for(int j = 0; j < W; ++j)
 75                 for(int k = 0; k < 4; ++k) {
 76                     int ti = i + dir[k][0];
 77                     int tj = j + dir[k][1];
 78                     if(ti < 0 || ti >= L || tj < 0 || tj >= W) continue;
 79                     if(mp[ti][tj] <= mp[i][j]) add(i*W+j,ti*W+tj);
 80                 }
 81         for(int i = 0; i < n; ++i) if(!dfn[i]) tarjan(i);
 82         if(scc < 2) puts("0");
 83         else{
 84             int x = 0,y = 0;
 85             for(int i = 0; i < n; ++i){
 86                 for(int j = head[i]; ~j; j = e[j].next){
 87                     if(belong[i] == belong[e[j].to]) continue;
 88                     in[belong[e[j].to]]++;
 89                     out[belong[i]]++;
 90                 }
 91             }
 92             for(int i = 1; i <= scc; ++i){
 93                 if(!in[i]) x++;
 94                 if(!out[i]) y++;
 95             }
 96             printf("%d
",max(x,y));
 97         }
 98     }
 99     return 0;
100 }
View Code
原文地址:https://www.cnblogs.com/crackpotisback/p/4070418.html