USACO Section 4.2: The Perfect Stall
这题关键就在将题转换成最大流模板题。首先有一个原始点,N个cow个点, M个barn点和一个终点,原始点到cow点和barn点到终点的流都为1,而cow对应的barn就是cow点到对应barn点的流,为1.这样题目就转换成了原始点到终点的最大流问题
/*
ID: yingzho1
LANG: C++
TASK: stall4
*/
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <vector>
#include <set>
#include <algorithm>
#include <stdio.h>
#include <queue>
#include <cstring>
#include <cmath>
#include <list>
#include <cstdio>
#include <cstdlib>
#include <limits>
#include <stack>
using namespace std;
ifstream fin("stall4.in");
ofstream fout("stall4.out");
;
;
int N, M;
*MAX][*MAX], f[*MAX][*MAX], pre[*MAX], inc[*MAX];
bool bfs(int s, int d) {
queue<int> que;
; i <= N+M+; i++) pre[i] = -;
que.push(s);
inc[s] = INF;
while (!que.empty()) {
int u = que.front();
que.pop();
; i <= N+M+; i++) {
&& f[u][i] < g[u][i]) {
inc[i] = min(inc[u], g[u][i]-f[u][i]);
pre[i] = u;
if (i == d) return true;
que.push(i);
}
}
}
return false;
}
int edmond_karp(int s, int d) {
;
while (bfs(s, d)) {
maxflow += inc[d];
for (int i = d; i != s; i = pre[i]) {
f[pre[i]][i] += inc[d];
f[i][pre[i]] -= inc[d];
}
}
return maxflow;
}
int main()
{
fin >> N >> M;
int s, d;
; i <= N; i++) {
g[][+i] = ;
fin >> s;
; j < s; j++) {
fin >> d;
g[+i][+N+d] = ;
}
}
; i <= N+M+; i++) g[i][N+M+] = ;
fout << edmond_karp(, N+M+) << endl;
;
}
USACO Section 4.2: The Perfect Stall的更多相关文章
- USACO Section 4.2 The Perfect Stall(二分图匹配)
二分图的最大匹配.我是用最大流求解.加个源点s和汇点t:s和每只cow.每个stall和t 连一条容量为1有向边,每只cow和stall(that the cow is willing to prod ...
- USACO 4.2 The Perfect Stall(二分图匹配匈牙利算法)
The Perfect StallHal Burch Farmer John completed his new barn just last week, complete with all the ...
- usaco training 4.2.2 The Perfect Stall 最佳牛栏 题解
The Perfect Stall题解 Hal Burch Farmer John completed his new barn just last week, complete with all t ...
- POJ1274 The Perfect Stall[二分图最大匹配]
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23911 Accepted: 106 ...
- poj 1247 The Perfect Stall 裸的二分匹配,但可以用最大流来水一下
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16396 Accepted: 750 ...
- POJ1274 The Perfect Stall
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25739 Accepted: 114 ...
- POJ1274 The Perfect Stall[二分图最大匹配 Hungary]【学习笔记】
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23911 Accepted: 106 ...
- poj 1274 The Perfect Stall (二分匹配)
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 17768 Accepted: 810 ...
- poj——1274 The Perfect Stall
poj——1274 The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25709 A ...
随机推荐
- Netsharp快速入门(之11) 销售管理(开发销售订单工作区)
作者:秋时 杨昶 时间:2014-02-15 转载须说明出处 4.3 销售订单开发 4.3.1 部件工作区设置 1.创建部件工作区,建工作区向导中要注意勾选组合并系部分.具体要建立的部 ...
- How to find and fix Bash Shell-shock vulnerability CVE-2014-6271 in unix like system
type command - env x='() { :;}; echo vulnerable' bash -c 'echo hello' in your terminal. if your sy ...
- vmware workstation 10.0
2013.9.3 vmware workstation 10.0 build 1295980新增功能– 可以将windows 8.1物理pc转变为虚拟机:unity模式增强,与windows 8.1 ...
- linux系统非ROOT用户80端口不能启动tomcat问题的变通办法——通过Iptables端口转发
2010-07-17 13:21:42 org.apache.tomcat.util.digester.SetPropertiesRule begin 警告: [SetPropertiesRule]{ ...
- SQL Server优化
虽然查询速度慢的原因很多,但是如果通过一定的优化,也可以使查询问题得到一定程度的解决. 查询速度慢的原因很多,常见如下几种: 没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) I/ ...
- WARNING: Calls to any function that may require a gradient calculation inside a conditional block may return undefined results
GLES2.0: Some device will give a warning on compling shaders(yet the compling will succeed), and the ...
- JavaScript之match()方法讲解
定义和用法 match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配. 该方法类似 indexOf() 和 lastIndexOf(),但是它返回指定的值,而不是字符串的位置. ...
- Keil RTX systick 初始化
在STM32F215上移植Keil的RTX操作系统,随便设置下就能好使,但是当我想知道systick到底是怎么设置的时候,就得翻翻代码了,原来在 rt_HAL_CM.h中以一个内联函数的形式定义的 _ ...
- HDU 1465 不容易系列之一(错排,递归)
简而言之,就是把n个信封全部装错的可能数.(中问题,具体看题目) //当n个编号元素放在n个编号位置,元素编号与位置编号各不对应的方法数用M(n)表示, //那么M(n-1)就表示n-1个编号元素放在 ...
- MJRefresh插件引起的错误
添加的头部或者尾部刷新,离开这个界面的时候需要移除 - (void)dealloc { [_tableView removeHeader];} 不同版本的处理的方式不同 报的错误: 类的一个实例 ...