POJ 3436 最大流
来源:互联网 发布:阳泉网络广播电视台 编辑:程序博客网 时间:2024/06/05 19:48
很羞愧啊 , 题都没看咋懂
翻译见:http://hi.baidu.com/lewutian/blog/item/1b0709220085b7fed7cae28b.html/cmtid/87a1a0ad1aebe6064b36d60e
题解参考了:http://zhyu.me/acm/poj-3436.html ,BUPT一位现役神牛的blog
每台机器分输入输出,很显然直接拆点,每个点的(in)和(out)间连一条容量为该机器产量的边,然后对于输入没有限制的点,从源点向该点的(in)连一条容量为inf的边,对于输出是P个零件都有的点,从该点的(out)向汇点连一条容量inf的边,然后枚举所有点,如果某点的输出满足另一点的输入,则连一条inf的边。建图后直接求最大流即可,要输出解集,只需要最后遍历所有点的(out) ,看其所连的所有边中不包括终点的边,且流量大于0的边即为所求。
/*ID: CUGB-wwjPROG:LANG: C++*/#include <iostream>#include <vector>#include <list>#include <map>#include <set>#include <deque>#include <queue>#include <stack>#include <bitset>#include <algorithm>#include <functional>#include <numeric>#include <utility>#include <sstream>#include <iomanip>#include <cstdio>#include <cmath>#include <cstdlib>#include <cctype>#include <string>#include <cstring>#include <cmath>#include <ctime>#define INF 1111111111#define MAXN 222#define MAXM 444444#define PI acos(-1.0)using namespace std;struct node{ int ver; // vertex int cap; // capacity int flow; // current flow in this arc int next, rev;}edge[MAXM];int dist[MAXN], numbs[MAXN], src, des, n;int head[MAXN], e, p;int ans[MAXM][3];struct abcd{ int val; int in[15],out[15];}x[MAXN];int srcout[15], desin[15];void add(int x, int y, int c){ //e记录边的总数 edge[e].ver = y; edge[e].cap = c; edge[e].flow = 0; edge[e].rev = e + 1; //反向边在edge中的下标位置 edge[e].next = head[x]; //记录以x为起点的上一条边在edge中的下标位置 head[x] = e++; //以x为起点的边的位置 //反向边 edge[e].ver = x; edge[e].cap = 0; //反向边的初始网络流为0 edge[e].flow = 0; edge[e].rev = e - 1; edge[e].next = head[y]; head[y] = e++;}void rev_BFS(){ int Q[MAXN], qhead = 0, qtail = 0; for(int i = 1; i <= n; ++i) { dist[i] = MAXN; numbs[i] = 0; } Q[qtail++] = des; dist[des] = 0; numbs[0] = 1; while(qhead != qtail) { int v = Q[qhead++]; for(int i = head[v]; i != -1; i = edge[i].next) { if(edge[edge[i].rev].cap == 0 || dist[edge[i].ver] < MAXN)continue; dist[edge[i].ver] = dist[v] + 1; ++numbs[dist[edge[i].ver]]; Q[qtail++] = edge[i].ver; } }}void init(){ e = 0; memset(head, -1, sizeof(head));}int maxflow(){ int u, totalflow = 0; int Curhead[MAXN], revpath[MAXN]; for(int i = 1; i <= n; ++i)Curhead[i] = head[i]; u = src; while(dist[src] < n) { if(u == des) // find an augmenting path { int augflow = INF; for(int i = src; i != des; i = edge[Curhead[i]].ver) augflow = min(augflow, edge[Curhead[i]].cap); for(int i = src; i != des; i = edge[Curhead[i]].ver) { edge[Curhead[i]].cap -= augflow; edge[edge[Curhead[i]].rev].cap += augflow; edge[Curhead[i]].flow += augflow; edge[edge[Curhead[i]].rev].flow -= augflow; } totalflow += augflow; u = src; } int i; for(i = Curhead[u]; i != -1; i = edge[i].next) if(edge[i].cap > 0 && dist[u] == dist[edge[i].ver] + 1)break; if(i != -1) // find an admissible arc, then Advance { Curhead[u] = i; revpath[edge[i].ver] = edge[i].rev; u = edge[i].ver; } else // no admissible arc, then relabel this vertex { if(0 == (--numbs[dist[u]]))break; // GAP cut, Important! Curhead[u] = head[u]; int mindist = n; for(int j = head[u]; j != -1; j = edge[j].next) if(edge[j].cap > 0)mindist = min(mindist, dist[edge[j].ver]); dist[u] = mindist + 1; ++numbs[dist[u]]; if(u != src) u = edge[revpath[u]].ver; // Backtrack } } return totalflow;}bool check(int* a, int* b){ for(int i = 0; i < p; i++) if(a[i] + b[i] == 1) return 0; return 1;}int main(){ init(); int nt; scanf("%d%d", &p, &nt); n = 2 * nt + 2; src = 1; des = n; for(int i = 0; i < p; i++) { srcout[i] = 0; desin[i] = 1; } for(int i = 1; i <= nt; i++) { scanf("%d", &x[i].val); for(int j = 0; j < p; j++) scanf("%d", &x[i].in[j]); for(int j = 0; j < p; j++) scanf("%d", &x[i].out[j]); } for(int i = 1; i <= nt; i++) { if(check(srcout, x[i].in)) add(src, i + 1, INF); if(check(x[i].out, desin)) add(i + 1 + nt, des, INF); add(i + 1, i + nt + 1, x[i].val); for(int j = 1; j <= nt; j++) if(i != j && check(x[i].out, x[j].in)) add(i + 1 + nt, j + 1, INF); } rev_BFS(); printf("%d ", maxflow()); int cnt = 0; for(int i = nt + 2; i <= des - 1; i ++) for(int j = head[i]; j != -1; j = edge[j].next) { if(edge[j].ver != des && edge[j].flow > 0) { ans[cnt][0] = i - nt - 1; ans[cnt][1] = edge[j].ver - 1; ans[cnt++][2] = edge[j].flow; } } printf("%d\n", cnt); for(int i = 0; i < cnt; i++) printf("%d %d %d\n", ans[i][0], ans[i][1], ans[i][2]); return 0;}
- POJ 3436 最大流
- 最大流 poj 3436
- poj 3436 (最大流)
- POJ-3436 拆点做最大流
- POJ 3436 拆点建图,第二道最大流
- poj 3436 ACM Computer Factory(最大流)
- poj 3436 ACM Computer Factory (最大流)
- poj 3436 ACM Computer Factory (最大流)
- POJ 3436 ACM Computer Factory 最大流
- POJ 3436 ACM Computer Factory 最大流
- poj 3436 最大流-拆点
- POJ 3436 ACM Computer Factory(最大流)
- poj 3436 ACM Computer Factory(最大流)
- POJ 3436:拆点最大流
- poj 1698(最大流)
- poj 1459(最大流)
- poj 1087(最大流)
- poj 1087 最大流
- 属于自己的抽屉(初步)
- ZOJ-3288 AC自动机
- 互联网网站架构升级----分布式环境的构建
- 生产和消费
- I Think I Need a Houseboat解题报告
- POJ 3436 最大流
- 中华人民共和国铸币图谱
- AS3 CheckBox ---- 复选框类
- 第八周任务3
- Linux跨平台C++代码解决方案(1)
- 万能const限定符与指针
- 宏展开出错
- 3D图形学矩阵完全解析傻瓜版
- 在PC机上BC45编译运行ucosii方法