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
- Lightoj-1330 Binary Matrix(网络流+构造)
- LightOJ 1330 Binary Matrix 最大流
- LightOJ 1363 - Binary Matrix (II)
- lightoj 1363 - Binary Matrix (II) 贪心
- CodeForces 803A Maximal Binary Matrix-【思维+构造】
- UVA 12534 - Binary Matrix 2 (网络流‘最小费用最大流’ZKW)
- lightoj 1071 DP/网络流
- [网络流]matrix题解
- Matrix 网络流
- LightOJ 1247 Matrix Game
- hdu5015 233 Matrix 西安网络赛I题 构造矩阵
- LightOJ 1080 Binary Simulation
- LightOJ 1307:构造三角形
- 【网络流】hdu3376 Matrix Again
- 【网络流】 HDOJ 4307 Matrix
- 网络流 构造总结
- BNU-Binary Matrix- 贪心
- UVALive 5809 Binary Matrix
- 0718学习记录(指针与数组)
- pandas中的resample的参数
- iOS之 在iOS中调用HTML
- (OK)(OK) compile_mongoose_4_Fedora23.txt
- HttpClient
- Lightoj-1330 Binary Matrix(网络流+构造)
- OpenCV2.3.1+VS2005配置方法
- uva11889 benifit
- UINavigationController介绍:1-导航控制器简介
- U-boot移植
- Android常用--正则,广播和as快捷键
- hive空值判断
- Mac下iterm2简单配置
- hdoj 1201 (+基础练习一)18岁生日