这题关键就在将题转换成最大流模板题。首先有一个原始点,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的更多相关文章

  1. USACO Section 4.2 The Perfect Stall(二分图匹配)

    二分图的最大匹配.我是用最大流求解.加个源点s和汇点t:s和每只cow.每个stall和t 连一条容量为1有向边,每只cow和stall(that the cow is willing to prod ...

  2. USACO 4.2 The Perfect Stall(二分图匹配匈牙利算法)

    The Perfect StallHal Burch Farmer John completed his new barn just last week, complete with all the ...

  3. 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 ...

  4. POJ1274 The Perfect Stall[二分图最大匹配]

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

  5. poj 1247 The Perfect Stall 裸的二分匹配,但可以用最大流来水一下

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16396   Accepted: 750 ...

  6. POJ1274 The Perfect Stall

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25739   Accepted: 114 ...

  7. POJ1274 The Perfect Stall[二分图最大匹配 Hungary]【学习笔记】

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

  8. poj 1274 The Perfect Stall (二分匹配)

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17768   Accepted: 810 ...

  9. poj——1274 The Perfect Stall

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

随机推荐

  1. 第五周&第六周

    学习进度表  周数 专业学习目标 学习时间 新增代码行 博客发表量 人文方面学习 知识总结 第四周 认真掌握老师上课所讲的内容,在课外多学习一些知识    5小时 50          1   阅读 ...

  2. ionice

    ionice – 获取或设置程序的IO调度与优先级. 命令格式: ionice [[-c class] [-n classdata] [-t]] -p PID [PID]… ionice [-c cl ...

  3. Facebook 和 Google 如何激发工程师的创造力

    原文链接:http://kb.cnblogs.com/page/193450/ 今天终于“朝圣”了两个伟大的公司——Facebook和Google,对创造力和驱动力的来源有了更多的理解,尤其是对于典型 ...

  4. 【BZOJ】【3238】【AHOI2013】diff(差异)

    题目链接:www.lydsy.com/JudgeOnline/problem.php?id=3238 后缀数组 这题题面给的暗示性就很强啊……一看就是要用后缀xx一家的算法,由于本蒻只会后缀数组所以就 ...

  5. color mask

    https://msdn.microsoft.com/zh-cn/library/windows/desktop/bb173595(v=vs.85).aspx void OMSetBlendState ...

  6. Asp.net MVC 实现图片上传剪切

    使用技术:Asp.net MVC与jquery.uploadify,Jcrop 首先上页面 01 <strong><!DOCTYPE html> 02  <html> ...

  7. 山寨小小军团开发笔记 之 GamePool

    很多时候我们对于物体(比如弓箭)大量的生成与销毁,这个时候可以把弓箭放在内存池中进行管理,加快体验.自己Copy了一个简易版的. 一.代码 GameObjectPoolManager.cs using ...

  8. Winform设置相关

    >>  Winform查找根目录 1) AppDomain.CurrentDomain.BaseDirectory 地址为: d:\MyProject\Bin\  Application. ...

  9. APM 终端用户体验监控分析(上)

    一.前言 理解用户体验是从终端用户角度了解应用交付质量的关键,这是考量业务健康运转的潜在因素.捕获此类数据的方法各种各样,具体的实现途径由应用.基础设施架构以及管理者和管理过程决定. 二.终端用户监控 ...

  10. poj 2311

    Cutting Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2844   Accepted: 1036 Desc ...