POJ-1273-Drainage Ditches 朴素增广路

Drainage Ditches
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 70588 Accepted: 27436

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

[Submit]   [Go Back]   [Status]   [Discuss]

解题思路:

是一道裸的一般增广路算法的题目,就是计算水塘最大的流量排到附近的小河中。

增广路中可能含有反向弧,最后流量改进的时候对于反向弧的处理,既可以按照残留网络中的对应的正向弧那样增加流量也可以直接把这条反向弧的流量减少相应的流量。

因为我用这两种解法都能AC,说明效果是一样的。




源代码:

<span style="font-size:18px;">
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;

#define MAXN 210

struct Matrix
{
    int c,f;//容量,流量
};
Matrix Edge[MAXN][MAXN];//流及容量(邻接矩阵)
int M,N;//弧的条数,顶点个数
int s,t;//源点(1),汇点(n)
int residul[MAXN][MAXN];//残留网络
int qu[MAXN*MAXN],qs,qe;//队列、队列头、队列尾
int pre[MAXN];//pre[i]为增广路上顶点的访问标志
int vis[MAXN];//BFS算法中各个顶点的访问标志
int maxflow,min_augment;//最大流流量、每次增广时的可以改进的量
void find_augment_path()//BFS寻找增广路
{
    int i,cu;//cu为队列头顶点
    memset(vis,0,sizeof(vis));
    qs=0;qu[qs]=s;//s入队列
    pre[s]=s;vis[s]=1;qe=1;
    memset(residul,0,sizeof(residul));memset(pre,0,sizeof(pre));
    while(qs<qe&&pre[t]==0)
    {
        cu=qu[qs];
        for(i=1;i<=N;i++)
        {
            if(vis[i]==0)
            {
                if(Edge[cu][i].c-Edge[cu][i].f>0)
                {
                    residul[cu][i]=Edge[cu][i].c-Edge[cu][i].f;
                    pre[i]=cu;qu[qe++]=i;vis[i]=1;
                }
                else if(Edge[i][cu].f>0)
                {
                    residul[cu][i]=Edge[i][cu].f;
                    pre[i]=cu;qu[qe++]=i;vis[i]=1;
                }
            }
        }
        qs++;
    }
}
void augment_flow()//计算可改进量
{
    int i=t,j;//t为汇点
    if(pre[i]==0)
    {
        min_augment=0;return;
    }
    j=0x7fffffff;
    while(i!=s)//计算增广路上可改进量的最小值
    {
        if(residul[pre[i]][i]<j) j=residul[pre[i]][i];
        i=pre[i];
    }
    min_augment=j;
}
void update_flow()//调整流量
{
    int i=t;//t为汇点
    if(pre[i]==0)return;
    while(i!=s)
    {
        if(Edge[pre[i]][i].c-Edge[pre[i]][i].f>0)
            Edge[pre[i]][i].f+=min_augment;
        else if(Edge[i][pre[i]].f>0) Edge[pre[i]][i].f+=min_augment;//或者写成Edge[i][pre[i]]-=min_augment;
        i=pre[i];
    }
}
void solve()
{
    s=1;t=N;
    maxflow=0;
    while(1)
    {
        find_augment_path();//BFS寻找增广路
        augment_flow();//计算可改进量
        maxflow+=min_augment;
        if(min_augment>0) update_flow();
        else return;
    }
}
int main()
{
    int i;
    int u,v,c;
    while(scanf("%d %d",&M,&N)!=EOF)
    {
        memset(Edge,0,sizeof(Edge));
        for(i=0;i<M;i++)
        {
            scanf("%d %d %d",&u,&v,&c);
            Edge[u][v].c+=c;
        }
        solve();
        printf("%d\n",maxflow);
    }
    return 0;
}
</span>


转载于:https://www.cnblogs.com/lemonbiscuit/p/7776052.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值