精确覆盖

zoj3209

#include
#include
#include
#include
using namespace std;
typedef long long LL;

const int INF=6000;
const int max_size = 1000*501;
int ygx_ans;
const int maxint = 0x3f3f3f3f;
int L[max_size], R[max_size], U[max_size], D[max_size], CH[max_size], RH[max_size];
int S[1005], O[1005];//S记录的是一列的sum,用于H函数,O记录选择的行坐标。
int head, size;

void remove(const int &c) {
    //remove column c and all row i that A[i][c] == 1
    L[R[c]] = L[c];
    R[L[c]] = R[c];
    //remove column c;
    for (int i = D[c]; i != c; i = D[i]) {
    //remove i that A[i][c] == 1
        for (int j = R[i]; j != i; j = R[j]) {
            U[D[j]] = U[j];
            D[U[j]] = D[j];
            --S[CH[j]];
            //decrease the count of column C[j];
        }
    }
}
void resume(const int &c) {
    for (int i = U[c]; i != c; i = U[i]) {
        for (int j = L[i]; j != i; j = L[j]) {
            ++S[CH[j]];
            U[D[j]] = j;
            D[U[j]] = j;
        }
    }
    L[R[c]] = c;
    R[L[c]] = c;
}

void dfs(int k) {
    //cout << "new DFS" << endl;
    if(k>=ygx_ans) return;
    if (R[head] == head) {
        //One of the answers has been found.
    //    return true;
     ygx_ans=k;
     return;
    }
    int s(maxint), c;
    //out(head);
    for (int t = R[head]; t != head; t = R[t]) {
        //out(t); out(R[t]);
        //select the column c which has the fewest number of element.
        if (S[t] < s) {
            s = S[t];
            c = t;
        }
    }
    //cout << "remove " << c << endl;
    remove(c);
    for (int i = D[c]; i != c; i = D[i]) {
        for (int j = R[i]; j != i; j = R[j]) {
            remove(CH[j]);
        }
        //if (dfs(k+1)) {
        //    return true;
        dfs(k+1);
    //    }
        for (int j = L[i]; j != i; j = L[j]) {
            resume(CH[j]);
        }
    }
    resume(c);
    //return false;
}
int node(int up, int down, int left, int right) {
    U[size] = up, D[size] = down;
    L[size] = left, R[size] = right;
    D[up] = U[down] = R[left] = L[right] = size;
    return size++;
}
void init(int M) {
    size = 0;
    head = node(0, 0, 0, 0);
    for (int j = 1; j <= M; ++j) {
        node(j, j, L[head], head);
        CH[j] = j, S[j] = 0;
    }
}

int main(){
    int t,n,m,p,a,b,c,d;
    scanf("%d",&t);
    while(t--){
               ygx_ans=INF;
               scanf("%d%d%d",&n,&m,&p);
               init(n*m);
               for(int i=0; i                        scanf("%d%d%d%d",&a,&b,&c,&d);
                       int row=-1;
                       for(int ii=a; ii                                for(int jj=b; jj                                        CH[size]=ii*m+jj+1,++S[ii*m+jj+1];
                                       if(row==-1){
                                                   row=node(U[CH[ii*m+jj+1]],CH[ii*m+jj+1],size,size);
                                                   RH[row]=i;
                                                   }
                                       else{
                                            int k=node(U[CH[ii*m+jj+1]],CH[ii*m+jj+1],L[row],row);
                                            RH[k]=i;
                                            }
                                       }
                               }
                       }
                       for(int i=1; i<=m*n; ++i)
                       if(!S[i]) goto L;
                                      dfs(0);
                                      L:
                                      if(ygx_ans!=INF)
                                      printf("%d/n",ygx_ans);
                                      else
                                      printf("-1/n");
               }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值