码迷,mamicode.com
首页 > 其他好文 > 详细

PIGS(最大流)

时间:2015-11-06 22:11:23      阅读:329      评论:0      收藏:0      [点我收藏+]

标签:

PIGS
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 18742   Accepted: 8511

Description

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

   这道题目的大意是这样的:

  • 有 M 个猪圈(M ≤ 1000),每个猪圈里初始时有若干头猪。
  • 一开始所有猪圈都是关闭的。
  • 依次来了 N 个顾客(N ≤ 100),每个顾客分别会打开指定的几个猪圈,从中买若干头猪。
  • 每个顾客分别都有他能够买的数量的上限。
  • 每个顾客走后,他打开的那些猪圈中的猪,都可以被任意地调换到其它开着的猪圈里,然后所有猪圈重新关上。

    问总共最多能卖出多少头猪。

    举个例子来说。有 3 个猪圈,初始时分别有 3、 1 和 10 头猪。依次来了 3 个顾客,第一个打开 1 号 和 2 号猪圈,最多买 2 头;第二个打开 1 号 和 3 号猪圈,最多买 3 头;第三个打开 2 号猪圈,最多买 6 头。那么,最好的可能性之一就是第一个顾客从 1 号圈买 2 头,然后把 1 号圈剩下的 1 头放到 2 号圈;第二个顾客从 3 号圈买 3 头;第三个顾客从 2 号圈买 2 头。总共卖出 2 + 3 + 2 = 7 头。□

    不难想像,这个问题的网络模型可以很直观地构造出来。就拿上面的例子来说,可以构造出图 1 所示的模型(图中凡是没有标数字的边,容量都是 +∞):

  • 三个顾客,就有三轮交易,每一轮分别都有 3 个猪圈和 1 个顾客的节点。
  • 从源点到第一轮的各个猪圈各有一条边,容量就是各个猪圈里的猪的初始数量。
  • 从各个顾客到汇点各有一条边,容量就是各个顾客能买的数量上限。
  • 在某一轮中,从该顾客打开的所有猪圈都有一条边连向该顾客,容量都是 +∞。
  • 最后一轮除外,从每一轮的 i 号猪圈都有一条边连向下一轮的 i 号猪圈,容量都是 +∞,表示这一轮剩下的猪可以留到下一轮。
  • 最后一轮除外,从每一轮被打开的所有猪圈,到下一轮的同样这些猪圈,两两之间都要连一条边,表示它们之间可以任意流通。

 

技术分享

 

图 1


    不难想像,这个网络模型的最大流量就是最多能卖出的数量。图中最多有 2 + N + M × N ≈ 100,000 个节点。□

    这个模型虽然很直观,但是节点数太多了,计算速度肯定会很慢。其实不用再想别的算法,就让我们继续上面的例子,用合并的方法来简化这个网络模型。

    首先,最后一轮中没有打开的猪圈就可以从图中删掉了,也就是图 2 中红色的部分,显然它们对整个网络的流量没有任何影响。

技术分享

图 2


    接着,看图 2 中蓝色的部分。根据我总结出的以下几个规律,可以把这 4 个点合并成一个:

    规律 1. 如果几个节点的流量的来源完全相同,则可以把它们合并成一个。

    规律 2. 如果几个节点的流量的去向完全相同,则可以把它们合并成一个。

    规律 3. 如果从点 u 到点 v 有一条容量为 +∞ 的边,并且 u 是 v 的唯一流量来源,或者 v 是 u 的唯一流量去向,则可以把 u 和 v 合并成一个节点。

    根据规律 1,可以把蓝色部分右边的 1、 2 号节点合并成一个;根据规律 2,可以把蓝色部分左边的 1、 2 号节点合并成一个;最后,根据规律 3,可以把蓝色部分的左边和右边(已经分别合并成了一个节点)合并成一个节点。于是,图 2 被简化成了图 3 的样子。也就是说,最后一轮除外,每一轮被打开的猪圈和下一轮的同样这些猪圈都可以被合并成一个点。

技术分享

图 3


    接着,根据规律 3图 3 中的蓝色节点、2 号猪圈和 1 号顾客这三点可以合并成一个;图 3 中的两个 3 号猪圈和 2 号顾客也可以合并成一个点。当然,如果两点之间有多条同向的边,则这些边可以合并成一条,容量相加,这个道理很简单,就不用我多说了。最终,上例中的网络模型被简化成了图 4 的样子。□

技术分享

 

