网络流 dinic poj 1273

本文介绍了一个典型的板子网络流问题,使用Dinic算法求解最大流。问题背景为农场主约翰通过一系列排水沟渠将积水从池塘引流到溪流中,每条沟渠有其排水速率限制。文章详细解释了如何构建网络图并应用Dinic算法来找到最大排水率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

板子 网络流题


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   (好像打成 dicnic了 。。=_=)

很简单的一道题    但是一开始的时候insert  ( x , y , z )  insert( y , x , 0 )  写成了  insert( x , y , z )    insert( y , z , 0 )   mdzz   给  RE  掉了

我好弱啊啊


附上 代码  

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#define maxn 10005
#define inf 0x3f3f3f3f
using namespace std;
int n,m,cnt=1,s=1,ans;
int head[maxn];
int dep[maxn];
struct edge{
     int to,next,w;
}e[maxn];


void insert(int x,int y,int w)
{
    e[++cnt].to=y;
    e[cnt].next=head[x];
    e[cnt].w=w;
    head[x]=cnt;
}


bool bfs()
{
   queue<int>q;
   memset(dep,-1,sizeof(dep));
   dep[s]=0;
   q.push(s);
   while(!q.empty())
   {
        int u=q.front();  q.pop();
        for(int i=head[u];i;i=e[i].next)
        {
             if(e[i].w&&dep[e[i].to]==-1)
               dep[e[i].to]=dep[u]+1,q.push(e[i].to);
        }
   }
   if(dep[n]==-1)return 0;
     
   return 1;
}


int dfs(int x,int f)
{
     if(x==n)
       return f;
     int w,used=0;
     for(int i=head[x];i;i=e[i].next)
     {
             int u=e[i].to;
             if(e[i].w&&dep[e[i].to]==dep[x]+1)
             {
                 w=dfs(e[i].to,min(e[i].w,f-used));
                 used+=w;
                 e[i].w-=w;
                 e[i^1].w+=w;
                 if(used==f)
                   return f;
             }
     }
     if(!used)
       dep[x]=-1;
     return used;
     
}


void dicnic()
{
     ans=0;
    while(bfs())
    {
        ans+=dfs(1,inf);
    }
}


int main()
{
     while(~scanf("%d%d",&m,&n))
     {
          memset(head,0,sizeof(head));
          memset(e,0,sizeof(e));
    
          cnt=1;
          int x,y,z;
          for(int i=1;i<=m;i++)
          {
              scanf("%d%d%d",&x,&y,&z);
              insert(x,y,z);
              insert(y,x,0);
          }    
        dicnic();
        printf("%d\n",ans);
     }




}

写的很丑。。毕竟我是弱弱嘛


by  LT  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值