Drainage Ditches(最大流)

Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlock any pighouse because he doesn't have the keys. Customers come to the farm one after another. Each of them has keys to some pig-houses and wants to buy a certain number of pigs. 
All data concerning customers planning to visit the farm on that particular day are available to Mirko early in the morning so that he can make a sales-plan in order to maximize the number of pigs sold. 
More precisely, the procedure is as following: the customer arrives, opens all pig-houses to which he has the key, Mirko sells a certain number of pigs from all the unlocked pig-houses to him, and, if Mirko wants, he can redistribute the remaining pigs across the unlocked pig-houses. 
An unlimited number of pigs can be placed in every pig-house. 
Write a program that will find the maximum number of pigs that he can sell on that day.

Input

The first line of input contains two integers M and N, 1 <= M <= 1000, 1 <= N <= 100, number of pighouses and number of customers. Pig houses are numbered from 1 to M and customers are numbered from 1 to N. 
The next line contains M integeres, for each pig-house initial number of pigs. The number of pigs in each pig-house is greater or equal to 0 and less or equal to 1000.
The next N lines contains records about the customers in the following form ( record about the i-th customer is written in the (i+2)-th line): 
A K1 K2 ... KA B It means that this customer has key to the pig-houses marked with the numbers K1, K2, ..., KA (sorted nondecreasingly ) and that he wants to buy B pigs. Numbers A and B can be equal to 0.

Output

The first and only line of the output should contain the number of sold pigs.

Sample Input

3 3
3 1 10
2 1 2 2
2 1 3 3
1 2 6

Sample Output

7

题意:农场里有一套排水系统,由于下大雨,需要通过排水沟将农场里的水排到池塘中,每个排水沟都有一个水速调节阀。yi已知每条排水沟的连通状况以及每条排水沟的最大载流量,求最多能同时排除多少加仑水。

思路:很明显这道题是容量网络的最大流(图论书 P271),可以采取最短增广路算法求解,通过在残留网络中寻找从源s到汇点t的最短路径的方式寻找增广路并增广,最终求得网络最大流。

代码如下:

#include<cstdio>
#include<cstring>
#define N 210
struct node//邻接矩阵,存放流,及容量
{
    int c,f;
}edge[N][N];
int m,n;//排水沟的数目,和节点数
int s,t;//源s,汇点t
int residual[N][N];//残留网络
int qu[N*N],qe,qs;//队列,队首和队尾
int pre[N];   //增广路上顶点i前面的顶点序号
int vis[N];   //bfs标记
int maxflow,min_augment;//最大流量,每次增广时的可改进量
void find_augment_path()  //bfs求增广路
{
    int i,cu;//队首顶点
    memset(vis,0,sizeof(vis));//初始化
    memset(residual,0,sizeof(residual));
    memset(pre,0,sizeof(pre));
    qs=0,qe=1,qu[qs]=s,vis[s]=1;//入队列
    while(qs<qe&&pre[t]==0) //队不为空
    {
        cu=qu[qs]; //取队首
        for(i=1;i<=n;i++)
        {
            if(!vis[i])
            {
                if(edge[cu][i].c-edge[cu][i].f>0)
                {
                    residual[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)
                {
                    residual[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])
    {
        min_augment=0;
        return;
    }
    j=0x3f3f3f3f;
    while(i!=s)  //计算增广路上可改进量的最小值
    {
        if(residual[pre[i]][i]<j)
            j=residual[pre[i]][i];
        i=pre[i];
    }
    min_augment=j;
}
void update_flow() //调整流量
{
    int i=t;
    if(!pre[i])
        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;
        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 u,v,c;
    while(~scanf("%d%d",&m,&n))
    {
        memset(edge,0,sizeof(edge));
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&u,&v,&c);
            edge[u][v].c+=c;
        }
        solve();
        printf("%d\n",maxflow);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值