[POJ 1274]The Perfect Stall

【问题描述】

  Farmer John completed his new barn just last week, complete with all the latest milking technology. Unfortunately, due to engineering problems, all the stalls in the new barn are different. For the first week, Farmer John randomly assigned cows to stalls, but it quickly became clear that any given cow was only willing to produce milk in certain stalls. For the last week, Farmer John has been collecting data on which cows are willing to produce milk in which stalls. A stall may be only assigned to one cow, and, of course, a cow may be only assigned to one stall. 
Given the preferences of the cows, compute the maximum number of milk-producing assignments of cows to stalls that is possible. 

【输入】

  The input includes several cases. For each case, the first line contains two integers, N (0 <= N <= 200) and M (0 <= M <= 200). N is the number of cows that Farmer John has and M is the number of stalls in the new barn. Each of the following N lines corresponds to a single cow. The first integer (Si) on the line is the number of stalls that the cow is willing to produce milk in (0 <= Si <= M). The subsequent Si integers on that line are the stalls in which that cow is willing to produce milk. The stall numbers will be integers in the range (1..M), and no stall will be listed twice for a given cow.

【输出】

  For each case, output a single line with a single integer, the maximum number of milk-producing stall assignments that can be made.

【算法分析】

  裸的二分图匹配,不解释。

【程序代码】

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #define maxn 10000
 5 #define inf 1000000
 6 using namespace std;
 7 int n,m,x,y,z;
 8 struct graph{
 9     int start,end,pointsize,tot;
10     int c[maxn<<1],to[maxn<<1],next[maxn<<1],head[maxn],dis[maxn],nowhead[maxn];
11     void clear(){
12         tot=1;
13         memset(c,0,sizeof(c));
14         memset(to,0,sizeof(to));
15         memset(next,0,sizeof(next));
16         memset(head,0,sizeof(head));
17         memset(dis,0,sizeof(dis));
18         memset(nowhead,0,sizeof(nowhead));
19     }
20     void addedge(int a,int b,int l){
21         c[++tot]=l;to[tot]=b;next[tot]=head[a];head[a]=tot;
22         c[++tot]=0;to[tot]=a;next[tot]=head[b];head[b]=tot;
23     }
24     int q[maxn],ql,qr;
25     bool BFS(){
26         for (int i=1;i<=pointsize;++i) nowhead[i]=head[i],dis[i]=0;
27         ql=1; qr=0; q[++qr]=end;
28         while (ql<=qr){
29             for (int k=q[ql++],p=head[k];p;p=next[p])
30                 if(c[p^1]&&!dis[to[p]]&&to[p]!=end) dis[q[++qr]=to[p]]=dis[k]+1;
31         }
32         return dis[start];
33     }
34     int DFS(int k,int maxflow){
35         if (k==end) return maxflow;
36         int flow=0,tflow;
37         for (int&p=nowhead[k];p&&maxflow;p=next[p])
38             if(c[p]&&dis[to[p]]+1==dis[k]&&(tflow=DFS(to[p],min(maxflow,c[p]))))
39                 c[p]-=tflow,c[p^1]+=tflow,maxflow-=tflow,flow+=tflow;
40         return flow;
41     }
42     int dinic(int a,int b){
43         int flow=0;
44         start=a; end=b;
45         while (BFS()) flow+=DFS(a,inf);
46         return flow;
47     }    
48     graph(){
49         tot=1;
50     }
51 } G;
52 int main(){
53     while (scanf("%d%d",&n,&m)!=EOF){
54         G.clear();
55         G.pointsize=m+n+2;
56         for (int i=1;i<=n;++i){
57             scanf("%d",&x);
58             for (int j=1;j<=x;++j){
59                 scanf("%d",&y);
60                 G.addedge(i,y+n,1);
61             }
62         }
63         for (int i=1;i<=n;++i)     G.addedge(m+n+1,i,1);
64         for (int i=1;i<=m;++i)  G.addedge(i+n,m+n+2,1);
65         printf("%d\n",G.dinic(m+n+1,m+n+2));
66     }
67     return 0;
68 }

声明:本博文为博主原创博文,未经允许请勿转载。

posted on 2016-02-22 15:29  Double680  阅读(159)  评论(0编辑  收藏  举报

导航