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 ...
随机推荐
- Careercup - Google面试题 - 5085331422445568
2014-05-08 23:45 题目链接 原题: How would you use Dijkstra's algorithm to solve travel salesman problem, w ...
- ubuntu(Eclipse+JDK) 自动安装脚本
sudo rm -rf jdk1.8.0_40sudo rm -rf /usr/lib/jvm sudo tar -zxvf jdk-8u40-linux-i586.tar.gzsudo mkdir ...
- Readhat Linux5.5 安装SVNService(经验总结)
Subversion独立服务和与apache整合服务. 一 .Svn独立服务安装 操作系统: Redhat Linux5.5 安装包获取: 下载 http://subversion.tigris.or ...
- Extjs 选择元素涉及方法总结
本文主要是解释Extjs在使用过程中使用的相关选择方法: 1.首先解释第一组概念: Ext.get(String/HTMLElement/Ext.Element el) Ext.getCmp(Stri ...
- poi实现Excel比较
http://stackoverflow.com/questions/866346/easiest-way-to-compare-two-excel-files-in-java http://stac ...
- ios 缓存相关信息收集
链接:http://www.cnblogs.com/pengyingh/category/353093.html 使用NSURLCache让本地数据来代替远程UIWebView请求 摘要: 原文作者: ...
- Game shader or System shader is busy ::VS CSG
this error means The GPU is freezen Phyre::PSemaphoreOrbis::wait()//callstack something illegal in c ...
- extjs4与ckeditor、ckfinder整合
<script type="text/javascript"src="<?php echo Yii::app()->request->baseUr ...
- 编译libcore-amr静态库
在此链接下 https://github.com/feuvan/opencore-amr-iOS 下载它的源码到本地, 然后cd到此目录下,在终端输入命令./build_ios_xcode6.sh,便 ...
- C++中的RAII机制
http://www.jellythink.com/archives/101 前言 在写C++设计模式——单例模式的时候,在写到实例销毁时,设计的GC类是很巧妙的,而这一巧妙的设计就是根据当对象的生命 ...