poj 1274(网络流解二分图的最大匹配)
2016-06-02 09:24
429 查看
The Perfect Stall
Description
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.
Input
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.
Output
For
each case, output a single line with a single integer, the maximum
number of milk-producing stall assignments that can be made.
Sample Input
Sample Output
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 22539 | Accepted: 10072 |
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.
Input
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.
Output
For
each case, output a single line with a single integer, the maximum
number of milk-producing stall assignments that can be made.
Sample Input
5 5 2 2 5 3 2 3 4 2 1 5 3 1 2 5 1 2
Sample Output
4 题意:农夫养了n头牛,建了m个牲口棚,他发现每头牛只在特定的一些牲口棚才能产奶,问最多有多少头牛能够同时产奶?? 题解:二分图的最大匹配,设立一个超级源点,向每头牛连一条长度为1的单向边,建立超级汇点,每个牲口棚向超级汇点连一条长度为1的单向边,然后每头牛向其喜爱的牲口棚连一条长度不小于1的边,求最大流即可。
#include <stdio.h> #include <algorithm> #include <queue> #include <string.h> #include <math.h> #include <iostream> using namespace std; const int N = 505; const int INF = 999999999; struct Edge{ int v,w,next; }edge[N*N]; int head ; int level ; int tot; void init(){ memset(head,-1,sizeof(head)); tot=0; } void addEdge(int u,int v,int w,int &k){ edge[k].v = v,edge[k].w=w,edge[k].next=head[u],head[u]=k++; edge[k].v = u,edge[k].w=0,edge[k].next=head[v],head[v]=k++; } int BFS(int src,int des){ queue<int >q; memset(level,0,sizeof(level)); level[src]=1; q.push(src); while(!q.empty()){ int u = q.front(); q.pop(); if(u==des) return 1; for(int k = head[u];k!=-1;k=edge[k].next){ int v = edge[k].v,w=edge[k].w; if(level[v]==0&&w!=0){ level[v]=level[u]+1; q.push(v); } } } return -1; } int dfs(int u,int des,int increaseRoad){ if(u==des) return increaseRoad; int ret=0; for(int k=head[u];k!=-1;k=edge[k].next){ int v = edge[k].v,w=edge[k].w; if(level[v]==level[u]+1&&w!=0){ int MIN = min(increaseRoad-ret,w); w = dfs(v,des,MIN); edge[k].w -=w; edge[k^1].w+=w; ret+=w; if(ret==increaseRoad) return ret; } } return ret; } int Dinic(int src,int des){ int ans = 0; while(BFS(src,des)!=-1) ans+=dfs(src,des,INF); return ans; } int main(){ int n,m; while(scanf("%d%d",&n,&m)!=EOF){ init(); int src = 0,des = n+m+1; for(int i=1;i<=n;i++){ addEdge(src,i,1,tot); int t,v; scanf("%d",&t); while(t--){ scanf("%d",&v); addEdge(i,v+n,1,tot); } } for(int i=n+1;i<=n+m;i++){ addEdge(i,des,1,tot); } printf("%d\n",Dinic(src,des)); } }
相关文章推荐
- JSP页面顶端出现“红色”的报错信息:The superclass "javax.servlet.http.HttpServlet" was not
- Retrofit A type-safe HTTP client for Android and Java
- AFN网络请求打印请求失败的错误
- 利用https实现站点的访问
- 利用https实现站点的访问
- 利用https实现站点的访问
- 社交网络中常用数据集
- centos6.7上使用qperf测试网络的带宽和时延!
- iOS开发 - 网络数据安全加密(MD5)
- Java下载网络压缩包及解压
- 网络协议_SNMP协议详解
- Json解析网络数据+listview展示跳转
- xml解析网络请求
- socket网络字节序以及大端序小端序
- TCP四次握手断开连接(图解)
- TCP数据的传输过程
- TCP数据报结构以及三次握手(图解)
- TCP的粘包问题以及数据的无边界性
- debian网络和源设置(新手)
- AnG-顶级的数字营销服务和软件提供商-http://www.agrant.cn