题意:
输入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. hihoCoder #1078 : 线段树的区间修改

    题目大意及分析: 线段树成段更新裸题. 代码如下: # include<iostream> # include<cstdio> # include<cstring> ...

  2. 微信JS-SDK应用DEMO

    首先需要将以下函数写入TinkPHP的公用function.php文件中以便调用 // 基于ThinkPHP /** * php curl 请求链接 * 当$post_data为空时使用GET方式发送 ...

  3. JSBinding + SharpKit / Important Notes

    Serialization of List<T> is not supported. 1 public int v; // SUPPORTED 2 public GameObject go ...

  4. CoordinatorLayout-带图片伸缩工具栏

    伸缩工具栏toolbardesign 效果图: 步骤一: 在build.gilde中添加以下代码 dependencies { compile fileTree(dir: 'libs', includ ...

  5. LINQ(LINQ to DataSet)

    http://www.cnblogs.com/SkySoot/archive/2012/08/21/2649471.html DataTable.Select()方法使用和 SQL 相似的过滤语法从 ...

  6. DMZ区域

    DMZ是英文"demilitarized zone"的缩写,中文名称为"隔离区",也称"非军事化区".它是为了解决安装防火墙后外部网络不能访 ...

  7. OpenJudge计算概论-单词替换

    /*====================================================================== 单词替换 总时间限制: 1000ms 内存限制: 65 ...

  8. Linux-CentOS6.4-PXE-DHCP-FTP

    DHCP服务器地址:192.168.1.100TFTPfFF服务器地址:192.168.1.100FTP服务器地址:192.168.1.100安装树目录:/var/ftp/pub引导程序目录:/var ...

  9. MVC 将视图页table导出成excel

    前台代码: <table class="tablelist" id="myTable">    <thead>        <t ...

  10. Rest Client(Rest接口调试工具,有保存功配置功能) chrome浏览器插件

    Rest Client(Rest接口调试工具,有保存功配置功能) chrome浏览器插件 下载地址 插件的操作很简单,下面是一些简单的实例. 1.安装 在谷歌应用商城搜索postman,如下图1-1所 ...