Poj 1358 Housing Complexes【二分匹配+二维前缀和预处理建图】

来源:互联网 发布:同比环比数据分析 编辑:程序博客网 时间:2024/06/07 23:07

Housing Complexes
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 337 Accepted: 117

Description

The Ministry of housing is planning a huge construction project of several housing complexes. Each complex includes several apartments to be sold to government employees at reasonable prices. The ministry has located several big m*n pieces of land that can potentially be used for such construction project; one complex in each land. The lands are all rectangular, each with m*n number of 1*1 square blocks. All housing complexes are h*w rectangles covering exactly h*w blocks of each containing land. 

The problem is that there are originally some old buildings, each covering exactly one block of a land, making it impossible to locate enough free space for all the complexes in order to start the project. Therefore, the ministry has to buy some of these buildings, and demolish them to free the needed space. The old buildings belong to certain number of people. These people are angry of the possibility that their building may be bought and demolished, especially because the government usually pays much less for their buildings compared to the open market prices. 

In response to the protests, the ministry announces a "fair" decision that if it buys some buildings in one land, it will only choose those that belong only to one owner, and will buy all of them at reasonable price. And, it promises not to buy buildings belonging to the same owner in other lands. Note that with this constraint, there may be some lands in which building a complex is impossible. Trying to keep its promises, the ministry has asked you to write a program to see how many housing complexes can be constructed at most with these conditions. 

Input

The first line of the input file contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case contains five integers k (1 <= k <= 30), the number of lands, m and n (1 <= m, n <= 50), the number of rows and columns in each land respectively, and h and w (1 <= h, w <= 50), the number of rows and columns a complex occupies. After the first line, there are k*m lines in the input, representing k lands, each by an m*n matrix. Each line contains a string of length n with no leading or trailing spaces. Each character in the strings represents a block in the land and may be an upper case alphabetic character 'A'..'Z', indicating the owner of the block, or the character '0' indicating the block is free.

Output

There should be one line per test case containing the maximum number of housing complexes that can be constructed for that test case. 

Sample Input

2 3 4 3 3 2 A0B 000 0A0 00B AA0 00B 0B0 000 A0A 000 B00 B00 3 4 3 3 2 A0B 000 0A0 00B AA0 00B 0B0 000 A0A 000 0B0 B00 

Sample Output

32 

Source

Tehran 2002

题目大意:

给你K个岛屿,每个岛屿的大小是n*m的,一个岛屿最多只能建立一个h*w的房子,岛屿上0的位子是可以直接用于建筑房子的,A~Z表示一些人拥有的地盘,现在政府可以对于一个岛屿,买下一个人拥有的所有地盘(但是其他岛屿就不能再买这个人的地盘了),然后在建立房子。

问最多可以建立多少房子。


思路:

1、问题肯定在于建立房子的最多数量上来,每个岛屿可以有多种选择,对于每个岛屿的选择可能会影响其他岛屿的选择情况,那么我们想要最优数量,显然最大二分匹配问题。


2、那么O(k*26*n*m)枚举每个岛屿想要买的地盘的主人的编号,然后二维前缀和维护这个岛屿买下了这个主人所拥有的地盘之后能否有一个h*w的全0子矩阵。

建好图之后跑一遍最大二分匹配匈牙利算法即可。

注意,如果一个岛屿如果不用购买某人的地盘就能够建成,那么这个岛屿就不用参与二分匹配了,直接output++即可。


3、本题有一个trick点,我们只能建立h*w的子矩阵,而不能建立w*h的子矩阵。


Ac代码:

#include<stdio.h>#include<string.h>using namespace std;char a[65][65][65];int mp[65][65];int sum[65][65];int match[65];int vis[65];int k,n,m,h,w;int Slove(int kk,int ch){    memset(sum,0,sizeof(sum));    for(int i=1;i<=n;i++)    {        for(int j=1;j<=m;j++)        {            if(a[kk][i][j]-'A'+1==ch||a[kk][i][j]=='0')            {                sum[i][j]=1;            }            else sum[i][j]=0;        }    }    for(int i=1;i<=n;i++)    {        for(int j=1;j<=m;j++)        {            sum[i][j]+=sum[i][j-1];        }    }    for(int i=1;i<=m;i++)    {        for(int j=1;j<=n;j++)        {            sum[j][i]+=sum[j-1][i];        }    }    for(int i=1;i<=n;i++)    {        for(int j=1;j<=m;j++)        {            if(i>=h&&j>=w)            {                int x1=i-h;                int y1=j-w;                if(sum[i][j]-sum[i-h][j]-sum[i][j-w]+sum[x1][y1]>=h*w)return 1;            }        }    }    return 0;}int find(int u){    for(int j=1;j<=26;j++)    {        if(mp[u][j]==1&&vis[j]==0)        {            vis[j]=1;            if(match[j]==-1||find(match[j]))            {                match[j]=u;                return 1;            }        }    }    return 0;}int main(){    int t;    scanf("%d",&t);    while(t--)    {        memset(mp,0,sizeof(mp));        memset(match,-1,sizeof(match));        scanf("%d%d%d%d%d",&k,&n,&m,&h,&w);        for(int i=1;i<=k;i++)        {            for(int j=1;j<=n;j++)scanf("%s",a[i][j]+1);        }        for(int i=1;i<=k;i++)        {            for(int j=0;j<=26;j++)            {                mp[i][j]=Slove(i,j);            }        }        int output=0;        for(int i=1;i<=k;i++)        {            if(mp[i][0]==1)output++;            else            {                memset(vis,0,sizeof(vis));                if(find(i)==1)output++;            }        }        printf("%d\n",output);    }}/*11 4 3 3 2A0B0000A000B*/





0 0
原创粉丝点击