poj 1273 Drainage Ditches【最大流入门】

时间:2021-12-02 13:15:20
Drainage Ditches
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 63924   Accepted: 24673

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
题意:把池塘(编号1)里面的水通过若干个渠沟排到小溪(编号n)里面,每个渠沟都有最大容量,求能从池塘排出来的最大水量。 最大流模版
#include<stdio.h>
#include<string.h>
#include<stack>
#include<queue>
#include<algorithm>
#define MAX 1100
#define INF 0x7fffff
using namespace std;
struct node
{
int from,to,cap,flow,next;
}edge[MAX];
int n,m;
int ans,head[MAX];
int vis[MAX];//用bfs求路径时判断当前点是否进队列,
int dis[MAX];//当前点到源点的距离
int cur[MAX];//保存该节点正在参加计算的弧避免重复计算
void init()
{
ans=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v,int w)
{
edge[ans].from=u;
edge[ans].to=v;
edge[ans].cap=w;
edge[ans].flow=0;
edge[ans].next=head[u];
head[u]=ans++;
}
void getmap()
{
int i,a,b,c;
while(n--)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);//正向建边c为最大容量
add(b,a,0);//反向建边,
}
}
int bfs(int beg,int end)
{
int i;
memset(vis,0,sizeof(vis));
memset(dis,-1,sizeof(dis));
queue<int>q;
while(!q.empty())
q.pop();
vis[beg]=1;
dis[beg]=0;
q.push(beg);
while(!q.empty())
{
int u=q.front();
q.pop();
for(i=head[u];i!=-1;i=edge[i].next)//遍历所有的与u相连的边
{
node E=edge[i];
if(!vis[E.to]&&E.cap>E.flow)//如果边未被访问且流量未满继续操作
{
dis[E.to]=dis[u]+1;//建立层次图
vis[E.to]=1;//将当前点标记
if(E.to==end)//如果当前点搜索到终点则停止搜索 返回1表示有从原点到达汇点的路径
return 1;
q.push(E.to);//将当前点入队
}
}
}
return 0;//返回0表示未找到从源点到汇点的路径
}
int dfs(int x,int a,int end)//把找到的这条边上的所有当前流量加上a(a是这条路径中的最小残余流量)
{
//int i;
if(x==end||a==0)//如果搜索到终点或者最小的残余流量为0
return a;
int flow=0,f;
for(int& i=cur[x];i!=-1;i=edge[i].next)//i从上次结束时的弧开始
{
node& E=edge[i];
if(dis[E.to]==dis[x]+1&&(f=dfs(E.to,min(a,E.cap-E.flow),end))>0)//如果
{//bfs中我们已经建立过层次图,现在如果 dis[E.to]==dis[x]+1表示是我们找到的路径
//如果dfs>0表明最小的残余流量还有,我们要一直找到最小残余流量为0
E.flow+=f;//正向边当前流量加上最小的残余流量
edge[i^1].flow-=f;//反向边
flow+=f;//总流量加上f
a-=f;//最小可增流量减去f
if(a==0)
break;
}
}
return flow;//所有边加上最小残余流量后的值
}
int Maxflow(int beg,int end)
{
int flow=0;
while(bfs(beg,end))//存在最短路径
{
memcpy(cur,head,sizeof(head));//复制数组
flow+=dfs(beg,INF,end);
}
return flow;//最大流量
}
int main()
{
int i,j;
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
getmap();
printf("%d\n",Maxflow(1,m));
}
return 0;
}

  

