POJ 1273 Drainage Ditches

本文介绍了一种解决最大流问题的方法——Dinic算法,并通过一个具体的编程实例详细展示了如何使用该算法来找到从起点到终点的最大流量。文章还提供了一份完整的C++实现代码。

摘要生成于 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

Source

USACO 93

题目大意

11m的网络流。

思路

Dinic

代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>

const int maxn=200;
const int maxm=400;
const int inf=0x3f3f3f3f;

int n,m;

struct queue
{
  int head,tail,q[(maxn<<2)+10];

  inline int empty()
  {
    return head==tail;
  }

  inline int push(int x)
  {
    ++tail;
    q[tail]=x;
    return 0;
  }

  inline int front()
  {
    return q[head+1];
  }

  inline int pop()
  {
    if(empty())
      {
    return -1;
      }
    ++head;
    return 0;
  }

  inline int mem()
  {
    head=tail=0;
    return 0;
  }
};

struct graph
{
  int pre[maxm+10],now[maxn+10],son[maxm+10],val[maxm+10],tot,d[maxn+10];
  queue q;

  inline int ins(int a,int b,int c)
  {
    pre[tot]=now[a];
    now[a]=tot;
    son[tot]=b;
    val[tot]=c;
    ++tot;
    pre[tot]=now[b];
    now[b]=tot;
    son[tot]=a;
    val[tot]=0;
    ++tot;
    return 0;
  }

  inline int bfs()
  {
    memset(d,0,sizeof d);
    q.mem();
    q.push(1);
    d[1]=1;
    while(!q.empty())
      {
    int u=q.front(),j=now[u];
    q.pop();
    while(j)
      {
        int v=son[j];
        if((!d[v])&&(val[j]))
          {
        d[v]=d[u]+1;
        if(v==n)
          {
            return 1;
          }
        q.push(v);
          }
        j=pre[j];
      }
      }
    return 0;
  }

  inline int dfs(int u,int ru)
  {
    if(u==n)
      {
    return ru;
      }
    int res=ru,j=now[u];
    while((j)&&res)
      {
    int v=son[j];
    if((val[j])&&(d[v]==d[u]+1))
      {
        int f=dfs(v,std::min(val[j],res));
        if(!f)
          {
        d[v]=0;
          }
        res-=f;
        val[j]-=f;
        val[j^1]+=f;
      }
    j=pre[j];
      }
    return ru-res;
  }

  inline int mem()
  {
    tot=1;
    memset(now,0,sizeof now);
    return 0;
  }
};

int a,b,c,ans;
graph g;

int main()
{
  while(scanf("%d%d",&m,&n)!=EOF)
    {
      ans=0;
      g.mem();
      while(m--)
    {
      scanf("%d%d%d",&a,&b,&c);
      g.ins(a,b,c);
    }
      while(g.bfs())
    {
      ans+=g.dfs(1,inf);
    }
      printf("%d\n",ans);
    }
  return 0;
}
资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在当今的软件开发领域,自动化构建与发布是提升开发效率和项目质量的关键环节。Jenkins Pipeline作为一种强大的自动化工具,能够有效助力Java项目的快速构建、测试及部署。本文将详细介绍如何利用Jenkins Pipeline实现Java项目的自动化构建与发布。 Jenkins Pipeline简介 Jenkins Pipeline是运行在Jenkins上的一套工作流框架,它将原本分散在单个或多个节点上独立运行的任务串联起来,实现复杂流程的编排与可视化。它是Jenkins 2.X的核心特性之一,推动了Jenkins从持续集成(CI)向持续交付(CD)及DevOps的转变。 创建Pipeline项目 要使用Jenkins Pipeline自动化构建发布Java项目,首先需要创建Pipeline项目。具体步骤如下: 登录Jenkins,点击“新建项”,选择“Pipeline”。 输入项目名称和描述,点击“确定”。 在Pipeline脚本中定义项目字典、发版脚本和预发布脚本。 编写Pipeline脚本 Pipeline脚本是Jenkins Pipeline的核心,用于定义自动化构建和发布的流程。以下是一个简单的Pipeline脚本示例: 在上述脚本中,定义了四个阶段:Checkout、Build、Push package和Deploy/Rollback。每个阶段都可以根据实际需求进行配置和调整。 通过Jenkins Pipeline自动化构建发布Java项目,可以显著提升开发效率和项目质量。借助Pipeline,我们能够轻松实现自动化构建、测试和部署,从而提高项目的整体质量和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值