zoj3548(最小点覆盖)

Vallis Brook和Helen Heather在一场特殊的棋盘绘制比赛中竞争。比赛的棋盘由n行m列的白色正方形组成,周围有黑色边框,且a(正方形边长)不小于b(边框宽度)。他们需要将一张黑白图片转换成这种棋盘样式,但Helen通过绘制矩形区域来改变颜色,每次操作需要T时间。任务是计算Helen完成棋盘绘制所需的最短时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:给定H*W的01矩阵,0为黑1为白。目标是要找出a和b使得能构造出如题图的n*m个边长为a的白色正方形,且每个正方形带长度为b的框。然后在原来的01矩阵的基础上,可以划定矩形给这个矩形染色,且这些划定的矩形不能相交,求用最少的矩形将原矩阵染成目标矩阵

显然对白色来说只要把n*m个正方形全部染一下就可以了。。而对黑色部分,如果是在正方形的边上(即没有2条横纵的黑色矩形相交得到)的部分,那一行(列)为必选,选完之后剩下的其他部分就可以选择横行或纵列去覆盖,然后这就是bzoj原题了。。直接横与列连边,跑最小点覆盖就可以了。。

 

 

/**
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神兽保佑,代码无bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<bitset>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1LL<<(x))
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define NM 205
#define nm 100005
#define pi 3.1415926535897931
using namespace std;
const ll inf=1e9+7;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}






struct edge{int t;edge*next;}e[nm],*h[NM],*o=e;
void add(int x,int y){o->t=y;o->next=h[x];h[x]=o++;}
int n,m,H,W,a[2005][2005],A,B,_t,ans,_x,v[NM],match[NM],s;
bool v1[NM],v2[NM];
char _s[2005];

bool dfs(int x){
    link(x)if(v[j->t]!=_x){
	v[j->t]=_x;
	if(!match[j->t]||dfs(match[j->t])){match[j->t]=x;return true;}
    }
    return false;
}


int main(){
    //freopen("data.in","r",stdin);
    while(~scanf("%d %d %d %d %d",&H,&W,&n,&m,&_t)){
	ans=inf;
	inc(i,1,H){
	    scanf("%s",_s+1);
	    inc(j,1,W)a[i][j]=_s[j]-'0'+a[i][j-1]+a[i-1][j]-a[i-1][j-1];
	}
	if(n!=m&&(H-W)%(n-m)){printf("-1\n");continue;}
	for(B=1,A=(H-(n+1)*B)/n;A>=B&&B<=min(W,H)/max(n+1,m+1);B++,A=(H-(n+1)*B)/n)if(n*A+(n+1)*B==H&&m*A+(m+1)*B==W){
	    inc(i,1,max(n+1,m+1))v[i]=match[i]=v1[i]=v2[i]=0;
	    inc(i,1,max(n+1,m+1))h[i]=0;
	    o=e;
	    s=0;
	    inc(i,1,n)inc(j,1,m)if(a[(A+B)*i][(A+B)*j]-a[(A+B)*i][(A+B)*j-A]-a[(A+B)*i-A][(A+B)*j]+a[(A+B)*i-A][(A+B)*j-A]<sqr(A))s++;
	    n++;m++;
	    inc(i,1,n)inc(j,1,m-1)if(a[(A+B)*i-A][(A+B)*j]-a[(A+B)*(i-1)][(A+B)*j]-a[(A+B)*i-A][(A+B)*j-A]+a[(A+B)*(i-1)][(A+B)*j-A])v1[i]++;
	    inc(i,1,n-1)inc(j,1,m)if(a[(A+B)*i][(A+B)*j-A]-a[(A+B)*i][(A+B)*(j-1)]-a[(A+B)*i-A][(A+B)*j-A]+a[(A+B)*i-A][(A+B)*(j-1)])v2[j]++;
	    inc(i,1,n)if(!v1[i])inc(j,1,m)if(!v2[j]&&
		    a[(A+B)*i-A][(A+B)*j-A]-a[(A+B)*(i-1)][(A+B)*j-A]-a[(A+B)*i-A][(A+B)*(j-1)]+a[(A+B)*(i-1)][(A+B)*(j-1)])add(i,j);
	    inc(i,1,n)if(v1[i])s++;
	    inc(i,1,m)if(v2[i])s++;
	    if(s>ans)continue;
	    inc(i,1,n)if(dfs(_x=i))s++;
	    n--;m--;ans=min(ans,s);
	}
	if(ans<inf)printf("%d\n",ans*_t);else printf("-1\n");
    }
    return 0;
}

 

 

 

Chess Board


Time Limit: 2 Seconds      Memory Limit: 65536 KB


Vallis Brook and Helen Heather like playing chess. Today they are not going to play chess buthave a competition on drawing a chess board. The chess board they used are a bit different from the ordinary one. The chess board consist of n rows and each row has m white square cells of the same size. Adjacent cells are seperated with a black border. There are also black borders on the edge of the chess board. Each border has the same width. The following figure shows how the chess board looks like. To make it like a chess board more, it is also constrained that the length of the cell should be no shorter than the width of the border, i.e. ab.

 

 

Now they are given a black-and-white image. They are going to make it a chess board as mentioned above. They both want to finish it faster than the other one.

In order to win the competition, Vallis managed to know how Helen would draw the chess board. Each time, Helen can draw a rectangle with one color and all the pixels in the rectangle will be painted with that color. He needs time T to draw one rectangle. In addition, he will only paint each pixel with the target color. That is to say, there will be no pixel to be painted multiple times with different colors. Because Vallis is busy drawing the chess board pixel by pixel, he ask you to help him calculate the minimum time for Helen to finish the job.

Input

There are multiple test cases. The first line of each test case contains five integers H, W, n, m and T (3 ≤ H, W ≤ 2000, 1 ≤ n, m ≤ 200, 0 < T ≤ 1000), indicating the height and the width of the image, the numbers of rows and columns and the time Helen needs to draw a rectangle. Then H lines follows, each of which contains W 0 or 1, indicating the image. 0 means the color of that pixel is black and 1 means white.

Output

Output the minimum time Helen needs to finish the chess board if it is possible to draw one. Otherwise, output -1 instead.

Sample Input

3 5 1 2 1
00000
01110
01110
5 7 1 2 1
0000000
0000000
0000000
0000000
0000000

Sample Output

2
-1

Author: GUAN, Yao
Contest: The 2011 ACM-ICPC Asia Dalian Regional Contest

Submit    Status

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值