博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
The Perfect Stall [USACO]
阅读量:4992 次
发布时间:2019-06-12

本文共 4383 字,大约阅读时间需要 14 分钟。

这题还是可以用最大流。只要添加一个源点和一个汇点,并添加源点至牛,汇点至各牧场的边即可转化为标准的最大流

不过,关于二分匹配有专用的算法,匈牙利算法,本质上来说,匈牙利算法应该属于XXX-XXX方法,就是添加增广路径的那个。只不过在二分匹配里面的增广路径是有特点的----它是一个交错路径(从起点算起1..2k+1,第奇数条边为非匹配的边,第偶数条边为匹配过的边)。交错路的搜索可以bfs,也可以dfs。 dfs写起来比较简单,bfs则需要记录前驱从而在搜索成功后回溯匹配过程。

第一次提交时,我用的dfs在test8时超时了,原因与dfs代码中被我注释掉的那句有关。

改了之后提交OK,又顺便试了一下bfs,也没有问题。

 

----------------------------------------------------------------------------------------------------------------------

/*ID: zhangyc1LANG: C++TASK: stall4*/#include 
#include
#include
#include
#include
using namespace std;ofstream fileout("stall4.out");const int MAXNODENUM = 201;int arrLinkL[MAXNODENUM], arrLinkR[MAXNODENUM], arrPre[MAXNODENUM];bool arrVisited[MAXNODENUM];int N, M, nCount = 0;struct SCowStall { int nSize; int arrStalls[MAXNODENUM - 1];};SCowStall arrCowStall[MAXNODENUM];void prepairData(){ ifstream filein("stall4.in"); filein >> N >> M; for (int i = 1; i<= N; i++) { filein >> arrCowStall[i].nSize; for (int j = 0; j < arrCowStall[i].nSize; j++) { filein >> arrCowStall[i].arrStalls[j]; } } filein.close(); memset(arrLinkL, 0, sizeof(arrLinkL)); memset(arrLinkR, 0, sizeof(arrLinkR));}bool dfs(int x){ for (int j = 0; j < arrCowStall[x].nSize; j++) { int y = arrCowStall[x].arrStalls[j]; if (!arrVisited[y]) { arrVisited[y] = true; if (arrLinkR[y] == 0 || dfs(arrLinkR[y])) { arrLinkL[x] = y; arrLinkR[y] = x; return true; } // 这个很重要,因为在一次子过程在y节点失败,则以后无论怎样搜索路径,到y节点都会失败。可以反证来证明。 //arrVisited[y] = false; } } return false;}bool bfs(int u){ memset(arrPre, 0, sizeof(arrPre)); queue
q; q.push(u); arrVisited[u] = true; bool bFound = false; while(!q.empty()) { int x = q.front(); q.pop(); for (int i = 0; i < arrCowStall[x].nSize; i++) { int y = arrCowStall[x].arrStalls[i]; if (arrLinkR[y] == 0) { // 找到了增广路径 int yy = y; while (arrLinkL[x] != 0) { int temp = arrLinkL[x]; arrLinkL[x] = yy; arrLinkR[yy] = x; yy = temp; x = arrPre[yy]; } arrLinkL[x] = yy; arrLinkR[yy] = x; return true; } // 防止重复 else if (!arrVisited[arrLinkR[y]]) { arrPre[y] = x; q.push(arrLinkR[y]); arrVisited[arrLinkR[y]] = true; } } } return false;}void process(){ for (int i = 1; i <= N; i++) { // 这个条件可以省去,因为每次更新路径只会改变左侧图中1号至当前节点的匹配情况 //if (arrLinkL[i] == 0) { memset(arrVisited, 0, sizeof(arrVisited)); if (bfs(i)) { nCount++; } } } fileout << nCount << endl;}int main(){ prepairData(); process(); fileout.close(); return 0;}

---------------------------------------------------题目如下------------------------

The Perfect Stall
Hal Burch

Farmer John completed his new barn just last week, complete with all the latest milking technology. Unfortunately, due to engineering problems, all the stalls in the new barn are different. For the first week, Farmer John randomly assigned cows to stalls, but it quickly became clear that any given cow was only willing to produce milk in certain stalls. For the last week, Farmer John has been collecting data on which cows are willing to produce milk in which stalls. A stall may be only assigned to one cow, and, of course, a cow may be only assigned to one stall.

Given the preferences of the cows, compute the maximum number of milk-producing assignments of cows to stalls that is possible.

PROGRAM NAME: stall4

INPUT FORMAT

Line 1: One line with two integers, N (0 <= N <= 200) and M (0 <= M <= 200). N is the number of cows that Farmer John has and M is the number of stalls in the new barn.
Line 2..N+1: N lines, each corresponding to a single cow. The first integer (Si) on the line is the number of stalls that the cow is willing to produce milk in (0 <= Si <= M). The subsequent Si integers on that line are the stalls in which that cow is willing to produce milk. The stall numbers will be integers in the range (1..M), and no stall will be listed twice for a given cow.

SAMPLE INPUT (file stall4.in)

5 52 2 53 2 3 42 1 53 1 2 51 2

OUTPUT FORMAT

A single line with a single integer, the maximum number of milk-producing stall assignments that can be made.

SAMPLE OUTPUT (file stall4.out)

4

转载于:https://www.cnblogs.com/doublemystery/archive/2013/03/19/2968903.html

你可能感兴趣的文章
强制修改常量的值
查看>>
Grunt 初体验
查看>>
hive跑mapreduce报java.lang.RuntimeException: Error in configuring object
查看>>
ArcGIS中的坐标系统定义与投影转换方法
查看>>
机械臂的碰撞检测资料
查看>>
[UnityShader基础]01.渲染队列
查看>>
字符串转整型C++
查看>>
随机生成红包算法
查看>>
Datatable get请求传参应用
查看>>
杭电1170
查看>>
3unit8
查看>>
kettle与各数据库建立链接的链接字符串
查看>>
【转】Apache Solr 访问权限控制
查看>>
PostgreSQL - 转义字符
查看>>
两步搞定一台电脑同时开启多个tomcat
查看>>
jQuery EasyUI弹出确认对话框(确认操作中.....)
查看>>
CentOS7 监控网络流量
查看>>
根据控件名称反射查找控件
查看>>
编写和执行C#代码的插件:CS-Script for Notepad++
查看>>
BZOJ3569: DZY Loves Chinese II(线性基构造)
查看>>