Food HDU - 4292 (结点容量 拆点) Dinic

时间:2023-03-09 20:21:26
Food HDU - 4292  (结点容量 拆点) Dinic
You, a part-time dining service worker in your college’s dining hall, are now confused with a new problem: serve as many people as possible. 
  The issue comes up as people in your college are more and more difficult to serve with meal: They eat only some certain kinds of food and drink, and with requirement unsatisfied, go away directly. 
  You have prepared F (1 <= F <= 200) kinds of food and D (1 <= D <= 200) kinds of drink. Each kind of food or drink has certain amount, that is, how many people could this food or drink serve. Besides, You know there’re N (1 <= N <= 200) people and you too can tell people’s personal preference for food and drink. 
  Back to your goal: to serve as many people as possible. So you must decide a plan where some people are served while requirements of the rest of them are unmet. You should notice that, when one’s requirement is unmet, he/she would just go away, refusing any service. 

Input  There are several test cases. 
  For each test case, the first line contains three numbers: N,F,D, denoting the number of people, food, and drink. 
  The second line contains F integers, the ith number of which denotes amount of representative food. 
  The third line contains D integers, the ith number of which denotes amount of representative drink. 
  Following is N line, each consisting of a string of length F. e jth character in the ith one of these lines denotes whether people i would accept food j. “Y” for yes and “N” for no. 
  Following is N line, each consisting of a string of length D. e jth character in the ith one of these lines denotes whether people i would accept drink j. “Y” for yes and “N” for no. 
  Please process until EOF (End Of File). 
Output  For each test case, please print a single line with one integer, the maximum number of people to be satisfied. 
Sample Input

4 3 3
1 1 1
1 1 1
YYN
NYY
YNY
YNY
YNY
YYN
YYN
NNY

Sample Output

3

题意:

有一些个数有限的  不同种类的糖果 和 一些不同种类的饮料 , 每个人的口味不同,所以可以选择任意的糖果和饮料 , 每个都只选一个

求能满足最多的人的数量

解析:

建立超级源点s和超级汇点t  把s和糖果连在一起,边权为糖果的数量,  t和饮料连载一其,边权为饮料的数量, 然后把每一个人拆成两个点 其中边权为1

人和糖果、饮料 都建立边 边权为INF;

代码如下:

Dinic + 当前弧优化

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <cmath>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn =, INF = 0x7fffffff;
int cnt = , s, t;
int head[maxn], d[maxn], cur[maxn];
char str[];
struct node{
int u, v, c, next;
}Node[maxn*]; void add_(int u, int v, int c)
{
Node[cnt].u = u;
Node[cnt].v = v;
Node[cnt].c = c;
Node[cnt].next = head[u];
head[u] = cnt++;
} void add(int u,int v,int c)
{
add_(u,v,c);
add_(v,u,);
} bool bfs()
{
queue<int> Q;
mem(d,);
Q.push(s);
d[s] = ;
while(!Q.empty())
{
int u = Q.front(); Q.pop();
for(int i=head[u]; i!=-; i=Node[i].next)
{
node e = Node[i];
if(!d[e.v] && e.c > )
{
d[e.v] = d[e.u] + ;
// cout<< e.v << " " << d[e.v] <<endl;
Q.push(e.v);
if(e.v == t) return ;
}
}
}
// cout<< d[t] <<endl;
return d[t] != ;
} int dfs(int u,int cap)
{
if(u == t || cap == )
return cap;
int ret = ;
for(int &i=cur[u]; i!=-; i=Node[i].next)
{
node e = Node[i];
if(d[e.v] == d[e.u] + && e.c > )
{
int V = dfs(e.v, min(cap, e.c));
Node[i].c -= V;
Node[i^].c += V;
cap -= V;
ret += V;
if(cap == ) break;
}
}
if(cap > ) d[u] = -;
return ret;
} int Dinic()
{
int ans = ;
while(bfs())
{
memcpy(cur,head,sizeof(head));
ans += dfs(s,INF);
}
return ans;
}
int main()
{
int n, f, d;
while(~scanf("%d%d%d",&n,&f,&d))
{
cnt = ;
mem(head,-);
int temp;
s = , t = f+d+n+n+;
for(int i=; i<=f; i++)
{
scanf("%d",&temp);
add(s,i,temp);
}
for(int i=; i<=d; i++)
{
scanf("%d",&temp);
add(f+i,t,temp);
}
for(int i=; i<=n; i++)
{
scanf("%s",str);
for(int j=; j<f; j++)
{
if(str[j] == 'Y'){
add(j+,f+d+i,INF);
}
}
}
for(int i=; i<=n; i++)
add(f+d+i, f+d+n+i,);
for(int i=; i<=n; i++)
{
scanf("%s",str);
for(int j=; j<d; j++)
{
if(str[j] == 'Y')
add(f+d+n+i,f+j+,INF);
}
}
cout<< Dinic() <<endl;
} return ;
}