Description

Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch. 
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network. 
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle. 

Input

The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.

Output

For each case, output a single integer, the maximum rate at which water may emptied from the pond.

Sample Input

5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10

Sample Output

50

网络流Dinic模板
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <string>
#include <cstring>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = ;
int c[maxn][maxn];
int dep[maxn];
int cur[maxn];
int n,m;
void pt()
{
for (int i=;i<=n;++i){
for (int j=;j<=n;++j)
printf("%d ",c[i][j]);
printf("\n");
}
printf("==================\n");
}
int bfs (int s,int t)
{
memset(dep,-,sizeof dep);
queue<int> q;
while (!q.empty()) q.pop();
dep[s] = ;
q.push(s);
while (!q.empty()){
int u = q.front();
q.pop();
for (int v=;v<=n;++v){
if (c[u][v]>&&dep[v]==-){//能到达该节点的条件是这条边有流量,而且这个点没有被访问
dep[v] = dep[u]+;
q.push(v);
}
}
}
return dep[t]!=-;
}
int dfs (int u,int mi,int t)
{
if (u==t)
return mi;
int tmp;
for (int &v=cur[u];v<=n;++v){//
if (c[u][v]>&&dep[v]==dep[u]+&&(tmp=dfs(v,min(mi,c[u][v]),t))){//下一节点的深度是当前节点+1
c[u][v]-=tmp;
c[v][u]+=tmp;
return tmp;
}
}
return ;//别忘写返回0!!!
}
int dinic ()
{
int ans = ;
int tmp;
while (bfs(,n)){//每次按照深度建立分层图,这样每次dfs的时候下一节点的深度是当前节点+1
while (){
for (int i=;i<maxn;++i) cur[i]=;//当前弧优化
tmp = dfs(,inf,n);
//printf("%d\n",tmp);
if (tmp==)
break;
//pt();
ans+=tmp;
}
}
return ans;
}
int main()
{
//freopen("de.txt","r",stdin);
while (~scanf("%d%d",&m,&n)){
memset(c,,sizeof c);
for (int i=;i<m;++i){
int u,v,cap;
scanf("%d%d%d",&u,&v,&cap);
c[u][v]+=cap;
}
printf("%d\n",dinic());
}
return ;
}

 

#include <iostream>#include <cstdio>#include <cmath>#include <algorithm>#include <queue>#include <string>#include <cstring>using namespace std;const int inf = 0x3f3f3f3f;const int maxn = 220;int c[maxn][maxn];int dep[maxn];int cur[maxn];int n,m;void pt(){    for (int i=1;i<=n;++i){            for (int j=1;j<=n;++j)                printf("%d ",c[i][j]);            printf("\n");        }    printf("==================\n");}int bfs (int s,int t){    memset(dep,-1,sizeof dep);    queue<int> q;    while (!q.empty()) q.pop();    dep[s] = 0;    q.push(s);    while (!q.empty()){        int u = q.front();        q.pop();        for (int v=1;v<=n;++v){            if (c[u][v]>0&&dep[v]==-1){//能到达该节点的条件是这条边有流量,而且这个点没有被访问                dep[v] = dep[u]+1;                q.push(v);            }        }    }    return dep[t]!=-1;}int dfs (int u,int mi,int t){    if (u==t)        return mi;    int tmp;    for (int &v=cur[u];v<=n;++v){//        if (c[u][v]>0&&dep[v]==dep[u]+1&&(tmp=dfs(v,min(mi,c[u][v]),t))){//下一节点的深度是当前节点+1            c[u][v]-=tmp;            c[v][u]+=tmp;            return tmp;        }    }    return 0;//别忘写返回0!!!}int dinic (){    int ans = 0;    int tmp;    while (bfs(1,n)){//每次按照深度建立分层图,这样每次dfs的时候下一节点的深度是当前节点+1        while (1){            for (int i=0;i<maxn;++i) cur[i]=1;//当前弧优化            tmp = dfs(1,inf,n);            //printf("%d\n",tmp);            if (tmp==0)                break;            //pt();            ans+=tmp;        }    }    return ans;}int main(){    //freopen("de.txt","r",stdin);    while (~scanf("%d%d",&m,&n)){        memset(c,0,sizeof c);        for (int i=0;i<m;++i){            int u,v,cap;            scanf("%d%d%d",&u,&v,&cap);            c[u][v]+=cap;        }        printf("%d\n",dinic());    }    return 0;}

