二分图(行列变化)

来源:互联网 发布:小学生题目解答软件 编辑:程序博客网 时间:2024/04/29 19:48

HDU 4185 :

Problem Description
Thanks to a certain "green" resources company, there is a new profitable industry of oil skimming. There are large slicks of crude oil floating in the Gulf of Mexico just waiting to be scooped up by enterprising oil barons. One such oil baron has a special plane that can skim the surface of the water collecting oil on the water's surface. However, each scoop covers a 10m by 20m rectangle (going either east/west or north/south). It also requires that the rectangle be completely covered in oil, otherwise the product is contaminated by pure ocean water and thus unprofitable! Given a map of an oil slick, the oil baron would like you to compute the maximum number of scoops that may be extracted. The map is an NxN grid where each cell represents a 10m square of water, and each cell is marked as either being covered in oil or pure water.
 

Input
The input starts with an integer K (1 <= K <= 100) indicating the number of cases. Each case starts with an integer N (1 <= N <= 600) indicating the size of the square grid. Each of the following N lines contains N characters that represent the cells of a row in the grid. A character of '#' represents an oily cell, and a character of '.' represents a pure water cell.
 

Output
For each case, one line should be produced, formatted exactly as follows: "Case X: M" where X is the case number (starting from 1) and M is the maximum number of scoops of oil that may be extracted.
 

Sample Input
16.......##....##.......#.....##......
 

Sample Output
Case 1: 3
 
求图中"**"(竖着也可以)。
#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<set>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )typedef long long LL;typedef pair<int,int>pil;const int maxn=600+10;char s[maxn][maxn];int mp[maxn][maxn],id[maxn][maxn];int used[maxn],link[maxn];int dr[][2]={-1,0,1,0,0,-1,0,1};int t,n,cnt,cas;bool dfs(int x){    REP(i,cnt)    {        if(mp[x][i]&&!used[i])        {            used[i]=true;            if(link[i]==-1||dfs(link[i]))            {                link[i]=x;                return true;            }        }    }    return false;}void work(){    int res=0;    CLEAR(link,-1);    REP(i,cnt)    {        CLEAR(used,0);        if(dfs(i)) res++;    }    printf("Case %d: %d\n",cas++,res/2);}bool ok(int x,int y){    if(x<0||x>=n||y<0||y>=n)  return false;    return  true;}int main(){    cas=1;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        cnt=1;        CLEAR(id,0);        REP(i,n)        {            scanf("%s",s[i]);            REP(j,n)              if(s[i][j]=='#') id[i][j]=cnt++;//编号        }        CLEAR(mp,0);        REP(i,n)        {            REP(j,n)            {                if(id[i][j])                {                    REP(k,4)                    {                        int xx=i+dr[k][0];                        int yy=j+dr[k][1];                        if(ok(xx,yy)&&id[xx][yy])                            mp[id[i][j]][id[xx][yy]]=1;                    }                }            }        }        work();    }    return 0;}

POJ 3020:

Description

The Global Aerial Research Centre has been allotted the task of building the fifth generation of mobile phone nets in Sweden. The most striking reason why they got the job, is their discovery of a new, highly noise resistant, antenna. It is called 4DAir, and comes in four types. Each type can only transmit and receive signals in a direction aligned with a (slightly skewed) latitudinal and longitudinal grid, because of the interacting electromagnetic field of the earth. The four types correspond to antennas operating in the directions north, west, south, and east, respectively. Below is an example picture of places of interest, depicted by twelve small rings, and nine 4DAir antennas depicted by ellipses covering them. 
 
Obviously, it is desirable to use as few antennas as possible, but still provide coverage for each place of interest. We model the problem as follows: Let A be a rectangular matrix describing the surface of Sweden, where an entry of A either is a point of interest, which must be covered by at least one antenna, or empty space. Antennas can only be positioned at an entry in A. When an antenna is placed at row r and column c, this entry is considered covered, but also one of the neighbouring entries (c+1,r),(c,r+1),(c-1,r), or (c,r-1), is covered depending on the type chosen for this particular antenna. What is the least number of antennas for which there exists a placement in A such that all points of interest are covered? 

Input

On the first row of input is a single positive integer n, specifying the number of scenarios that follow. Each scenario begins with a row containing two positive integers h and w, with 1 <= h <= 40 and 0 < w <= 10. Thereafter is a matrix presented, describing the points of interest in Sweden in the form of h lines, each containing w characters from the set ['*','o']. A '*'-character symbolises a point of interest, whereas a 'o'-character represents open space. 

Output

For each scenario, output the minimum number of antennas necessary to cover all '*'-entries in the scenario's matrix, on a row of its own.

Sample Input

27 9ooo**oooo**oo*ooo*o*oo**o**ooooooooo*******ooo*o*oo*oo*******oo10 1***o******

Sample Output

175

Source

Svenskt Mästerskap i Programmering/Norgesmesterskapet 2001

#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<set>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )typedef long long LL;typedef pair<int,int>pil;const int maxn=500+10;char s[maxn][maxn];int mp[maxn][maxn],id[maxn][maxn];int used[maxn],link[maxn];int dr[][2]={-1,0,1,0,0,-1,0,1};int t,n,m,cnt,cas;bool dfs(int x){    REPF(i,1,cnt)    {        if(mp[x][i]&&!used[i])        {            used[i]=true;            if(link[i]==-1||dfs(link[i]))            {                link[i]=x;                return true;            }        }    }    return false;}void work(){    int res=0;    CLEAR(link,-1);    REPF(i,1,cnt-1)    {        CLEAR(used,0);        if(dfs(i)) res++;    }    printf("%d\n",cnt-1-res/2);}bool ok(int x,int y){    if(x<0||x>=n||y<0||y>=m)  return false;    return  true;}int main(){    cas=1;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&m);        cnt=1;        CLEAR(id,0);        REP(i,n)        {            scanf("%s",s[i]);            REP(j,m)              if(s[i][j]=='*') id[i][j]=cnt++;//编号        }        CLEAR(mp,0);        REP(i,n)        {            REP(j,m)            {                if(id[i][j])                {                    REP(k,4)                    {                        int xx=i+dr[k][0];                        int yy=j+dr[k][1];                        if(ok(xx,yy)&&id[xx][yy])                            mp[id[i][j]][id[xx][yy]]=1;                    }                }            }        }        work();    }    return 0;}


0 0