[最大流]UVa11082
2015-09-05 14:06
197 查看
思想完全是紫书上的思想
模板并不能很好的理解
还需要花时间好好理解
模板并不能很好的理解
还需要花时间好好理解
#include<bits/stdc++.h> using namespace std; const int maxn = 50 + 5; const int INF = 0x7f7f7f7f; struct Edge{ int from,to,cap,flow; Edge(int u=0,int v=0 ,int c=0,int f=0): from(u),to(v),cap(c),flow(f){} }; struct EdmondsKarp{ int n,m; vector<Edge> edges; vector<int> G[maxn]; int a[maxn]; int p[maxn]; void Init(int n){ for(int i=0;i<n;i++) G[i].clear(); edges.clear(); } void AddEdge(int from,int to,int cap){ edges.push_back(Edge(from,to,cap,0)); edges.push_back(Edge(to,from,0,0)); m=edges.size(); G[from].push_back(m-2); G[to].push_back(m-1); } int MaxFlow(int s,int t){ int flow=0; for(;;){ memset(a,0,sizeof(a)); queue<int> Q; Q.push(s); a[s]=INF; while(!Q.empty()){ int x=Q.front(); Q.pop(); for(int i=0;i<G[x].size();i++){ Edge& e=edges[G[x][i]]; if(!a[e.to]&&e.cap>e.flow){ a[e.to]=min(a[x],e.cap-e.flow); p[e.to]=G[x][i]; Q.push(e.to); } } if(a[t]) break; } if(!a[t]) break; for(int u=t;u!=s;u=edges[ p[u] ].from){ edges[ p[u] ].flow+=a[t]; edges[ p[u]^1 ].flow -= a[t]; } flow += a[t]; } return flow; } }; EdmondsKarp g; int ind[maxn][maxn]; int main() { int T,R,C; scanf("%d",&T); for(int kase=1;kase<=T;kase++){ scanf("%d%d",&R,&C); g.Init(R+C+2); int cur,last=0; for(int i=1;i<=R;i++){ scanf("%d",&cur); g.AddEdge(0,i,cur-last-C); last=cur; } last=0; for(int i=1;i<=C;i++){ scanf("%d",&cur); g.AddEdge(R+i,R+C+1,cur-last-R); last=cur; } for(int i=1;i<=R;i++){ for(int j=1;j<=C;j++) { g.AddEdge(i,j+R,19); ind[i][j]=g.edges.size()-2; } } g.MaxFlow(0,R+C+1); printf("Matrix %d\n",kase); for(int i=1;i<=R;i++){ printf("%d",g.edges[ ind[i][1] ].flow+1); for(int j=2;j<=C;j++) printf(" %d",g.edges[ ind[i][j] ].flow+1); printf("\n"); } printf("\n"); } return 0; }
相关文章推荐
- 40 Combination Sum II
- 4.7.5 Efficient Construction of LALR Parsing Tables
- Java文件上传下载
- JBOSS学习-一些基本配置了解
- Standard Compilation & OSR
- 早起5个迹象说明体内湿寒
- 财付通安全性存疑:资金频被盗刷
- Codeforces 567C Geometric Progression (离散 + DP)
- sizeof求结构体所占空间大小
- Spring学习之注入方式
- Stanford机器学习---第十讲. 数据降维
- 粒子群算法1
- 【more effective c++读书笔记】【第5章】技术(4)——Smart Pointers(智能指针)
- spring初接触
- bzoj1699:排队
- 【more effective c++读书笔记】【第5章】技术(4)——Smart Pointers(智能指针)
- 字符,字节和编码
- Stanford机器学习---第九讲. 聚类
- php fgets函数读取多余的空格解决
- 第11章 线程