题意:

  给出一个有向图,以及边上的容量上限,求最大流。(有重边,要将容量上限叠加)

思路:

  用最简单的EK+BFS解决。每次搜到一条到达终点的路径,就立刻退出,更新ans,然后再回头修改图中的当前flow状况(这就得靠记录路径了)。当当前图没有到达终点的路径图,流已经饱和,可以结束程序了。

 #include <bits/stdc++.h>
#define LL long long
#define pii pair<int,int>
#define INF 0x7f7f7f7f
using namespace std;
const int N=+; vector<int> vect[N];
int c[N][N]; //容量
int flow[N][N]; //流量 int a[N]; //临时流量
int path[N]; //得记录用的是哪条边,好更新flow和cap int BFS(int m)
{
deque<int> que;
que.push_back();
a[]=INF; //先置为无穷大 while(!que.empty())
{
int x=que.front();
que.pop_front();
for(int i=; i<vect[x].size(); i++)
{
int t=vect[x][i];
if(!a[t] && c[x][t]>flow[x][t] ) //未遍历过,且容>流
{
path[t]=x; //只需要记得到达t的是哪个点
a[t]=min(a[x], c[x][t]-flow[x][t]); //要么全部流给你,要么取能流过的上限
que.push_back(t);
}
}
if(a[m]) return a[m]; //只要有路径能够更新到终点m,立刻退出。
}
return ;
} int cal(int m)
{
int ans_flow=;
while(true) //求最大流
{
memset(a,,sizeof(a));
memset(path,,sizeof(path)); int tmp=BFS(m);
if(!tmp) return ans_flow; //找不到增广路了
ans_flow+=tmp; int ed=m;
while(ed!=) //根据路径调整一下流及上限
{
int from=path[ed];
flow[from][ed]+=tmp; //正向边加流量
flow[ed][from]-=tmp; //反向边减流量,相当于cap-flow一样大于0。
ed=from;
}
}
} int main()
{
freopen("input.txt", "r", stdin);
int n, m, st, ed, ca;
while(~scanf("%d%d",&n,&m))
{ for(int i=; i<=m; i++) vect[i].clear();
memset(c, , sizeof(c));
memset(flow, , sizeof(flow)); for(int i=; i<n; i++)
{
scanf("%d %d %d", &st, &ed, &ca);
vect[st].push_back(ed); //邻接表
vect[ed].push_back(st); //反向边,容量是0的。
c[st][ed]+=ca; //坑在这
}
cout<<cal(m)<<endl; }
return ;
}

AC代码

HDU 1532 Drainage Ditches 排水渠(最大流,入门)的更多相关文章

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

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

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

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

  3. hdu 1532 Drainage Ditches(最大流)

                                                                                            Drainage Dit ...

  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 (网络流)

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

  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. USACO93网络流入门Drainage Ditches 排水渠(DCOJ 5130)

    题目描述 (传送门:http://poj.org/problem?id=1273翻译 by sxy(AFO的蒟蒻)) 每次约翰的农场下雨,Bessie的水池里的四叶草就会被弄破.这就意味着,这些四叶草 ...

  9. hdu 1532 Drainage Ditches (最大流)

    最大流的第一道题,刚开始学这玩意儿,感觉好难啊!哎····· 希望慢慢地能够理解一点吧! #include<stdio.h> #include<string.h> #inclu ...

随机推荐

  1. win8安装python环境和pip & easy_install工具

    最近在学python,2.7.6的版本 首先安装python2.7 官网下载地址https://www.python.org/downloads/ 下载相应版本即可,应该是一个msi的文件,默认安装到 ...

  2. 获取局域网ip

    显然不可使用基于request请求的request.getRemoteAddr()这个是获取广域网内的服务器地址,比如我请求百度使用这个方法就可以获取到百度的服务器地址 那么InetAddress的I ...

  3. python学习笔记20(字符串格式化)

    Python中内置有对字符串进行格式化的操作% 模板 格式化字符串时,Python使用一个字符串作为模板.模板中有格式符,这些格式符为真实值预留位置,并说明真实数值应该呈现的格式.Python用一个t ...

  4. HTTP verb的安全性和幂等性

    Http协议规定了不同方法的安全特性和幂等特性,作为服务提供者的服务器必需为客户端提供这些特性. 安全性,仅指该方法的多次调用不会产生副作用,不涉及传统意义上的“安全”,这里的副作用是指资源状态.即, ...

  5. 学习javascript总结下来的性能优化的小知识(一)

    http://www.cnblogs.com/ctriphire/p/4115525.html http://www.cnblogs.com/xjchenhao/archive/2012/10/22/ ...

  6. Monad学习

    这是观看Cousera上的课程<Principles of Reactive Programming>中week1里的Monad一节所做的笔记. What is a Monad? What ...

  7. 关于Java中try-catch-finally-return语句的思考

    我们知道return语句用在某一个方法中,一是用于返回函数的执行结果,二是用于返回值为void类型的函数中,仅仅是一个return语句(return ;),此时用于结束方法的执行,也即此return后 ...

  8. poj 1095 Trees Made to Order 卡特兰数

    这题用到了卡特兰数,详情见:http://www.cnblogs.com/jackge/archive/2013/05/19/3086519.html 解体思路详见:http://blog.csdn. ...

  9. WPF跨程序集共享样式(跨程序集隔离样式和代码)

    前记:WPF中的样式使用一般分为两种Statci和Dynamic.两者的区别可以理解为,前者在运行的时候已经确定了样式的风格,而后者可以根据资源在运行时的修改而修改也可以使用那些在运行时才存在的资源. ...

  10. Fundamental Datastructure

    11988 - Broken Keyboard (a.k.a. Beiju Text) 可以用deque来模拟. #include <iostream> #include <stri ...