poj1274 The Perfect Stall【二分图匹配】
来源:互联网 发布:平面设计图纸软件 编辑:程序博客网 时间:2024/06/05 23:45
题目链接:http://poj.org/problem?id=1274
题意:一个农场主,建了m个高科技产奶器,他由n头奶牛,每头奶牛值喜欢其中的几个产奶器,只有去自己喜欢的产奶器那里才能产奶,现问你最多几头牛能产奶
解析:二分图匹配的经典问题,直接上匈牙利
#include <iostream>#include <algorithm>#include <string>#include <cstring>#include <cstdio>using namespace std;const int maxn = 405;struct node{ int to,next;}edge[5*maxn];int head[maxn],cnt;int match[maxn],vis[maxn];void init(){ memset(head,-1,sizeof(head)); memset(match,0,sizeof(match)); cnt = 0;}void addEdge(int u,int v){ edge[cnt].to = v; edge[cnt].next = head[u]; head[u] = cnt++;}bool dfs(int u){ for(int i=head[u];i!=-1;i = edge[i].next) { int v = edge[i].to; if(!vis[v]) { vis[v] = 1; if(match[v]==0||dfs(match[v])) { match[v] = u; match[u] = v; return true; } } } return false;}int main(void){ int n,m; while(~scanf("%d %d",&n,&m)) { init(); for(int i=1;i<=n;i++) { int s,x; scanf("%d",&s); for(int j=0;j<s;j++) { scanf("%d",&x); addEdge(i,x+n); } } int ans = 0; for(int i=1;i<=n;i++) { memset(vis,0,sizeof(vis)); vis[i] = 1; if(dfs(i)) ans++; } printf("%d\n",ans); } return 0;}
阅读全文
0 0
- poj1274 The Perfect Stall(二分图匹配)
- poj1274 The Perfect Stall【二分图匹配】
- POJ1274 The Perfect Stall [二分图最大匹配 匈牙利算法]
- POJ1274 The Perfect Stall【二分图最大匹配】
- poj1274 The Perfect Stall(二分图匹配 / 最大流)
- POJ1274 The Perfect Stall(二分图)
- POJ1274 The Perfect Stall 简单最大二分匹配
- poj1274 The Perfect Stall 二分匹配简单题
- POJ1274:The Perfect Stall(二分图最大匹配 匈牙利算法)
- poj1274 The Perfect Stall 二分图最大匹配模板题 匈牙利算法BFS
- poj1274 The Perfect Stall(二分图模板题)
- POJ1274 The Perfect Stall 二分图,匈牙利算法
- POJ1274 The Perfect Stall
- POJ1274 The Perfect Stall
- poj1274 - The Perfect Stall
- poj1274 The Perfect Stall
- POJ1274 The Perfect Stall
- 【poj1274】 The Perfect Stall
- 对象初始化
- java语法
- Django+mysql配置与简单操作数据库实例
- 常见测试点总结
- Ubuntu16.04+hadoop2.7.3环境搭建
- poj1274 The Perfect Stall【二分图匹配】
- UVA 442
- sbt安装
- CodeForces 441 E.Valera and Number(概率DP)
- canvas合成海报所遇问题及解决方案总结
- tomcat学习二
- Subdirectory 【Introduction to Linux basics】
- 五种网站Web服务器技术和网页设计技术
- EM算法的丢硬币实例