poj 1273 Drainage Ditches【最大流入门】的更多相关文章

  1. poj 1273 Drainage Ditches 最大流入门题

    题目链接:http://poj.org/problem?id=1273 Every time it rains on Farmer John's fields, a pond forms over B ...

  2. POJ 1273 - Drainage Ditches - &lbrack;最大流模板题&rsqb; - &lbrack;EK算法模板&rsqb;&lbrack;Dinic算法模板 - 邻接表型&rsqb;

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

  3. Poj 1273 Drainage Ditches&lpar;最大流 Edmonds-Karp &rpar;

    题目链接:poj1273 Drainage Ditches 呜呜,今天自学网络流,看了EK算法,学的晕晕的,留个简单模板题来作纪念... #include<cstdio> #include ...

  4. POJ 1273 Drainage Ditches 最大流

    这道题用dinic会超时 用E_K就没问题 注意输入数据有重边.POJ1273 dinic的复杂度为O(N*N*M)E_K的复杂度为O(N*M*M)对于这道题,复杂度是相同的. 然而dinic主要依靠 ...

  5. POJ 1273 Drainage Ditches &vert; 最大流模板

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

  6. POJ 1273 Drainage Ditches&lpar;最大流Dinic 模板&rpar;

    #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n, ...

  7. poj 1273 Drainage Ditches(最大流)

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

  8. POJ 1273 Drainage Ditches &lpar;网络最大流&rpar;

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

  9. 网络流最经典的入门题 各种网络流算法都能AC。 poj 1273 Drainage Ditches

    Drainage Ditches 题目抽象:给你m条边u,v,c.   n个定点,源点1,汇点n.求最大流.  最好的入门题,各种算法都可以拿来练习 (1):  一般增广路算法  ford() #in ...

随机推荐

  1. ASP&period;NET MVC3 Razor 调试与预加载

    目录(?)[-] 获取服务器信息 FormsAuthenticationSlidingExpiration 属性 MVC3预加载   在ASP.NET MVC3开发中,调试中怎么也是不可缺少的,那对于 ...

  2. 在MySQL向表中插入中文时,出现:incorrect string value 错误

    在MySQL向表中插入中文时,出现:incorrect string value 错误,是由于字符集不支持中文.解决办法是将字符集改为GBK,或UTF-8.      一.修改数据库的默认字符集   ...

  3. 用JSON-server模拟REST API&lpar;二&rpar; 动态数据

    用JSON-server模拟REST API(二) 动态数据 上一篇演示了如何安装并运行 json server , 在这里将使用第三方库让模拟的数据更加丰满和实用. 目录: 使用动态数据 为什么选择 ...

  4. js星级评分点击星级评论打分效果

    html代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...

  5. dedecms修改templets为别的名字

    修改templets模板文件夹的方法: 首先找到系统配置文件common.inc.php,此文件存放在Include目录下,打开common.inc.php来修改默认模板目录templets, 查找: ...

  6. Android的Activity跳转动画各种效果整理

    Android的Activity跳转就是很生硬的切换界面.其实Android的Activity跳转可以设置各种动画,本文整理了一些,还有很多动画效果,就要靠我们发挥自己的想象力 大家使用Android ...

  7. javaEE开发中使用session同步和token机制来防止并发重复提交

    javaEE开发中使用session同步和token机制来防止并发重复提交 通常在普通的操作当中,我们不需要处理重复提交的,而且有很多方法来防止重复提交.比如在登陆过程中,通过使用redirect,可 ...

  8. Django Cookie 和 Sessions 应用

    在Django里面,使用Cookie和Session看起来好像是一样的,使用的方式都是request.COOKIES[XXX]和request.session[XXX],其中XXX是您想要取得的东西的 ...

  9. dedecms中&lbrace;dede&colon;myad name&equals;'about'&sol;&rcub; 修改内容

    网站首页index.htm中调用这个命令,显示一段文字,但是想要修改内容.所以想知道这个命令指定的文件内容在哪里寻找或者指定内容在哪里修改? 匿名 | 浏览 6036 次 发布于2014-02-19 ...

  10. OGG-02803 Encountered a Data Guard role transition

    告警提示其实已经很明显了OGG-02803 Encountered a Data Guard role transition. Alter Extract to SCN 15,756,246 and ...