二分匹配传送门[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 Stal - 网络流的更多相关文章

  1. [题解]poj 1274 The Perfect Stall(网络流)

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

  2. 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 农夫约翰上个 ...

  3. poj——1274 The Perfect Stall

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

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

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

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

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

  6. [POJ] 1274 The Perfect Stall(二分图最大匹配)

    题目地址:http://poj.org/problem?id=1274 把每个奶牛ci向它喜欢的畜栏vi连边建图.那么求最大安排数就变成求二分图最大匹配数. #include<cstdio> ...

  7. POJ 1274 The Perfect Stall (二分图匹配)

    [题目链接] http://poj.org/problem?id=1274 [题目大意] 给出一些奶牛和他们喜欢的草棚,一个草棚只能待一只奶牛, 问最多可以满足几头奶牛 [题解] 奶牛和喜欢的草棚连线 ...

  8. poj 1274 The Perfect Stall 解题报告

    题目链接:http://poj.org/problem?id=1274 题目意思:有 n 头牛,m个stall,每头牛有它钟爱的一些stall,也就是几头牛有可能会钟爱同一个stall,问牛与 sta ...

  9. POJ 1274 The Perfect Stall(二分图 && 匈牙利 && 最小点覆盖)

    嗯... 题目链接:http://poj.org/problem?id=1274 一道很经典的匈牙利算法的题目: 将每只奶牛看成二分图中左边的点,将牛圈看成二分图中右边的点,如果奶牛看上某个牛圈,就将 ...

随机推荐

  1. sqoop学习笔记

    #################################################################################################### ...

  2. iOS 项目架构tabbarController 嵌套 navbarController

    简单思路: 进入APP,首先加载 splashVC,加载完成之后,在viewDidAppear里跳转到loginVC,(这里一定要在viewDidLoad方法里新建loginVC跳转). 登陆成功之后 ...

  3. Linux执行Cron Job失败,在Shell sh下执行却能成功 - 环境变量?

    博客分类: Linux linuxcrontabpermissionetc/profile环境变量  一.我们常常碰到在shell下执行某个命令能够成功,比如执行一个java程序: java -jar ...

  4. Mirror--如何TSQL查看镜像状态和镜像相关存储过程

    --==================================================== --查看镜像状态 SELECT DB_NAME(database_id) AS Datab ...

  5. 理解SQL SERVER中的逻辑读,预读和物理读

    转自:https://www.cnblogs.com/CareySon/archive/2011/12/23/2299127.html 在我的上一篇关于SQL SERVER索引的博文,有圆友问道关于逻 ...

  6. dedecms自增标签[field:global.autoindex/]的运用

    用bootstrap建站时用到幻灯片切换模块,里面有个active(下面代码中的data-slide-to="0"),其余的按顺序递增(1,2),如果用dedecms就可以用aut ...

  7. js-jquery-插件开发(一)

    jQuery插件开发模式 jQuery插件开发方式主要有三种:1.通过$.extend()来扩展jQuery 主要是在jQuery命名空间或者理解成jQuery身上添加了一个静态方法2.通过$.fn ...

  8. [LeetCode] 121. Best Time to Buy and Sell Stock_Easy tag: Dynamic Programming

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  9. Hive学习笔记:基础语法

    Hive基础语法 1.创建表 – 用户表 CREATE [EXTERNAL外部表] TABLE [IF NOT EXISTS 是否存在] HUserInfo ( userid int comment ...

  10. 查询set、dict、dict.keys()的速度对比

    查找效率:set>dict>list 单次查询中: list set dict O(n) set做了去重,本质应该一颗红黑树 (猜测,STL就是红黑树),复杂度 O(logn): dict ...