POJ 1273 Drainage Ditches (网络流Dinic模板)的更多相关文章

  1. POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]

    题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...

  2. poj 1273 Drainage Ditches 网络流最大流基础

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 59176   Accepted: 2272 ...

  3. POJ 1273 Drainage Ditches 网络流 FF

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 74480   Accepted: 2895 ...

  4. poj 1273 Drainage Ditches (网络流 最大流)

    网络流模板题. ============================================================================================ ...

  5. POJ 1273 Drainage Ditches | 最大流模板

    #include<cstdio> #include<algorithm> #include<cstring> #include<queue> #defi ...

  6. poj 1273 Drainage Ditches(最大流)

    http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Subm ...

  7. POJ 1273 Drainage Ditches

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 67387   Accepted: 2603 ...

  8. POJ 1273 Drainage Ditches (网络最大流)

    http://poj.org/problem? id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

  9. POJ 1273 Drainage Ditches(网络流dinic算法模板)

    POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdi ...

随机推荐

  1. Java Web学习总结(6)Cookie/Session

    一.会话的概念 会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. 二.会话过程中要解决的一些问题 每个用户在使用浏览器与服务器进行 ...

  2. qbxt Day2 on 19-7-25

    qbxt Day2 on 19-7-25 --TGZCBY 上午 1. 矩阵乘法在图论上的应用 有的时候图论的转移方程可以用dp的方式转移 特别是两个数的乘积求和的时候 比如邻接矩阵中f[i][j]表 ...

  3. iphone-命令行编译之--xcodebuild

    参考 : https://www.cnblogs.com/xiaodao/archive/2012/03/01/2375609.html

  4. 测开之路四十九:用Django实现扑克牌游戏

    用Django实现和之前flask一样的扑克牌游戏 项目结构 html <!DOCTYPE html><html lang="en"><head> ...

  5. 如何设置Windows操作系统打印机与xlpd连接

    Xlpd是Xmanager中负责远程打印的软件,除了打印远程文件,它还具备很多功能,本集将具体讲解Xlpd的主要功能. 主要功能如下: 1.  支持LPD协议(RFC1179) 在RFC1179中定义 ...

  6. python排序算法-冒泡和快速排序,解答阿里面试题

    ''常见的排序算法\ 插入排序/希尔排序/直接排序/堆排序 冒泡排序/快速排序/归序排序/基数排序 给定一个列表,将这个列表进行排序,要求:> 时间复杂度要小于O(n^2) 复杂度:1.时间复杂 ...

  7. Java + selenium 元素定位(5)之By Xpath

    这篇关于Xpath方法的文章和之前那篇CSS的方法一样,使用前,需要先掌握一些Xpath的相关知识.当然,网上也有各种工具可以帮助我们获取到元素的Xpath,但是这并不代表着我们就可以不用了解Xpat ...

  8. Cocos2d-x之数据的处理

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. FileUtils 在游戏中,用户要保存自己的偏好设置和玩家的信息,都需要涉及到游戏数据的处理.首先要想处理数据,则要找到文件,创建文件, ...

  9. Win10自动更新关闭

    1.在win7和win8的系统中我们可以很轻松的通过控制面板的中的Windows Update中找到关闭自动更新的选项, 但是在win10中我们是找不到的.如下图: 2.在win10中的设置里面我们也 ...

  10. 深入理解__proto__ 、constructor和prototype的关系

    深入理解__proto__ .constructor和prototype的关系 2013-11-12 09:56 1390人阅读 评论(3) 收藏 举报  分类: 前端之Javascript(59)  ...