1. 程式人生 > >Dining(最大流)

Dining(最大流)

Dining

http://poj.org/problem?id=3281

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 23462   Accepted: 10371

Description

Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others.

Farmer John has cooked fabulous meals for his cows, but he forgot to check his menu against their preferences. Although he might not be able to stuff everybody, he wants to give a complete meal of both food and drink to as many cows as possible.

Farmer John has cooked F (1 ≤ F ≤ 100) types of foods and prepared D

 (1 ≤ D ≤ 100) types of drinks. Each of his N (1 ≤ N ≤ 100) cows has decided whether she is willing to eat a particular food or drink a particular drink. Farmer John must assign a food type and a drink type to each cow to maximize the number of cows who get both.

Each dish or drink can only be consumed by one cow (i.e., once food type 2 is assigned to a cow, no other cow can be assigned food type 2).

Input

Line 1: Three space-separated integers:  NF, and  D 
Lines 2.. N+1: Each line  i starts with a two integers  Fi and  Di, the number of dishes that cow  i likes and the number of drinks that cow  i likes. The next  Fi integers denote the dishes that cow  i will eat, and the  Di integers following that denote the drinks that cow  i will drink.

Output

Line 1: A single integer that is the maximum number of cows that can be fed both food and drink that conform to their wishes

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

Sample Output

3

Hint

One way to satisfy three cows is: 
Cow 1: no meal 
Cow 2: Food #2, Drink #2 
Cow 3: Food #1, Drink #1 
Cow 4: Food #3, Drink #3 
The pigeon-hole principle tells us we can do no better since there are only three kinds of food or drink. Other test data sets are more challenging, of course.

 

建圖是關鍵。。。

把牛拆成兩個點才能起到限流的作用

