1. 程式人生 > >zoj3548(最小點覆蓋)

zoj3548(最小點覆蓋)

題意:給定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. a
b.

 

 

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