POJ-1273-Drainage Ditches(网络最大流 标号法)

Drainage Ditches
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 69828 Accepted: 27070
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

题意:多组测试数据,每组数据第一行给出M,N代表有M条边,N个点。接着M行,每行三个数字,u,v,w,代表u到v容量为w,求汇点流入的总流量

思路:标号法水过。。。

代码

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
//标号法网络最大流
const int maxn=205;
int N;
int M;
struct node
{
    int c;//容量
    int f;//流量
} map[maxn][maxn];
int residul[maxn][maxn];//残留网络
int queue[maxn];//数组模拟队列
int q_star,q_end;
int path[maxn];//记录增广路径
int vis[maxn];//标记是否在队列中
int max_flow,min_flow;
int s,t;//源点和汇点
void init()//构图
{
    memset(map,0,sizeof(map));
    for(int i=0; i<M; i++)
    {
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w);
        map[u][v].c+=w;//容量累加
    }
}
void find_augment_path()//寻找增广路
{
    memset(vis,0,sizeof(vis));
    memset(residul,0,sizeof(residul));
    memset(path,0,sizeof(path));
    q_star=q_end=0;
    queue[q_end++]=s;
    path[s]=s;
    vis[s]=1;//标记进入队列
    while(q_star<q_end&&path[t]==0)//未增广到汇点前一直增广
    {
        int u=queue[q_star++];
        for(int i=1; i<=N; i++)
        {
            if(vis[i]==0)//该点未入队
            {
                if(map[u][i].c-map[u][i].f>0)
                {
                    residul[u][i]=map[u][i].c-map[u][i].f;//记录残留网络
                    path[i]=u;
                    queue[q_end++]=i;
                    vis[i]=1;
                }
                else if(map[i][u].f>0)//逆向流量
                {
                    residul[u][i]=map[i][u].f;
                    path[i]=u;
                    queue[q_end++]=i;
                    vis[i]=1;
                }
            }
        }
    }
}
void augment_flow()//计算可改进量
{
    min_flow=0;
    if(path[t]==0)
        return;
    min_flow=0x3f3f3f3f;//这个最大值可能有点小
    for(int i=t; i!=s; i=path[i])
        if(residul[path[i]][i]<min_flow)
            min_flow=residul[path[i]][i];
}
void update_flow()//调整流量
{
    if(path[t]==0)
        return;
    for(int i=t; i!=s; i=path[i])
    {
        if(map[path[i]][i].c-map[path[i]][i].f>0)
            map[path[i]][i].f+=min_flow;
        else if(map[i][path[i]].f>0)
            map[path[i]][i].f+=min_flow;
    }
}
void solve()
{
    s=1,t=N;
    max_flow=0;
    while(1)
    {
        find_augment_path();
        augment_flow();
//        printf("%d\n",min_flow);
        max_flow+=min_flow;
        if(min_flow>0)
            update_flow();
        else
            return;
    }
}
int main()
{
    while(~scanf("%d%d",&M,&N))
    {
        init();
        solve();
        printf("%d\n",max_flow);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值