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. Oracle体系结构理论

    问题: 1:通常说Oracle数据库是什么? 2:如何理解Oracle实例? 3:Oracle实例由哪些部分组成,它们之间的作用是什么? 4:如何理解Oracle的物理结构? 5:Oracle的物理结 ...

  2. 误删SQL Server日志文件后怎样附加数据库

    SQL Server日志文件因为误操作被删除,当附加数据库的时候提示:附加数据库失败. 解决办法如下: 1.新建一个同名数据库. 2.停止数据库服务,覆盖新建的数据库主文件(小技巧:最好放在同一个磁盘 ...

  3. Bugku | 游戏过关

    思路:绕过判断,直接跳转到算flag的函数哪里 1.找到计算flag的函数在哪里,记住 "0075e940",这是入口 2.找到一个现成的跳转指令,修改它: 3.重新运行一遍,得到 ...

  4. 经典JS 判断上传文件大小和JS即时同步电脑时间

    我也是新手,还是一个JS笨,有一些网站要实现的功能要自己写么? 答案是不会,去问同事大佬吧,闲简单.就在晚上看了一些其他大佬们写的JS效果, 代码很少.占用网站CPU也小的多.可以一用, 废话少扯.代 ...

  5. (三)修改内核大小,适配目标板Nand flash分区配置

    一. 修改内核大小 1. 在你的配置文件下uboot/include/config/xxx.h 里面有一个宏定义 #define MTDPARTS_DEFAULT "mtdparts=jz2 ...

  6. COALESCE 函数作用

    用途. 将空值替换成其他值 返回第一个非空值. 任意一个不为空的值.比较有用.

  7. 第一次刷Leetcode,为什么耗费很多时间

    Leetcode第2题思考过程分析:耗费的时间与思考过程 1. 审题耗费了很长时间,英文看不懂.两个单链表代表了两个整数,整数逆序,(2 -> 4 -> 3) + (5 -> 6 - ...

  8. 自定义实现字符串string的接口

    用char*管理String类的内存,new动态分配,在析构函数中delete char*指向的new出来的内存,一个string类需要实现那些接口可参考标准库里的string:  http://ww ...

  9. selenium和phantomjs,完成豆瓣音乐排行榜的内容爬取

    代码要多敲 注释要清晰 哪怕再简单 #使用selenium和phantomjs,完成豆瓣音乐排行榜的内容爬取 #地址:https://music.douban.com/chart #导入需要的模块 f ...

  10. fiddler如何抓取https接口

    1.Fiddler工作原理:    Fiddler 是以代理 web 服务器的形式工作的,它使用代理地址:127.0.0.1端口:8888. 当 Fiddler 退出的时候它会自动注销,这样就不会影响 ...