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 preparedD (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
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
Sample Output
3
Hint
人生第一道网络流!!!思路大致是建一个流网络将问题转化为裸的最大流。先设一个超级源点和一个超级汇点,源点向每种食物建一条cap为1的弧,每种饮料向汇点建一条cap为1的弧(饮料和食物可互换)。接下来讨论每头牛,由于一头牛只能与一种饮料和一种食物匹配,故此处应考虑拆点,将每个牛拆分为左牛和右牛,每头牛的左向右建一条cap为1弧即可满足上述要,再将每种食物和饮料与喜欢的牛建弧,cap同为1,跑一次最大流即可。
调了一晚上,为了练习算法遂用ISAP和Dinic各写了一遍。一开始用ISAP写的,由于总点数的计算有误导致不能回流。。。其实maxflow里面 “d[s] < N” 的 “N” 是你建的流网络的总点数也是增广路长度的上限,一开始脑子短路了莫名其妙给了个 6。。。,总分算是调出来了。Dinic就顺利多了,1A。上代码:
//Dinic
#include
#include
#include
#include
#include
using namespace std;
typedef struct node
{
int from,to,cap,flow;
node(int f = 0,int t = 0,int c = 0,int ff = 0):from(f),to(t),cap(c),flow(ff){}
}node;
const int INF = 1<<30;
const int maxn = 400 + 5;
int n,f,d,s,t,N;
vector<node> edge;
vector<int> G[maxn];
int cur[maxn],di[maxn];
bool vis[maxn];
void addedge(int from,int to,int cap,int flow)
{
edge.push_back(node(from,to,cap,0));
edge.push_back(node(to,from,0,0));
int m = edge.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool BFS()
{
int x;
memset(vis,false,sizeof(vis));
queue<int> q;
q.push(s);
vis[s] = true;
di[s] = 0;
while(!q.empty())
{
x = q.front();
q.pop();
for(int i = 0;i < G[x].size(); ++i)
{
node &e = edge[G[x][i]];
if(e.cap > e.flow && !vis[e.to])
{
di[e.to] = di[x] + 1;
vis[e.to] = true;
q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x,int a)
{
int flow = 0,f;
if(x == t || a == 0) return a;
for(int &i = cur[x];i< G[x].size(); ++i)
{
node &e = edge[G[x][i]];
if(di[e.to] == di[x] + 1 && (f = DFS(e.to,min(a,e.cap - e.flow))) > 0)
{
e.flow += f;
edge[G[x][i]^1].flow -= f;
flow += f;
a -= f;
if(a == 0) break;
}
}
return flow;
}
int maxflow()
{
int flow = 0;
while(BFS())
{
memset(cur,0,sizeof(cur));
flow += DFS(s,INF);
}
return flow;
}
int main()
{
int x,y,p;
cin >> n >> f >> d;
s = 0,t = 400 + 1;
for(int i = 1;i <= f; ++i) addedge(s,i,1,0); //food
for(int i = 1;i <= d; ++i) addedge(i + 300,t,1,0);//drink
for(int i = 1;i <= n; ++i) addedge(i + 100,i + 200,1,0);//cow
for(int i = 1;i <= n; ++i)
{
cin >> x >> y;
for(int j = 0;j < x; ++j)
{
cin >> p;
addedge(p,i + 100,1,0);
}//food
for(int j = 0;j < y; ++j)
{
cin>>p;
addedge(i + 200,p + 300,1,0);
}//drink
}
cout<<maxflow()<<endl;
return 0;
}
//ISAP
#include
#include
#include
#include
#include
using namespace std;
typedef struct node
{
int from,to,cap,flow;
node(int f = 0,int t = 0,int c = 0,int ff = 0):from(f),to(t),cap(c),flow(ff){}
}node;
const int INF = 1<<30;
const int maxn = 400 + 5;
int n,f,d,s,t,N;
vector<node> edge;
vector<int> G[maxn];
int p[maxn],num[maxn],cur[maxn],a[maxn],di[maxn];
bool vis[maxn];
void addedge(int from,int to,int cap,int flow)
{
edge.push_back(node(from,to,cap,0));
edge.push_back(node(to,from,0,0));
int m = edge.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
void BFS()
{
queue<int> q;
memset(di,0,sizeof(di));
memset(vis,false,sizeof(vis));
q.push(t);
di[t] = 0;
vis[t] = true;
while(!q.empty())
{
int x = q.front();
q.pop();
for(int i = 0;i < G[x].size(); ++i)
{
node &e = edge[G[x][i]];
if(!vis[e.to] && e.cap == 0)
{
vis[e.to] = true;
di[e.to] = di[x] + 1;
q.push(e.to);
}
}
}
}
int add()
{
int x = t,a = INF;
while(x != s)
{
node &e = edge[p[x]];
a = min(a,e.cap-e.flow);
x = edge[p[x]].from;
}
x = t;
while(x != s)
{
edge[p[x]].flow +=a;
edge[p[x]^1].flow -= a;
x = edge[p[x]].from;
}
return a;
}
int maxflow()
{
int flow = 0;
BFS();
memset(cur,0,sizeof(cur));
memset(num,0,sizeof(num));
for(int i = 0;i <= maxn - 5; ++i) num[di[i]]++;
num[0] = INF,N = 2 * n + 2 + f + d;
int x = s;
while(di[s] < N)
{
if(x == t)
{
flow += add();
x = s;
}
bool ok = false;
for(int i = cur[x];i < G[x].size(); ++i)
{
node &e = edge[G[x][i]];
if(e.cap > e.flow && di[e.to] + 1== di[x])
{
ok = true;
p[e.to] = G[x][i];
cur[x] = i;
x = e.to;
break;
}
}
if(!ok)
{
int m = N;
for(int i = 0;i < G[x].size(); ++i)
{
node &e = edge[G[x][i]];
if(e.cap > e.flow) m = min(m,di[e.to]);
}
if(--num[di[x]] == 0) break;
num[di[x] = m + 1]++;
cur[x] = 0;
if(x != s) x = edge[p[x]].from;
}
}
return flow;
}
int main()
{
int x,y,p;
cin >> n >> f >> d;
s = 0,t = 400 + 1;
for(int i = 1;i <= f; ++i) addedge(s,i,1,0); //food
for(int i = 1;i <= d; ++i) addedge(i + 300,t,1,0);//drink
for(int i = 1;i <= n; ++i) addedge(i + 100,i + 200,1,0);//cow
for(int i = 1;i <= n; ++i)
{
cin >> x >> y;
for(int j = 0;j < x; ++j)
{
cin >> p;
addedge(p,i + 100,1,0);
}//food
for(int j = 0;j < y; ++j)
{
cin>>p;
addedge(i + 200,p + 300,1,0);
}//drink
}
cout<<maxflow()<<endl;
return 0;
}