hdu4185(二分图+最大匹配)

来源:互联网 发布:会计课程 知乎 编辑:程序博客网 时间:2024/06/05 11:35

Oil Skimming

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 562    Accepted Submission(s): 247


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
 

Source
The 2011 South Pacific Programming Contest
 
是个典型的二分匹配问题,若某个位置‘#’,则它可与上下左右位置上的‘#’连一条无向边,然后寻找最大匹配,最大匹配数即所求
 
#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int MAXN=600+10;int cnt,n;//u,v数目int g[MAXN][MAXN];int linker[MAXN];bool visited[MAXN];bool dfs(int u)//从左边开始找增广路径{    int v;    for(v=1;v<=cnt;v++)//这个顶点编号从0开始,若要从1开始需要修改      if(g[u][v]&&!visited[v])      {          visited[v]=true;          if(linker[v]==-1||dfs(linker[v]))          {//找增广路,反向              linker[v]=u;              return true;          }      }    return false;//这个不要忘了,经常忘记这句}int hungary(){    int res=0;    int u;    memset(linker,-1,sizeof(linker));    for(u=1;u<=cnt;u++)    {        memset(visited,0,sizeof(visited));        if(dfs(u)) res++;    }    return res;}char str[650][650];int mat[65][65];void Judge(int x,int y){if(y<n-1&&mat[x][y+1])g[mat[x][y]][mat[x][y+1]]=g[mat[x][y+1]][mat[x][y]]=1;if(x<n-1&&mat[x+1][y])     g[mat[x][y]][mat[x+1][y]]=g[mat[x+1][y]][mat[x][y]]=1;}int main(){int cas,i,j,tag=1;cin>>cas;while(cas--){cnt=0;scanf("%d",&n);for(i=0;i<n;i++){scanf("%s",str[i]);for(j=0;j<n;j++)if(str[i][j]=='#')mat[i][j]=++cnt;else mat[i][j]=0;}memset(g,0,sizeof(g));for(i=0;i<n;i++){for(j=0;j<n;j++){if(mat[i][j])Judge(i,j);}}printf("Case %d: %d\n",tag++,hungary()/2);}return 0;}

原创粉丝点击