BZOJ1711: [Usaco2007 Open]Dingin吃饭

1711: [Usaco2007 Open]Dingin吃饭

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 508  Solved: 259
[Submit][Status]

Description

农 夫JOHN为牛们做了很好的食品,但是牛吃饭很挑食. 每一头牛只喜欢吃一些食品和饮料而别的一概不吃.虽然他不一定能把所有牛喂饱,他还是想让尽可能多的牛吃到他们喜欢的食品和饮料. 农夫JOHN做了F (1 <= F <= 100) 种食品并准备了D (1 <= D <= 100) 种饮料. 他的N (1 <= N <= 100)头牛都以决定了是否愿意吃某种食物和喝某种饮料. 农夫JOHN想给每一头牛一种食品和一种饮料,使得尽可能多的牛得到喜欢的食物和饮料. 每一件食物和饮料只能由一头牛来用. 例如如果食物2被一头牛吃掉了,没有别的牛能吃食物2.

Input

* 第一行: 三个数: N, F, 和 D

* 第2..N+1行: 每一行由两个数开始F_i 和 D_i, 分别是第i 头牛可以吃的食品数和可以喝的饮料数.下F_i个整数是第i头牛可以吃的食品号,再下面的D_i个整数是第i头牛可以喝的饮料号码.

Output

* 第一行: 一个整数,最多可以喂饱的牛数.

Sample Input

4 3 3
2 2 1 2 3 1
2 2 2 3 1 2
2 2 1 3 1 2
2 1 1 3 3

输入解释:

牛 1: 食品从 {1,2}, 饮料从 {1,2} 中选
牛 2: 食品从 {2,3}, 饮料从 {1,2} 中选
牛 3: 食品从 {1,3}, 饮料从 {1,2} 中选
牛 4: 食品从 {1,3}, 饮料从 {3} 中选

Sample Output

3
输出解释:

一个方案是:
Cow 1: 不吃
Cow 2: 食品 #2, 饮料 #2
Cow 3: 食品 #1, 饮料 #1
Cow 4: 食品 #3, 饮料 #3
用鸽笼定理可以推出没有更好的解 (一共只有3总食品和饮料).当然,别的数据会更难.

HINT

Source

Gold

题解:

比较好想的一道网络流。

如果一头牛可以吃多顿的话就直接在所有可能的组合间连边然后dinic即可

现在牛有了容量限制,自然而然想到把牛拆点,然后就可以解决了。

代码:

  1 #include<cstdio>
  2 
  3 #include<cstdlib>
  4 
  5 #include<cmath>
  6 
  7 #include<cstring>
  8 
  9 #include<algorithm>
 10 
 11 #include<iostream>
 12 
 13 #include<vector>
 14 
 15 #include<map>
 16 
 17 #include<set>
 18 
 19 #include<queue>
 20 
 21 #include<string>
 22 
 23 #define inf 1000000000
 24 
 25 #define maxn 100000
 26 
 27 #define maxm 500000
 28 
 29 #define eps 1e-10
 30 
 31 #define ll long long
 32 
 33 #define pa pair<int,int>
 34 
 35 #define for0(i,n) for(int i=0;i<=(n);i++)
 36 
 37 #define for1(i,n) for(int i=1;i<=(n);i++)
 38 
 39 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
 40 
 41 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
 42 
 43 #define mod 1000000007
 44 
 45 using namespace std;
 46 
 47 inline int read()
 48 
 49 {
 50 
 51     int x=0,f=1;char ch=getchar();
 52 
 53     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
 54 
 55     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
 56 
 57     return x*f;
 58 
 59 }
 60 int  n,m,s,t,x,y,maxflow,tot=1,a[maxn],b[maxn],head[maxn],cur[maxn],h[maxn],q[maxn];
 61 
 62 struct edge{int go,next,v;}e[maxm];
 63 
 64 void ins(int x,int y,int z){e[++tot].go=y;e[tot].v=z;e[tot].next=head[x];head[x]=tot;}
 65 
 66 void insert(int x,int y,int z){ins(x,y,z);ins(y,x,0);}
 67 
 68 bool bfs()
 69 
 70 {
 71 
 72     for(int i=s;i<=t;i++)h[i]=-1;
 73 
 74     int l=0,r=1;q[1]=s;h[s]=0;
 75 
 76     while(l<r)
 77 
 78     {
 79 
 80         int x=q[++l];
 81 
 82         for(int i=head[x];i;i=e[i].next)
 83 
 84          if(e[i].v&&h[e[i].go]==-1)
 85 
 86          {
 87 
 88             h[e[i].go]=h[x]+1;q[++r]=e[i].go;
 89 
 90          }
 91 
 92     }
 93 
 94     return h[t]!=-1;
 95 
 96 }
 97 
 98 int dfs(int x,int f)
 99 
100 {
101 
102     if(x==t) return f;
103 
104     int tmp,used=0;
105 
106     for(int i=head[x];i;i=e[i].next)
107 
108      if(e[i].v&&h[e[i].go]==h[x]+1)
109 
110     {
111 
112         tmp=dfs(e[i].go,min(e[i].v,f-used));
113 
114         e[i].v-=tmp;if(e[i].v)cur[x]=i;
115 
116         e[i^1].v+=tmp;used+=tmp;
117 
118         if(used==f)return f;       
119 
120     }
121 
122     if(!used) h[x]=-1;
123 
124     return used;
125 
126 }
127 
128 void dinic()
129 
130 {
131 
132     maxflow=0;
133 
134     while(bfs())
135 
136     {
137 
138         for (int i=s;i<=t;i++)cur[i]=head[i];maxflow+=dfs(s,inf);
139 
140     }
141 
142 }
143 
144 int main()
145 
146 {
147 
148     freopen("input.txt","r",stdin);
149 
150     freopen("output.txt","w",stdout);
151 
152     n=read();x=read();y=read();
153     s=0;t=2*n+x+y+1;
154     for1(i,n)insert(i,i+n,1);
155     for1(i,x)insert(s,2*n+i,1);
156     for1(i,y)insert(2*n+x+i,t,1);
157     for1(i,n)
158     {
159         int xx=read(),yy=read();
160         for1(j,xx)insert(2*n+read(),i,1);
161         for1(j,yy)insert(n+i,2*n+x+read(),1);
162     }
163     dinic();
164     printf("%d\n",maxflow);
165 
166     return 0;
167 
168 }
View Code

 

posted @ 2014-10-16 18:55  ZYF-ZYF  Views(192)  Comments(0Edit  收藏  举报