二分匹配传送门[here]

原题传送门[here]


  题意大概说一下,就是有N头牛和M个牛棚,每头牛愿意住在一些牛棚,求最大能够满足多少头牛的要求。

  很明显就是一道裸裸的二分图最大匹配,但是为了练练网络流(做其它的题的时候,神奇地re掉了,于是就写基础题了)的最大流算法,就做做这道题。

  每一个牛都可一看成是个源点,每一个牛棚都可以看成是个汇点,但是一个网络应该只有一个汇点和一个源点才对,于是构造一个连接每个牛的超级源点,一个连接每个牛棚的超级汇点,每条边的容量为1,然后最大流Dinic算法(其它最大流算法也行)就行了。

Code极其不简洁的代码

 /**
* poj.org
* Problem#1274
* Accepted
* Time:16ms
* Memory:1980k
*/
#include<iostream>
#include<sstream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<vector>
#include<algorithm>
using namespace std;
typedef bool boolean;
#define smin(a, b) (a) = min((a), (b))
#define smax(a, b) (a) = max((a), (b))
#define INF 0xfffffff
template<typename T>
inline boolean readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-' && ~x);
if(!(~x)) return false;
if(x == '-'){
aFlag = -;
x = getchar();
}
for(u = x - ''; isdigit((x = getchar())); u = u * + x - '');
ungetc(x, stdin);
u *= aFlag;
return true;
}
///map template starts
typedef class Edge{
public:
int end;
int next;
int cap;
int flow;
Edge(const int end = , const int next = , const int cap = , const int flow = ):end(end), next(next), cap(cap), flow(flow){}
}Edge; typedef class MapManager{
public:
int ce;
int *h;
Edge *edge;
MapManager(){}
MapManager(int points, int limit):ce(){
h = new int[(const int)(points + )];
edge = new Edge[(const int)(limit + )];
memset(h, , sizeof(int) * (points + ));
}
inline void addEdge(int from, int end, int cap, int flow){
edge[++ce] = Edge(end, h[from], cap, flow);
h[from] = ce;
}
inline void addDoubleEdge(int from, int end, int cap){
addEdge(from, end, cap, );
addEdge(end, from, cap, cap);
}
Edge& operator [](int pos){
return edge[pos];
}
inline int reverse(int pos){ //反向边
return (pos & ) ? (pos + ) : (pos - );
}
inline void clear(){
delete[] edge;
delete h;
ce = ;
}
}MapManager; #define m_begin(g, i) (g).h[(i)]
#define m_end(g, i) (g).edge[(i)].end
#define m_next(g, i) (g).edge[(i)].next
#define m_cap(g, i) (g).edge[(i)].cap
#define m_flow(g, i) (g).edge[(i)].flow
///map template ends int n, m;
int t;
MapManager g; inline boolean init(){
if(!readInteger(n)) return false;
readInteger(m);
t = n + m + ;
g = MapManager(t + , (n + ) * (m + ) * );
for(int i = , a, b; i <= n; i++){
readInteger(a);
while(a--){
readInteger(b);
g.addDoubleEdge(i, b + n, );
}
}
for(int i = ; i <= n; i++)
g.addDoubleEdge(, i, );
for(int i = ; i <= m; i++)
g.addDoubleEdge(i + n, t, );
return true;
} boolean *visited;
int* divs;
queue<int> que; inline boolean getDivs(){
memset(visited, false, sizeof(boolean) * (t + ));
divs[] = ;
visited[] = true;
que.push();
while(!que.empty()){
int e = que.front();
que.pop();
for(int i = m_begin(g, e); i != ; i = g[i].next){
int& eu = g[i].end;
if(!visited[eu] && g[i].flow < g[i].cap){
divs[eu] = divs[e] + ;
visited[eu] = true;
que.push(eu);
}
}
}
return visited[t];
} int blockedflow(int node, int minf){
if(node == t || minf == ) return minf;
int f, flow = ;
for(int i = m_begin(g, node); i != ; i = m_next(g, i)){
int& e = g[i].end;
if(divs[e] == divs[node] + && visited[e] && (f = blockedflow(e, min(minf, g[i].cap - g[i].flow))) > ){
flow += f;
g[i].flow += f;
g[g.reverse(i)].flow -= f;
minf -= f;
if(minf == ) return flow;
}
}
return flow;
} inline int maxflow(){
visited = new boolean[(const int)(t + )];
divs = new int[(const int)(t + )];
int res = ;
while(getDivs()){
res += blockedflow(, INF);
}
return res;
} inline void solve(){
int res = maxflow();
cout << res << endl;
} inline void clear(){
delete[] visited;
delete[] divs;
g.clear();
} int main(){
while(init()){
solve();
clear();
}
return ;
}

