Lightoj-1330 Binary Matrix(网络流+构造)

来源:互联网 发布:数控折弯机怎样编程 编辑:程序博客网 时间:2024/06/16 12:38

题目链接
给定一个r*c的01矩阵,但是只给了每行和每列的和,问能不能构造一个01矩阵出来;如果能救出最小字典序的01矩阵。

首先判断所有行的和是否等于所有列的和,不等就直接impossible结束了。
然后建图就是套路了。建立一个超级原点vs=0,超级汇点vt=r+c+1;
然后是行号[1,r],列号[r+1,r+c]。w[vs][row] = row[i],w[col][vt] = col[j],w[i][j] = 1.
跑遍最大流。
接下来就是比较重要的了,按照字典序进行构造调整。
如果flow[j][i] = 1(ps:此时w[vs][i] = 0, w[i][j] = 0,w[j][vt] = 0)
假定这个1不在mat[i][j-r]这个位置,那么w[vs][i] = 1, w[j][t] = 1,再对原图进行增广,如果能增广成功,说明mat[i][j-1]可以为0,即是这个1的位置可以在这行中往后移动;否则就只能为1。

/*****************************************Author      :Crazy_AC(JamesQi)Time        :2016File Name   :*****************************************/// #pragma comment(linker, "/STACK:1024000000,1024000000")#include <iostream>#include <algorithm>#include <iomanip>#include <sstream>#include <string>#include <stack>#include <queue>#include <deque>#include <vector>#include <map>#include <set>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <climits>using namespace std;#define MEM(x,y) memset(x, y,sizeof x)#define pk push_back#define lson rt << 1#define rson rt << 1 | 1#define bug cout << "BUG HERE\n"typedef long long LL;typedef unsigned long long ULL;typedef pair<int,int> ii;typedef pair<ii,int> iii;const double eps = 1e-8;const double pi = 4 * atan(1);const int inf = 1 << 30;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;int nCase = 0;int dcmp(double x){//精度正负、0的判断    if (fabs(x) < eps) return 0;    return x < 0?-1:1;}template<class T>inline bool read(T &n){    T x = 0, tmp = 1;    char c = getchar();    while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();    if(c == EOF) return false;    if(c == '-') c = getchar(), tmp = -1;    while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();    n = x*tmp;    return true;}template <class T>inline void write(T n){    if(n < 0)    {        putchar('-');        n = -n;    }    int len = 0,data[20];    while(n)    {        data[len++] = n%10;        n /= 10;    }    if(!len) data[len++] = 0;    while(len--) putchar(data[len]+48);}const int maxn = 110;vector<int> G[maxn];int w[maxn][maxn];int mark[maxn];int pre[maxn];int r, c;void addedge(int u,int v,int c) {    G[u].push_back(v);G[v].push_back(u);    w[u][v] = c;w[v][u] = 0;}bool augment_path(int s,int t) {    queue<int> que;    que.push(s);    memset(pre, -1,sizeof pre);    memset(mark, 0, sizeof mark);    while(!que.empty()) {        int u = que.front();        que.pop();        int size = G[u].size();        for (int i = 0;i < size;++i) {            int v = G[u][i];            if (!mark[v] && w[u][v] > 0) {                mark[v] = 1;                pre[v] = u;                if (v == t) continue;                que.push(v);            }        }    }    return pre[t] != -1;}int updata(int s,int t) {    int flow = INF;    for (int u = t;u != s;) {        flow = min(flow, w[pre[u]][u]);        u = pre[u];    }    for (int u = t;u != s;) {        w[pre[u]][u] -= flow;        w[u][pre[u]] += flow;        u = pre[u];    }    return flow;}int maxflow(int s,int t) {    int flow = 0;    while(augment_path(s, t)) {        int size = G[t].size();        for (int i = 0;i < size;++i) {            int u = G[t][i];            if (!mark[u] || w[u][t] <= 0) continue;            pre[t] = u;            flow += updata(s, t);        }    }    return flow;}int main(int argc, const char * argv[]){        // freopen("in.txt","r",stdin);    // freopen("out.txt","w",stdout);    // clock_t _ = clock();    int kase;cin >> kase;    while(kase--) {        read(r), read(c);        int vs = 0, vt = r + c + 1;        for (int i = 0;i <= vt;++i)            G[i].clear();        memset(w, 0, sizeof w);        int sumr = 0, sumc = 0;        for (int i = 1;i <= r;++i) {            int x;cin >> x;            addedge(vs, i, x);            sumr += x;        }        for (int i = 1;i <= c;++i) {            int x;cin >> x;            addedge(i+r, vt, x);            sumc += x;        }        printf("Case %d:", ++nCase);        if (sumr != sumc) {            puts(" impossible");            continue;        }        for (int i = 1;i <= r;++i) {            for (int j = 1;j <= c;++j) {                addedge(i, j + r, 1);            }        }        int flow = maxflow(vs, vt);        if (flow != sumc) {            puts(" impossible");            continue;        }        puts("");        for (int i = 1;i <= r;++i) {            for (int j = 1;j <= c;++j) {                int o = w[j+r][i];                w[i][j+r] = w[j+r][i] = 0;                if (o) {                    w[vs][i] = w[j+r][vt] = 1;                    if (augment_path(vs, vt)) {                        updata(vs, vt);                        o = 0;                    }else {                        w[vs][i] = w[j+r][vt] = 0;                    }                }                printf("%d", o);            }            puts("");        }    }    // printf("\nTime cost: %.2fs\n", 1.0 * (clock() - _) / CLOCKS_PER_SEC);    return 0;}
0 0
原创粉丝点击