uva820 最大流模板题
来源:互联网 发布:社交网络免费观看 编辑:程序博客网 时间:2024/06/03 15:38
注意以下输出格式,还有就是有重边。
这是邻接表的做法:
#include<iostream>#include<cstring>#include<cstdlib>#include<cstdio>#include<cmath>#include<string>#include<map>#include<set>#include<algorithm>#include<vector>#include<stack>#include<queue>#include<sstream>#define LL long long#define OJ_PRINT 0#define READ_FILE 0using namespace std;const int NN_MAX = 110;const int MM_MAX = NN_MAX*NN_MAX;const int INF = 0x1fffffff;struct Edge{ int from,to,cap,flow; Edge (int a1,int a2,int a3,int a4):from (a1),to (a2),cap (a3),flow (a4){} Edge (){}}theEdge[MM_MAX*4];/**********************************************************/int n,m,s,t;vector<int>G[NN_MAX];int a[NN_MAX],p[NN_MAX];int flow=0;/**********************************************************/int min_2 (int x,int y) {return x<y?x:y;}int max_2 (int x,int y) {return x>y?x:y;}void EK ();/**********************************************************/int main(){ if (READ_FILE) freopen ("in.txt","r",stdin); int tt=1; while (scanf ("%d",&n) && n) {scanf ("%d %d %d",&s,&t,&m);for (int i=0;i<=n;i++) G[i].clear ();int a1,a2,a3;int lenEdge=0;for (int i=0;i<m;i++){ scanf ("%d%d%d",&a1,&a2,&a3); theEdge[lenEdge++]=Edge (a1,a2,a3,0); G[a1].push_back (lenEdge-1); theEdge[lenEdge++]=Edge (a2,a1,0,0); G[a2].push_back (lenEdge-1); theEdge[lenEdge++]=Edge (a2,a1,a3,0); G[a2].push_back (lenEdge-1); theEdge[lenEdge++]=Edge (a1,a2,0,0); G[a1].push_back (lenEdge-1);}flow=0;EK ();//if (tt>1) printf ("\n");printf ("Network %d\nThe bandwidth is %d.\n\n",tt++,flow); } return 0;}void EK (){ while (1) {memset (a,0,sizeof (a));queue<int>qee; qee.push (s);a[s]=INF;while (!qee.empty ()){ int x=qee.front (); qee.pop (); for (int i=0;i<G[x].size ();i++){Edge& e=theEdge[G[x][i]];if (!a[e.to] && e.cap>e.flow){ p[e.to]=G[x][i]; a[e.to]=min_2 (a[x],e.cap-e.flow); qee.push (e.to);} } //if (a[t]) break;}if (a[t]==0) break;flow+=a[t];for (int i=t;i!=s;i=theEdge[p[i]].from){ theEdge[p[i]].flow+=a[t]; theEdge[p[i]^1].flow-=a[t];} }}
这是邻接矩阵的做法:
#include<iostream>#include<cstring>#include<cstdlib>#include<cstdio>#include<cmath>#include<string>#include<map>#include<set>#include<algorithm>#include<vector>#include<stack>#include<queue>#include<sstream>#define LL long long#define OJ_PRINT 0#define READ_FILE 0using namespace std;const int NN_MAX = 110;const int INF = 0x1fffffff;/**********************************************************/int n,m,s,t;int a[NN_MAX],p[NN_MAX],cap[NN_MAX][NN_MAX],flow[NN_MAX][NN_MAX];int maxflow=0;/**********************************************************/int min_2 (int x,int y) {return x<y?x:y;}int max_2 (int x,int y) {return x>y?x:y;}void EK ();/**********************************************************/int main(){ if (READ_FILE) freopen ("in.txt","r",stdin); int tt=1; while (scanf ("%d",&n) && n) {scanf ("%d %d %d",&s,&t,&m);int a1,a2,a3;int lenEdge=0;memset (cap,0,sizeof (cap));memset (flow,0,sizeof (flow));for (int i=0;i<m;i++){ scanf ("%d%d%d",&a1,&a2,&a3); cap[a1][a2]+=a3; cap[a2][a1]=cap[a1][a2];}maxflow=0;EK ();printf ("Network %d\nThe bandwidth is %d.\n\n",tt++,maxflow); } return 0;}void EK (){ while (1) {queue<int>qee;memset (a,0,sizeof (a)); a[s]=INF;qee.push (s);while (!qee.empty ()){ int x=qee.front (); qee.pop (); for (int y=1;y<=n;y++){if (!a[y] && cap[x][y]>flow[x][y]){ a[y]=min_2 (a[x],cap[x][y]-flow[x][y]); p[y]=x; qee.push (y);} }}if (a[t]==0) break;maxflow+=a[t];for (int i=t;i!=s;i=p[i]){ flow[p[i]][i]+=a[t]; flow[i][p[i]]-=a[t];} }}
0 0
- uva820 最大流模板题
- uva820(最大流)
- [最大流]UVa820 - Internet Bandwidth
- 【最大流问题 dinic算法】Internet Bandwidth, ACM/ICPC World Finals 2000, UVa820
- Uva820 Internet Bandwidth(网络流、双向EdmondsKarp)
- poj1459SAP最大流模板题
- hdu3549 最大流模板题
- 【HDU4280】【最大流模板题】
- POJ1273 最大流模板题_Edmonds_Karp
- POJ 1273 最大流模板题
- HDU1532 最大流-模板题
- 最小费用最大流模板题
- 【poj1459】Power Network【模板题】【最大流】
- 【最大流之sap】【HDU1532】模板题
- hdu1532Drainage Ditches 最大流模板水题
- hdu3549Flow Problem 最大流模板水题
- hdu3549(又是最大流模板题)
- HDU 1532(最大流模板题)
- POJ 1986 Distance Queries
- Mac Pro 丢失管理员_管理员变成普通用户
- Java IO最详解
- UVA 10361 Automatic Poetry
- 南邮 OJ 1009 2的N次方
- uva820 最大流模板题
- 算法速成(2)五大经典查找
- C# winform键盘钩子
- H.264量化参数QP和量化步长Qstep
- Linux 软件包管理
- HDU 5339 Untitled(DFS)
- Sandbox 沙盒
- mysql 多条重复行,查询出一条,并说明列为最大的
- hdu 1452 happy 2004