[题解]poj 1274 The Perfect Stall(网络流)的更多相关文章

  1. Luogu 1894 [USACO4.2]完美的牛栏The Perfect Stall / POJ 1274 The Perfect Stall(二分图最大匹配)

    Luogu 1894 [USACO4.2]完美的牛栏The Perfect Stall / POJ 1274 The Perfect Stall(二分图最大匹配) Description 农夫约翰上个 ...

  2. poj——1274 The Perfect Stall

    poj——1274   The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25709   A ...

  3. POJ 1274 The Perfect Stall || POJ 1469 COURSES(zoj 1140)二分图匹配

    两题二分图匹配的题: 1.一个农民有n头牛和m个畜栏,对于每个畜栏,每头牛有不同喜好,有的想去,有的不想,对于给定的喜好表,你需要求出最大可以满足多少头牛的需求. 2.给你学生数和课程数,以及学生上的 ...

  4. POJ 1274 The Perfect Stall、HDU 2063 过山车(最大流做二分匹配)

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24081   Accepted: 106 ...

  5. poj 1274 The Perfect Stall【匈牙利算法模板题】

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20874   Accepted: 942 ...

  6. poj 1274 The Perfect Stall (二分匹配)

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17768   Accepted: 810 ...

  7. poj —— 1274 The Perfect Stall

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26274   Accepted: 116 ...

  8. [题解]poj 1274 The Prefect Stall

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22736   Accepted: 10144 Description Far ...

  9. poj 1274 The Perfect Stal - 网络流

    二分匹配传送门[here] 原题传送门[here] 题意大概说一下,就是有N头牛和M个牛棚,每头牛愿意住在一些牛棚,求最大能够满足多少头牛的要求. 很明显就是一道裸裸的二分图最大匹配,但是为了练练网络 ...

随机推荐

  1. IIS发布文件出现:未能加载文件或程序集“xxxx”或它的某一个依赖项。试图加载格式不正确的程序。

    解决方案:IIS——应用程序池—选中网站—高级设置——启用32位应用程序 :true.

  2. Android自学笔记:Git下载源代码

    Info:做J2ME几年了,现在基本没有公司用了,是时候向Android领域进军了. 自学中,难免会有疏漏,有问题请及时提出,共同学习共同进步. 2014-10-13:初版 2014-10-14:添加 ...

  3. 哆啦A梦 canvas

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. Samba日志分析

    Samba日志分析 随着我们文件共享安全级别的提高,越来越多的情况下需要对日志进行记录并审计.Linux平台下的Samba服务的配置文件是smb.conf,有不少图形化配置工具例如Webmin.smb ...

  5. js复制

    JS实现各种复制到剪贴板 一.实现点击按钮,复制文本框中的的内容                         <script type="text/javascript" ...

  6. 用ionic快速开发hybird App(已附源码,在下面+总结见解)

    1.ionic简介 ionic 是用于敏捷开发APP的解决方案.核心思路是:利用成熟的前端开发技术,来写UI和业务逻辑.也就是说,就是一个H5网站,这个区别于react-native,native.即 ...

  7. 谈谈Ruby中的类变量

    Ruby中的类变量,很多文章都是不太建议使用的,主要原因在于他的一些特性容易导致犯一些错误,尤其在广泛使用元编程的时候. 初步接触类变量可能觉得他跟C++的类静态成员和Java中的静态变量没什么区别, ...

  8. Struts2 XML配置详解

    struts官网下载地址:http://struts.apache.org/   1.    深入Struts2的配置文件 本部分主要介绍struts.xml的常用配置. 1.1.    包配置: S ...

  9. 关于C中struct和union长度的详解

    这几天看<代码大全>中的第十三章---不常见的数据类型,里面讲解到了C语言中的struct以及对指针的解释,联想到以前看过相关的关于C语言中stuct长度的文章,只是现在有些淡忘了,因此今 ...

  10. arcgis如何制作DEM数据

    DEM描述的是地面高程信息,它在测绘.水文.气象.地貌.地质.土壤.工程建设.通讯.军事等国民经济和国防建设以及人文和自然科学领域有着广泛的应用.如在工程建设上,可用于如土方量计算.通视分析等:在防洪 ...