源點連食物,食物連牛in,牛in連牛out,牛out連飲料,飲料連匯點

 

  1 #include<iostream>
  2 #include<cstring>
  3 #include<string>
  4 #include<cmath>
  5 #include<cstdio>
  6 #include<algorithm>
  7 #include<queue>
  8 #include<vector>
  9 #include<set>
 10 #define maxn 100005
 11 #define MAXN 100005
 12 #define mem(a,b) memset(a,b,sizeof(a))
 13 const int N=200005;
 14 const int M=200005;
 15 const int INF=0x3f3f3f3f;
 16 using namespace std;
 17 int n;
 18 struct Edge{
 19     int v,next;
 20     int cap,flow;
 21 }edge[MAXN*20];//注意這裡要開的夠大。。不然WA在這裡真的想罵人。。問題是還不報RE。。
 22 int cur[MAXN],pre[MAXN],gap[MAXN],path[MAXN],dep[MAXN];
 23 int cnt=0;//實際儲存總邊數
 24 void isap_init()
 25 {
 26     cnt=0;
 27     memset(pre,-1,sizeof(pre));
 28 }
 29 void isap_add(int u,int v,int w)//加邊
 30 {
 31     edge[cnt].v=v;
 32     edge[cnt].cap=w;
 33     edge[cnt].flow=0;
 34     edge[cnt].next=pre[u];
 35     pre[u]=cnt++;
 36 }
 37 void add(int u,int v,int w){
 38     isap_add(u,v,w);
 39     isap_add(v,u,0);
 40 }
 41 bool bfs(int s,int t)//其實這個bfs可以融合到下面的迭代裡,但是好像是時間要長
 42 {
 43     memset(dep,-1,sizeof(dep));
 44     memset(gap,0,sizeof(gap));
 45     gap[0]=1;
 46     dep[t]=0;
 47     queue<int>q;
 48     while(!q.empty())
 49     q.pop();
 50     q.push(t);//從匯點開始反向建層次圖
 51     while(!q.empty())
 52     {
 53         int u=q.front();
 54         q.pop();
 55         for(int i=pre[u];i!=-1;i=edge[i].next)
 56         {
 57             int v=edge[i].v;
 58             if(dep[v]==-1&&edge[i^1].cap>edge[i^1].flow)//注意是從匯點反向bfs,但應該判斷正向弧的餘量
 59             {
 60                 dep[v]=dep[u]+1;
 61                 gap[dep[v]]++;
 62                 q.push(v);
 63                 //if(v==sp)//感覺這兩句優化加了一般沒錯,但是有的題可能會錯,所以還是註釋出來,到時候視情況而定
 64                 //break;
 65             }
 66         }
 67     }
 68     return dep[s]!=-1;
 69 }
 70 int isap(int s,int t)
 71 {
 72     if(!bfs(s,t))
 73     return 0;
 74     memcpy(cur,pre,sizeof(pre));
 75     //for(int i=1;i<=n;i++)
 76     //cout<<"cur "<<cur[i]<<endl;
 77     int u=s;
 78     path[u]=-1;
 79     int ans=0;
 80    // cout<<dep[s]<<" "<<n<<endl;
 81     while(dep[s]<n)//迭代尋找增廣路
 82     {
 83         if(u==t)
 84         {
 85             int f=INF;
 86             for(int i=path[u];i!=-1;i=path[edge[i^1].v])//修改找到的增廣路
 87                 f=min(f,edge[i].cap-edge[i].flow);
 88             for(int i=path[u];i!=-1;i=path[edge[i^1].v])
 89             {
 90                 edge[i].flow+=f;
 91                 edge[i^1].flow-=f;
 92             }
 93             ans+=f;
 94             u=s;
 95             continue;
 96         }
 97         bool flag=false;
 98         int v;
 99         for(int i=cur[u];i!=-1;i=edge[i].next)
100         {
101             v=edge[i].v;
102             if(dep[v]+1==dep[u]&&edge[i].cap-edge[i].flow)
103             {
104                 cur[u]=path[v]=i;//當前弧優化
105                 flag=true;
106                 break;
107             }
108         }
109         if(flag)
110         {
111             u=v;
112             continue;
113         }
114         int x=n;
115         if(!(--gap[dep[u]]))return ans;//gap優化
116         for(int i=pre[u];i!=-1;i=edge[i].next)
117         {
118             if(edge[i].cap-edge[i].flow&&dep[edge[i].v]<x)
119             {
120                 x=dep[edge[i].v];
121                 cur[u]=i;//常數優化
122             }
123         }
124         dep[u]=x+1;
125         gap[dep[u]]++;
126         if(u!=s)//當前點沒有增廣路則後退一個點
127         u=edge[path[u]^1].v;
128      }
129      return ans;
130 }
131 
132 
133 int main(){
134     int m,s,t;
135     int F,D;
136     scanf("%d %d %d",&n,&F,&D);
137     int a,b,c;
138     isap_init();
139     int x1[105],x2[105];
140     int f,d;
141     for(int i=1;i<=n;i++){
142         scanf("%d %d",&f,&d);
143         for(int j=1;j<=f;j++){
144             scanf("%d",&x1[j]);
145             add(x1[j],F+i,1);
146         }
147         for(int j=1;j<=d;j++){
148             scanf("%d",&x2[j]);
149             for(int k=1;k<=f;k++){
150                 add(n+F+i,x2[j]+n+n+F,1);
151             }
152         }
153     }
154     for(int i=1;i<=F;i++){
155         add(0,i,1);
156     }
157     for(int i=1;i<=n;i++){
158         add(F+i,n+F+i,1);
159     }
160     for(int i=1;i<=D;i++){
161         add(i+n+n+F,F+n+n+D+1,1);
162     }
163     s=0,t=F+n+n+D+1;
164     n=F+n+n+D+2;
165     printf("%d\n",isap(s,t));
166 }
167 
168 
169 /*
170 2 2 2
171 2 1 1 2 1
172 1 1 1 2
173 
174 2
175 */
View Code