关闭

hdu 1532/poj 1273 Drainage Ditches(最大流模板)

标签: ACMpoj
211人阅读 评论(0) 收藏 举报
分类:
Drainage Ditches
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 67890   Accepted: 26235

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

此题为最大流模板题

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
#define N 1111
#define INF 0x3f3f3f3f
struct Node
{
    int to;
    int cap;///容量
    int next;
    int rev;///反向边序
}edge[N];
int used[N],head[N],cnt;
void addedge(int from,int to,int cap)
{
    edge[cnt].to=to;edge[cnt].cap=cap;
    edge[cnt].next=head[from];edge[cnt].rev=cnt+1;
    head[from]=cnt++;
    edge[cnt].to=from;edge[cnt].cap=0;
    edge[cnt].next=head[to];edge[cnt].rev=cnt-1;
    head[to]=cnt++;
}
int dfs(int s,int t,int f)
{
    if(s==t) return f;
    used[s]=1;
    for(int i=head[s];i!=-1;i=edge[i].next)
    {
        Node &temp=edge[i];
        if(!used[temp.to]&&temp.cap>0)
        {
            int d=dfs(temp.to,t,min(f,temp.cap));
            if(d>0)
            {
                temp.cap-=d;
                edge[temp.rev].cap+=d;
                return d;
            }
        }
    }
    return 0;
}
int max_flow(int s,int t)
{
    int flow=0;
    while(1)
    {
        memset(used,0,sizeof(used));
        int d=dfs(s,t,INF);
        if(d==0) return flow;
        flow+=d;
    }
}
int main()
{
    int n,m;
    int from,to,cap;
    while(~scanf("%d %d",&m,&n))
    {
        cnt=0;
        memset(head,-1,sizeof(head));
        for(int i=0; i<m; i++)
        {
            scanf("%d %d %d",&from,&to,&cap);
            addedge(from,to,cap);
        }
        printf("%d\n",max_flow(1,n));
    }
    return 0;
}



Dinic算法模板:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
#define N 1111
#define INF 0x3f3f3f3f
struct Node
{
    int to,cap,next;
}edge[N];
int head[N];
int d[N];
int cnt;
void init()
{
    memset(head,-1,sizeof(head));
    cnt=0;
}
void addedge(int from,int to,int cap)
{
    edge[cnt].to=to;edge[cnt].cap=cap;
    edge[cnt].next=head[from];head[from]=cnt++;
    edge[cnt].to=from;edge[cnt].cap=0;
    edge[cnt].next=head[to];head[to]=cnt++;
}
int bfs(int s,int t)
{
    memset(d,-1,sizeof(d));
    queue<int> q;
    q.push(s);
    d[s]=0;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(d[v]==-1&&edge[i].cap>0)
            {
                d[v]=d[u]+1;

                q.push(v);
            }
        }
    }
    return d[t]!=-1;
}
int dfs(int s,int t,int f)
{
    if(s==t||f==0) return f;
    int flow=0;
    for(int i=head[s];i!=-1&&flow<f;i=edge[i].next)
    {
        int v=edge[i].to;
        if(d[v]==d[s]+1&&edge[i].cap>0)
        {
            int x=min(f-flow,edge[i].cap);
            x=dfs(v,t,x);
            flow+=x;
            edge[i].cap-=x;
            edge[i^1].cap+=x;
        }
    }
    if(!flow) d[s]=-2;
    return flow;
}
int Dinic(int s,int t)///起点s终点t
{
    int flow=0,f;
    while(bfs(s,t))
    {
        while(f=dfs(s,t,INF))
            flow+=f;
    }
    return flow;
}
int main()
{
   int n,m;
   int from,to,cap;
   while(~scanf("%d %d",&m,&n))
   {
       init();
       for(int i=0;i<m;i++)
       {
           scanf("%d %d %d",&from,&to,&cap);
           addedge(from,to,cap);
       }
       printf("%d\n",Dinic(1,n));
   }
   return 0;
}


0
0
查看评论

hdoj 1532 Drainage Ditches 题解(最大流)

Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7845&#...
  • y990041769
  • y990041769
  • 2014-03-27 11:27
  • 5930

【网络流之最大流】POJ1273-Drainage Ditche【模板题】

题目链接:http://poj.org/problem?id=1273 这是一道网络流的入门题,用来理解最大流很好。 这个题目我是看的bin神专门为我们这些歌渣渣写的最大流入门博客学的;可以去膜拜一下。链接:http://www.cnblogs.com/kuangbin/archive/2011/0...
  • wlxsq
  • wlxsq
  • 2015-08-29 11:21
  • 1486

网络流题单

最大流  POJ 1273 Drainage Ditches  POJ 1274 The Perfect Stall (二分图匹配)  POJ 1698 Alice's Chance  POJ 1459 Power Network  ...
  • yang_7_46
  • yang_7_46
  • 2013-06-16 15:21
  • 1065

最大流 Ford-Fulkerson算法模板

最大流Ford-Fulkerson算法模板
  • tengfei461807914
  • tengfei461807914
  • 2016-08-14 00:40
  • 378

hdu1532Drainage Ditches 最大流模板水题

感觉这道题纯粹就是试模板的,给一个有向图,求其最大流 Dinic算法:#include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std...
  • cq_pf
  • cq_pf
  • 2015-08-10 16:37
  • 553

poj 1273 & hdu 1532 Drainage Ditches(最大流 )EK,dinic模板

第一发网络流纪念下~~~ 因为这里e和v都差不多,所以EK和dinic跑出来的时间差不多。纠结的是如何把邻接矩阵换成邻接表的.. EK写法: //沿此路径添加反向边,同时修改路径上每条边的容量#include #include #include #include #include using n...
  • u014204835
  • u014204835
  • 2014-12-25 15:41
  • 453

HDU1532 Drainage Ditches 最大流Dinic模板

Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16186 Accepted Submission...
  • LuRiCheng
  • LuRiCheng
  • 2017-01-17 00:17
  • 272

hdu 1532 Drainage Ditches (网络流两种方法)

hdu 1532 Drainage Ditches (网络流两种方法)
  • u012860063
  • u012860063
  • 2014-03-04 17:54
  • 2001

POJ 1273 && HDU 1532:Drainage Ditches

网络流最大流入门
  • wyxeainn
  • wyxeainn
  • 2017-09-14 18:14
  • 125

POJ 1273 DRAINAGE DITCHES【最大流】

模板题。 DescriptionEvery time it rains on Farmer John’s fields, a pond forms over Bessie’s favorite clover patch. This means that the clover is covered ...
  • u014427196
  • u014427196
  • 2015-06-05 23:28
  • 293
    个人资料
    • 访问:141340次
    • 积分:5220
    • 等级:
    • 排名:第6245名
    • 原创:396篇
    • 转载:11篇
    • 译文:0篇
    • 评论:21条
    最新评论