详细讲解:http://blog.csdn.net/smartxxyx/article/details/9293665

下面贴上我的第一道最大流的题:

hdu3549

 1 #include<stdio.h>
2 #include<algorithm>
3 #include<stdlib.h>
4 #include<iostream>
5 #include<string.h>
6 #include<math.h>
7 #include<vector>
8 #include<map>
9 #include<queue>
10 struct pp
11 {
12 int x;
13 int cap;
14 int pre;
15 };
16 const int N=1e8;
17 bool used[30];
18 using namespace std;
19 vector<pp>GG[30];
20 void add(int from,int to,int vv)
21 { pp ss;
22 ss.cap=vv;ss.x=to;ss.pre=GG[to].size();
23 GG[from].push_back(ss);
24 ss.cap=0;ss.x=from;ss.pre=GG[from].size()-1;
25 GG[to].push_back(ss);
26 }
27 int dfs(int x,int y,int co)
28 {
29 if(x==y)
30 {
31 return co;
32 }
33 used[x]=true;
34 int i,j;
35 for(i=0;i<GG[x].size();i++)
36 { int cc ;
37 pp &LL=GG[x][i];
38 if(!used[LL.x]&&LL.cap>0)
39 {
40 cc=dfs(LL.x,y,min(co,LL.cap));
41 if(cc>0)
42 {
43 LL.cap-=cc;
44 GG[LL.x][LL.pre].cap+=cc;
45 return cc;
46 }
47 }
48 }
49 return 0;
50 }
51 int max_flow(int x,int t)
52 {
53 int flow=0;
54 while(1)
55 {memset(used,0,sizeof(used));
56 int kk=dfs(x,t,N);
57 if(kk==0)
58 {
59 return flow;
60 }
61 else flow+=kk;
62 }
63
64 }
65 int main(void)
66 {
67 int i,j,k,p,q;
68 scanf("%d",&k);
69
70 for(i=1;i<=k;i++)
71 { for(j=0;j<30;j++)
72 GG[j].clear();
73 scanf("%d %d",&p,&q);
74 int x;int y;int n,m;
75 for(j=0;j<q;j++)
76 {
77 scanf("%d %d %d",&x,&y,&n);
78 add(x,y,n);
79 }
80 int M=max_flow(1,p);
81 printf("Case %d: %d\n",i,M);
82 }
83 return 0;
84 }

DINIC算法

  1 #include<stdio.h>
2 #include<algorithm>
3 #include<stdlib.h>
4 #include<iostream>
5 #include<string.h>
6 #include<math.h>
7 #include<vector>
8 #include<map>
9 #include<queue>
10 using namespace std;
11 struct node
12 {
13 int to;
14 int cap;
15 int rev;
16 };
17 int level[20];
18 int iter[20];
19 vector<node>vec[20];
20 void add_edge(int from,int to,int cap )
21 {
22 vec[from].push_back(node {to,cap,vec[to].size()});
23 vec[to].push_back(node {from,0,vec[from].size()-1});
24 }
25 void BFS(int s);
26 int dfs(int id,int t,int f );
27 int max_flow(int s,int t);
28 const int N=1e9;
29 int main(void)
30 {
31 int i,j,k,p,q;
32 int x,y;
33 scanf("%d",&k);
34 int s;
35 for(s=1; s<=k; s++)
36 {
37 scanf("%d %d",&p,&q);
38 for(i=0;i<20;i++)
39 {
40 vec[i].clear();
41 }
42 for(i=0; i<q; i++)
43 {
44 int z;
45 scanf("%d %d %d",&x,&y,&z);
46 add_edge(x,y,z);
47 }
48 int sum=max_flow(1,p);
49 printf("Case %d: ",s);
50 printf("%d\n",sum);
51 }
52 return 0;
53 }
54 void BFS(int s)
55 {
56 memset(level,-1,sizeof(level));
57 queue<int>que;
58 level[s]=0;
59 que.push(s);
60 while(!que.empty())
61 {
62 int id=que.front();
63 que.pop();
64 for(int i=0; i<vec[id].size(); i++)
65 {
66 node x=vec[id][i];
67 if(level[x.to]<0&&x.cap>0)
68 {
69 level[x.to]=level[id]+1;
70 que.push(x.to);
71 }
72 }
73 }
74 }
75 int dfs(int id,int t,int f )
76 {
77 if(t==id)return f;
78 for(int &i=iter[id]; i<vec[id].size(); i++)
79 {
80 node &vv=vec[id][i];
81 if(level[vv.to]>level[id]&&vv.cap>0)
82 {
83 int d=dfs(vv.to,t,min(f,vv.cap));
84 if(d>0)
85 {
86 vv.cap-=d;
87 vec[vv.to][vv.rev].cap+=d;
88 return d;
89 }
90 }
91 }
92 return 0;
93 }
94 int max_flow(int s,int t)
95 {
96 int flow=0;
97 for(;;)
98 {
99 BFS(s);
100 if(level[t]<0)
101 {
102 return flow;
103 }
104 int f=0;
105 memset(iter,0,sizeof(iter));
106 while( (f=dfs(s,t,N))>0)
107 {
108 if(f==0)break;
109 flow+=f;
110 }
111 }
112 }

最新文章

  1. PAT树_层序遍历叶节点、中序建树后序输出、AVL树的根、二叉树路径存在性判定、奇妙的完全二叉搜索树、最小堆路径、文件路由
  2. php strcmp引起的问题
  3. 加载 pcntl 多进程
  4. 大晚上装CocoaPods出现错误坑爹
  5. Quartz2D 图像处理
  6. poj1828
  7. Jquery实现 TextArea 文本框根据输入内容自动适应高度
  8. 使用Python提取中文字符
  9. 2015年百度实习生前端笔试题上海卷a
  10. js 金额处理加小数点后两位
  11. Linux记录-JMX监控Tomcat上传到falcon
  12. WebSocket异步通讯,实时返回数据相关问题论坛
  13. C++设计模式——单例模式(转)
  14. shell脚本${}、##和%%使用范例
  15. Python自动化开发 - 进程、线程(一)
  16. 使用反射代替不断添加的if-else来实现代码的可扩展性
  17. MachineLearning ---- lesson 1
  18. LA-4255 Guess (拓扑排序+构造)
  19. 【HTML5】HTML5 WebSocket简介以及简单示例
  20. 剑指Offer面试题:8.二进制中1的个数

热门文章

  1. Demo02一千以内的水仙花数
  2. Leetcode中的SQL题目练习(一)
  3. Scala(一)【安装和IDEA中开发】
  4. 一起手写吧!call、apply、bind!
  5. eclipse上安装 windowBuilder方法
  6. Linux基础命令---mailq显示邮件队列
  7. Linux:awk与cut命令的区别
  8. jquery对radio和checkbox的操作
  9. entfrm-app赋能entfrm零代码开发平台 开启多平台分发
  10. ANTLR 相关术语