Drainage Ditches(最大流基础_增广路算法)



Drainage DitchesCrawling in process...
Crawling failed
Time Limit:1000MS    
Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit
Status

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 

题意:有n条路,m个点,让你求出以1为源点,m为汇点的最大流;

思路:典型的最大流增光路的算法。当且仅当残量网络中不存在s-t有向道路(增广路)时,此时的流是从s到t的最大流;

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <queue>
using namespace std;
#define inf  9999999999
int flow[210][210];
int maxflow[210],father[210],vis[210];
int max_flow;
int m,i;
void EK(int s,int e)
{
    queue<int >q;
    int u,v;
    while(1)
    {
        memset(maxflow,0,sizeof(maxflow));//每次寻找增广路径都将每个点的流入容量置为0;
        memset(vis,0,sizeof(vis));
        maxflow[s]=inf;//源点的流入量置为正无穷;
        q.push(s);//将源点压入队列;
        while(!q.empty())//当队列不为空
        {
            u=q.front();
            q.pop();
            for(v=s;v<=e;v++)
            {
                if(!vis[v]&&flow[u][v]>0)
                {
                    vis[v]=1;
                    father[v]=u;//记录下他的父亲方便反向更新
                    q.push(v);
                    maxflow[v]=min(maxflow[u],flow[u][v]);//当前点的容量为父亲点容量与边流量的较小者
                }
            }
            if(maxflow[e]>0)//如果找到了汇点并且汇点容量不为0则清空队列
            {
                while(!q.empty())
                    q.pop();
                break;
            }
        }
        if(maxflow[e]==0)//已经找不到到汇点的增光路经了,就退出整个循环
            break;
        for(i=e;i!=s;i=father[i])
        {
            flow[father[i]][i]-=maxflow[e];//正向更新
            flow[i][father[i]]+=maxflow[e];//反向更新
        }
        max_flow+=maxflow[e];//更新最大流
    }
}
int main()
{
    int n;
    int si,ei,ci;
    while(~scanf("%d %d",&n,&m))
    {
        max_flow=0;//最大流初始化;
        memset(flow,0,sizeof(flow));
        for(i=0;i<n;i++)
        {
            scanf("%d %d %d",&si,&ei,&ci);
            flow[si][ei]+=ci;
        }
        EK(1,m);
        printf("%d\n",max_flow);
    }
}

Drainage Ditches(最大流基础_增广路算法)

时间: 2024-08-10 19:09:24

Drainage Ditches(最大流基础_增广路算法)的相关文章

Power Network(最大流基础_增广路算法:多源多汇,自建总源点和总汇点)

 Power NetworkCrawling in process... Crawling failed Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description A power network consists of nodes (power stations, consumers and dispatchers) connected by p

最大流——增广路算法

关于网络流的增广路算法,网上有很多详细介绍,这里是摘录的模板.详细请见:http://www.cnblogs.com/kuangbin/archive/2011/07/26/2117636.html 1 #include<iostream> 2 #include<iomanip> 3 #include<ctime> 4 #include<climits> 5 #include<algorithm> 6 #include<queue>

关于最短增广路算法和连续最短增广路算法的操作步骤

最短增广路算法(SAP): 1.初始化容量网络和网络流: 2.构造残留网络和层次网络,如果汇点不在层次网络中,则算法结束: 3.在层次网络中不断用BFS增广,直到层次网络中没有增广路为止:每次增广完毕,在层次网络中要去掉因改进流量而导致饱和的弧: 4.转到步骤(2). 连续最短增广路算法(Dinic): 1.初始化容量网络和网络流: 2.构造残留网络和层次网络,如果汇点不在层次网络中,则算法结束: 3.在层次网络中用一次DFS过程进行增广,DFS执行完毕,该阶段的增广也执行完毕: 4.转到步骤(

增广路算法

#include<queue> #include<cstdio> #include<iostream> #include<cstring> using namespace std; const int maxn = 20; const int INF = (1<<30); int cap[maxn][maxn],flow[maxn][maxn];//cap记录容量,flow记录流量 int m; //弧的数量 int EdmondsKarp(in

poj-1273 Drainage Ditches(最大流基础题)

题目链接: Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 67475   Accepted: 26075 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 cover

最大网络流——增广路算法

几句废话:读了刘汝佳的书之后,感觉一切都是那么茫然,于是自己在网上找教程,自己一点点码的,大概用了三天.网络流基础:看来我很有必要说一下网络流的基础网络流问题就是给你一个图,每个图的边权叫做这条边的流量,问你从起始点出发,有多少值能通过这些边流到重点我知道你没看懂,举个例子: 如图: 最大值为 从1到2到4运6个 从1到2到3到4运1个 从1到3到4运3个 一共运10个. 举例说完了,那么我说几个定义: 容量,就只一条边的权值,表示能从这条边运送的最大值 流量,表示一条边实际上流过的最大值 那么

网络流 之 一般增广路算法 标号法实现

数据输入格式:首先输入顶点个数n和弧数m,然后输入每条弧的数据.规定源点为顶点0,汇点为顶点n-1.每条弧的数据格式为:u,v,c,f,分别表示这条弧的起点,终点,容量和流量.顶点序号从0开始. 代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 #include <str

网络流之 最短增广路算法模板(SAP)

数据输入格式:首先输入顶点个数n和弧数m,然后输入每条弧的数据.规定源点为顶点0,汇点为顶点n-1.每条弧的数据格式为:u,v,w,分别表示这条弧的起点,终点,容量.顶点序号从0开始. 代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 #include <string&g

网络流——增广路算法(dinic)模板

1 #include<iostream> 2 #include<cstring> 3 #include<algorithm> 4 #include<cmath> 5 #include<cstdio> 6 #include<queue> 7 using namespace std; 8 struct data 9 { 10 int from,to,next,cup,flow; 11 data(){from=-1,to=-1,next=-