图 4


    让我们从图 4 中重新总结一下构造这个网络模型的规则:

  • 每个顾客分别用一个节点来表示。
  • 对于每个猪圈的第一个顾客,从源点向他连一条边,容量就是该猪圈里的猪的初始数量。如果从源点到一名顾客有多条边,则可以把它们合并成一条,容量相加。
  • 对于每个猪圈,假设有 n 个顾客打开过它,则对所有整数 i ∈ [1, n),从该猪圈的第 i 个顾客向第 i + 1 个顾客连一条边,容量为 +∞。
  • 从各个顾客到汇点各有一条边,容量是各个顾客能买的数量上限。

    拿我们前面一直在讲的例子来说:1 号猪圈的第一个顾客是 1 号顾客,所以从源点到 1 号顾客有一条容量为 3 的边;1 号猪圈的第二个顾客是 2 号顾客,因此从 1 号顾客到 2 号顾客有一条容量为 +∞ 的边;2 号猪圈的第一个顾客也是 1 号顾客,所以从源点到 1 号顾客有一条容量为 1 的边,和之前已有的一条边合并起来,容量变成 4;2 号猪圈的第二个顾客是 3 号顾客,因此从 1 号顾客到 3 号顾客有一条容量为 +∞ 的边;3 号猪圈的第一个顾客是 2 号顾客,所以从源点到 2 号顾客有一条容量为 10 的边。□

    新的网络模型中最多只有 2 + N = 102 个节点,计算速度就可以相当快了。可以这样理解这个新的网络模型:对于某一个顾客,如果他打开了猪圈 h,则在他走后,他打开的所有猪圈里剩下的猪都有可能被换到 h 中,因而这些猪都有可能被 h 的下一个顾客买走。所以对于一个顾客打开的所有猪圈,从该顾客到各猪圈的下一个顾客,都要连一条容量为 +∞ 的边。

    在面对网络流问题时,如果一时想不出很好的构图方法,不如先构造一个最直观,或者说最“硬来”的模型,然后再用合并节点和边的方法来简直化这个模型。经过简化以后,好的构图思路自然就会涌现出来了。这是解决网络流问题的一个好方法。

代码:

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<cmath>
  5 #include<algorithm>
  6 #include<queue>
  7 #include<vector>
  8 using namespace std;
  9 #define mem(x,y) memset(x,y,sizeof(x))
 10 const int INF=0x3f3f3f3f;
 11 const int MAXN=110;
 12 const int MAXM=11000<<1;
 13 struct Edge{
 14     int from,to,next,cup,flow;
 15 };
 16 Edge edg[MAXM];
 17 vector<int>vec[1010];
 18 int head[MAXM],dis[MAXN],edgnum,vis[MAXN],c[MAXM];
 19 int pig[1100],want[MAXN];
 20 queue<int>dl;
 21 void initial(){
 22     edgnum=0;mem(head,-1);
 23 }
 24 void add(int u,int v,int w){
 25     int i;
 26     for(i=head[u];i!=-1;i=edg[i].next)
 27         if(edg[i].to==v)break;
 28         if(i!=-1){
 29             if(w!=INF)edg[i].cup+=w;return;
 30         }
 31     Edge E={u,v,head[u],w,0};
 32     edg[edgnum]=E;
 33     head[u]=edgnum++;
 34     E={v,u,head[v],0,0};
 35     edg[edgnum]=E;
 36     head[v]=edgnum++;
 37 }
 38 bool bfs(int s,int e){
 39     mem(dis,-1);mem(vis,0);
 40     while(!dl.empty())dl.pop();
 41     dis[s]=0;vis[s]=1;dl.push(s);
 42     while(!dl.empty()){
 43         int u=dl.front();
 44         dl.pop();
 45         for(int i=c[u];i!=-1;i=edg[i].next){
 46             Edge v=edg[i];
 47             if(!vis[v.to]&&v.cup-v.flow){
 48                 vis[v.to]=1;
 49                 dis[v.to]=dis[u]+1;
 50                 dl.push(v.to);
 51                 if(v.to==e)return true;
 52             }
 53         }
 54     }
 55     return false;
 56 }
 57 int dfs(int x,int la,int e){
 58     if(x==e||la==0)return la;
 59     int temp,flow=0;
 60     for(int i=c[x];i!=-1;i=edg[i].next){
 61         Edge &v=edg[i];
 62         if(dis[v.to]==dis[x]+1&&(temp=dfs(v.to,min(la,v.cup-v.flow),e))>0){
 63             flow+=temp;
 64             v.flow+=temp;
 65             edg[i^1].flow-=temp;
 66             la-=temp;
 67             if(la==0)break;
 68         }
 69     }
 70     return flow;
 71 }
 72 int maxflow(int s,int e){
 73     int flow=0;
 74     while(bfs(s,e))memcpy(c,head,sizeof(head)),flow+=dfs(s,INF,e);
 75     return flow;
 76 }
 77 int main(){
 78     int M,N,t,id;
 79     while(~scanf("%d%d",&M,&N)){
 80         initial();
 81         for(int i=1;i<=M;i++)scanf("%d",pig+i),vec[i].clear();
 82         for(int i=1;i<=N;i++){
 83             scanf("%d",&t);
 84             while(t--){
 85                 scanf("%d",&id);
 86                 vec[id].push_back(i);
 87             }
 88             scanf("%d",want+i);
 89             add(i,N+1,want[i]);
 90         }
 91         for(int i=1;i<=M;i++){
 92             if(vec[i].size()==0)continue;
 93             int x=vec[i][0],y;
 94             add(0,x,pig[i]);
 95             for(int j=1;j<vec[i].size();j++){
 96             //    x=vec[i][j-1];
 97                  y=vec[i][j];
 98                 add(x,y,INF);
 99             }
100         }
101         //for(int i=1;i<=N;i++)add(i,N+1,want[i]);
102         printf("%d\n",maxflow(0,N+1));
103     }
104     return 0;
105 }

 

PIGS(最大流)

标签:

原文地址:http://www.cnblogs.com/handsomecui/p/4943613.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!