[题解]poj 1274 The Perfect Stall(网络流)
二分匹配传送门[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(网络流)的更多相关文章
- 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 农夫约翰上个 ...
 - poj——1274   The Perfect Stall
		
poj——1274 The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25709 A ...
 - POJ 1274 The Perfect Stall || POJ 1469 COURSES(zoj 1140)二分图匹配
		
两题二分图匹配的题: 1.一个农民有n头牛和m个畜栏,对于每个畜栏,每头牛有不同喜好,有的想去,有的不想,对于给定的喜好表,你需要求出最大可以满足多少头牛的需求. 2.给你学生数和课程数,以及学生上的 ...
 - POJ 1274 The Perfect Stall、HDU 2063 过山车(最大流做二分匹配)
		
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24081 Accepted: 106 ...
 - poj 1274 The Perfect Stall【匈牙利算法模板题】
		
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20874 Accepted: 942 ...
 - poj 1274 The Perfect Stall (二分匹配)
		
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 17768 Accepted: 810 ...
 - poj ——  1274  The Perfect Stall
		
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26274 Accepted: 116 ...
 - [题解]poj 1274 The Prefect Stall
		
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22736 Accepted: 10144 Description Far ...
 - poj 1274 The Perfect Stal - 网络流
		
二分匹配传送门[here] 原题传送门[here] 题意大概说一下,就是有N头牛和M个牛棚,每头牛愿意住在一些牛棚,求最大能够满足多少头牛的要求. 很明显就是一道裸裸的二分图最大匹配,但是为了练练网络 ...
 
随机推荐
- QTP处理各类鼠标事件fireEvent
			
在做web测试时,页面上的HTML标签会包含各类事件:单击(onclick).双击(onDblClick).光标聚集(OnBlur).onchange.onfocus.onmousedown.onmo ...
 - 再谈vertical-align与line-height
			
每次遇到多个inline-block元素排列的怪异垂直位置的问题的时候都可以通过经验,设置vertical-align来解决,没深入研究过,现在需要分析总结下这个问题. 问题引出 有小鲜肉新做了个页面 ...
 - 在osx下通过vmware无GUI方式运行centos 7
			
启动虚拟机: /Applications/VMware\ Fusion.app/Contents/Library/vmrun -T fusion start "CentOS 64-bit.v ...
 - String类常用方法
			
1.String类的特点,字符串一旦被初始化就不会被改变. 2.String对象定义的两种方式 ①String s = "affdf";这种定义方式是在字符串常量池中创建一个Str ...
 - Monkey基础
			
一.Monkey工具简介 Monkey是Android中的一个命令行工具,可以运行在模拟器里或实际设备中.它向系统发送伪随机的用户事件流(如按键输入.触摸屏输入.手势输入等),实现对正在开发的应用程序 ...
 - declaration expected but identifier 'firedac' found
			
老是出现这种错误,原来是//注解时出来标点错误!
 - java性能调优工具
			
windows调优工具: 任务管理器(ctrl+alt+delete或).资源管理器(任务管理器->性能进入或运行resmon.exe):JVM分析工具Jconsole,jProfile,Vis ...
 - 40、dom以xml结尾的文件
			
1.student.xml文件 <?xml version="1.0" encoding="utf-8" ?> <!-- 1.书写根元素(因为 ...
 - JS 中 new 操作符
			
按照javascript语言精粹中所说,如果在一个函数前面带上new来调用该函数,那么将创建一个隐藏连接到该函数的prototype成员的新对象,同时this将被绑定到那个新对象上.这个话很抽象,我想 ...
 - ubuntu查看系统资源占用(内存,cpu和进程)
			
ubuntu查看系统资源占用(内存,cpu和进程) 1 top 查看ubuntu的资源占用的命令为$: top 说明:top命令就可以查看内存,cpu和进程了,很方便 top: 主要参数 d: ...