题意:
输入m n, m是边数,n是点数。
接下来m行: 起点,终点,容量。
求以 1 为源点, n为汇点的最大流。
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std; const int INF = 0xfffffff;
const int MAXN = + ; //邻接矩阵存放图。
int flow[MAXN][MAXN];
//mark[]标记是否访问过,pre[]记录增广路。
int mark[MAXN], pre[MAXN];
int m, n, f; //f为最大流。 void max_flow()
{
//不断寻找增广路,知道找不到为止,找不到的标志为 mark[n]==0.
while() {
memset(mark, , sizeof(mark));
memset(pre, , sizeof(pre)); queue<int> Q;
mark[] = ;
Q.push();
while( !Q.empty() ) {
int cnt = Q.front();
Q.pop();
//找到增广路,跳出。
if(cnt == n) {
break;
} for(int i = ; i <= n; i++) {
if(!mark[i] && flow[cnt][i] > ) {
mark[i] = ;
Q.push(i);
pre[i] = cnt;
}
}
} //如果没找到可增广的路,直接跳出.
if( !mark[n] ) {
break;
} //计算该增广路最大可增加的流量.
int minx = INF;
for(int i = n; i != ; i = pre[i]) {
minx = min( flow[pre[i]][i], minx );
} for(int i = n; i != ; i = pre[i]) {
flow[pre[i]][i] -= minx; //更新正向流量。
flow[i][pre[i]] += minx; //更新反向流量。
}
f += minx;
}
} int main()
{
while(scanf("%d %d", &m, &n) != EOF) {
memset(flow, , sizeof(flow));
for(int i = ; i < m; i++) {
int u, v, len;
scanf("%d %d %d", &u, &v, &len);
//这个题有重边的情况,所以要flow[u][v] += len;直接等于过不去。
flow[u][v] += len;
}
f = ;
max_flow();
printf("%d\n", f);
}
return ;
}

上面是EK、
下面是flod 还有一个区别就是 上面用邻接矩阵,而下面用邻接表。
    #include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
const int N = ;
const int INF = 0x3f3f3f3f; struct Node
{
int to;//终点
int cap; //容量
int rev; //反向边
}; vector<Node> v[N];
bool used[N]; void add_Node(int from,int to,int cap) //重边情况不影响
{
v[from].push_back((Node){to,cap,v[to].size()});
v[to].push_back((Node){from,,v[from].size()-});
} int dfs(int s,int t,int f)
{
if(s==t)
return f;
used[s]=true;
for(int i=;i<v[s].size();i++)
{
Node &tmp = v[s][i]; //注意
if(used[tmp.to]==false && tmp.cap>)
{
int d=dfs(tmp.to,t,min(f,tmp.cap));
if(d>)
{
tmp.cap-=d;
v[tmp.to][tmp.rev].cap+=d;
return d;
}
}
}
return ;
} int max_flow(int s,int t)
{
int flow=;
for(;;){
memset(used,false,sizeof(used));
int f=dfs(s,t,INF);
if(f==)
return flow;
flow+=f;
}
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
memset(v,,sizeof(v));
for(int i=;i<n;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add_Node(x,y,z);
}
printf("%d\n",max_flow(,m));
}
}

另附这题大神BLOG:http://blog.csdn.net/y990041769/article/details/22276101

HDU 1532 Drainage Ditches EK算法 flod算法的更多相关文章

  1. poj 1273 && hdu 1532 Drainage Ditches (网络最大流)

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 53640   Accepted: 2044 ...

  2. POJ 1273 || HDU 1532 Drainage Ditches (最大流模型)

    Drainage DitchesHal Burch Time Limit 1000 ms Memory Limit 65536 kb description Every time it rains o ...

  3. HDU 1532 Drainage Ditches (网络流)

    A - Drainage Ditches Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  4. hdu 1532 Drainage Ditches(最大流模板题)

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. hdu 1532 Drainage Ditches(最大流)

                                                                                            Drainage Dit ...

  6. HDU 1532 Drainage Ditches 分类: Brush Mode 2014-07-31 10:38 82人阅读 评论(0) 收藏

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  7. HDU 1532 Drainage Ditches (最大网络流)

    Drainage Ditches Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) To ...

  8. HDU 1532 Drainage Ditches(最大流 EK算法)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1532 思路: 网络流最大流的入门题,直接套模板即可~ 注意坑点是:有重边!!读数据的时候要用“+=”替 ...

  9. hdu 1532 Drainage Ditches(网络流)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532 题目大意是:农夫约翰要把多个小池塘的水通过池塘间连接的水渠排出去,从池塘1到池塘M最多可以排多少 ...

随机推荐

  1. HDU 2085 核反应堆 --- 简单递推

    HDU 2085 核反应堆 /* HDU 2085 核反应堆 --- 简单递推 */ #include <cstdio> ; long long a[N], b[N]; //a表示高能质点 ...

  2. 固定虚拟机的IP

  3. HDU-4455 Substrings(DP)

    题目大意:给一个长度为n的整数序列,定义egg(i,j)表示区间[i,j]中不同的数的个数.q次询问,每次询问x,表示求所有长度为x连续区间的 egg 之和. 题目分析:定义dp(len)表示所有长度 ...

  4. Text 绑定

    Text绑定 目的 Text绑定主要是让DOM元素显示参数值. 通常情况下,该绑定在<span>和<em>这样的元素上非常有用,而实际上你可以绑定任何元素. 示例 123456 ...

  5. 用 C# 在 Windows 7 中写注册表想到的

    摘自:http://blog.163.com/dpj_001/blog/static/2742941520110251500753/ 某日做一个项目,需要在注册表中加入键,同时写值,操作系统环境为 W ...

  6. Qt_Window@Qt Command Prompt从命令行创建工程

    #include <QApplication> #include <QLabel> int main(int argc, char *argv[]) { QApplicatio ...

  7. 10个你能参与并学习的Java开源项目

    本文转载于:http://code.csdn.net/news/2822604 有很多备受关注的初创开源项目,下面列出十个项目是我觉得非常有趣的,涉及到的学习范围也很广.其中有些还在早期阶段,这对我们 ...

  8. C#中有关string和byte[]转换的问题

    byte[] byteArray = System.Text.Encoding.Default.GetBytes( str ); 怎么样,够简单吧? 反过来也是一样,把byte[]转成string: ...

  9. VS 2012 C#快捷键

    ctrl + J 重现智能提示 ctrl + L    删除一行ctrl + K ctrl + C 注释选中行ctrl +K ctrl +U    取消注释 ctrl +K ctrl +F    格式 ...

  10. python--flask使用

    Flask是一个使用 Python 编写的轻量级 Web 应用框架.下面我将使用Flask框架,创建一个简单的html页面示例. 1.项目的目录结构如下所示:exweb\  uniqueenv\  a ...