这题关键就在将题转换成最大流模板题。首先有一个原始点,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. Ubuntu 下编译安装linux

    1. 准备工作切换为管理员权限,sudo –i 输入用户密码 进入root 权限apt-get install build-essential kernel-package libncurses5-d ...

  2. Dynamic Programming - Part1

    public static void main(String[] args) { //give N, find the number of different ways to write N as t ...

  3. Leetcode#59 Spiral Matrix II

    原题地址 相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些. 注意当n是奇数的时候,中心小块要单独赋值(代码21行) 代码: vector<vector& ...

  4. POI中设置Excel单元格格式

    引用:http://apps.hi.baidu.com/share/detail/17249059 POI中可能会用到一些需要设置EXCEL单元格格式的操作小结: 先获取工作薄对象: HSSFWork ...

  5. SQL Server中存储过程 比 直接运行SQL语句慢的原因

    问题是存储过程的Parameter sniffing     在很多的资料中都描述说SQLSERVER的存储过程较普通的SQL语句有以下优点: 1. 存储过程只在创造时进行编译即可,以后每次执行存储过 ...

  6. 跨站点端口攻击 – XSPA(SSPA)

    许多Web应用程序提供的功能将数据从其他Web服务器,由于种种原因.下载XML提要,从远程服务器,Web应用程序可以使用用户指定的URL,获取图像,此功能可能会被滥用,使制作的查询使用易受攻击的Web ...

  7. Intent.ACTION广播大全

    Intent.ACTION广播大全 Intent.ACTION_AIRPLANE_MODE_CHANGED; //关闭或打开飞行模式时的广播 Intent.ACTION_BATTERY_CHANGED ...

  8. HDOJ 1709 The Balance(母函数)

    The Balance Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. HTML5 canvas 绘图步骤

    1.先把canvas选出来,不选出来你往哪儿画! var oCan=  document.getElementById('xxx'); 2.声明基于 canvas 的context对象,没他你怎么调用 ...

  10. java基础知识回顾之javaIO类---BufferedReader和BufferedWriter

    使用了装饰设计模式:此类的设计是为了提高流操作数据的效率.思想就是定义容器将数据进行临时存储,对于缓冲区对象,其实就是将这个容器进行了分装,并提供了更高效的操作方法. BufferReader: pa ...