POJ1274 The Perfect Stall 二分图,匈牙利算法
N头牛,M个畜栏,每头牛仅仅喜欢当中的某几个畜栏,可是一个畜栏仅仅能有一仅仅牛拥有,问最多能够有多少仅仅牛拥有畜栏。
典型的指派型问题,用二分图匹配来做,求最大二分图匹配能够用最大流算法,也能够用匈牙利算法,这里使用匈牙利算法。
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <math.h>
#include <string.h>
#include <string>
#include <iostream>
#include <queue>
#include <list>
#include <algorithm>
#include <stack>
#include <map> #include<iostream>
#include<cstdio>
using namespace std; #define MAXN 401 bool Visited[MAXN];
int Result[MAXN];
int G[MAXN][MAXN]; bool dfs(int s, int n, int m)
{ for (int i = n + 1; i <= n + m;i++)
{
if (G[s][i] && Visited[i] == false)
{
Visited[i] = true;
if (Result[i] == 0 ||dfs(Result[i], n, m))
{
Result[i] = s;
return true;
}
}
}
return false;
} int main()
{
#ifdef _DEBUG
freopen("d:\\in.txt", "r", stdin);
#endif
int n, m;
while (scanf("%d %d", &n, &m) != EOF)
{
memset(G, 0, sizeof(G));
memset(Result, 0, sizeof(Result));
for (int i = 1; i <= n;i++)
{
int k;
scanf("%d", &k);
for (int j = 0; j < k; j++)
{
int d;
scanf("%d", &d);
G[i][n + d] = 1;
} }
int max = 0;
for (int s = 1; s <= n; s++)
{
memset(Visited, 0, sizeof(Visited));
if (dfs(s, n, m))
{
max++;
} }
printf("%d\n", max);
}
return 0;
}
POJ1274 The Perfect Stall 二分图,匈牙利算法的更多相关文章
- poj 1274 The Perfect Stall【匈牙利算法模板题】
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20874 Accepted: 942 ...
- POJ1274 The Perfect Stall[二分图最大匹配]
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23911 Accepted: 106 ...
- POJ1274 The Perfect Stall[二分图最大匹配 Hungary]【学习笔记】
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23911 Accepted: 106 ...
- hiho1122_二分图匈牙利算法
题目 给定一个图的N个节点和节点之间的M条边,数据保证该图可以构成一个二分图.求该二分图最大匹配. 题目链接:二分图最大匹配 首先通过染色法,将图的N个节点分成两个部分:然后通过匈牙利算法求二 ...
- POJ1274 The Perfect Stall【二部图最大匹配】
主题链接: id=1274">http://poj.org/problem? id=1274 题目大意: 有N头奶牛(编号1~N)和M个牛棚(编号1~M). 每头牛仅仅可产一次奶.每一 ...
- POJ1274 The Perfect Stall
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25739 Accepted: 114 ...
- hdu-1150(二分图+匈牙利算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1150 思路:题目中给出两个机器A,B:给出k个任务,每个任务可以由A的x状态或者B的y状态来完成. 完 ...
- [SinGuLaRiTy] 二分图&匈牙利算法
[SinGuLaRiTY-1019] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 二分图 二分图是图论中一种特殊的图形.顾名思义,二分图G ...
- POJ 3020 Antenna Placement(二分图 匈牙利算法)
题目网址: http://poj.org/problem?id=3020 题意: 用椭圆形去覆盖给出所有环(即图上的小圆点),有两种类型的椭圆形,左右朝向和上下朝向的,一个椭圆形最多可以覆盖相邻的两 ...
随机推荐
- Unity3d 刚体
using UnityEngine; using System.Collections; public class rigidbody_ : MonoBehaviour { private Rigid ...
- 第4章 部署模式 Three-Tiered Distribution(三级分布)
影响因素 Tiered Distribution 中讨论的影响因素也适用于此模式.有关这些通用影响因素的讨论,请参阅"Tiered Distribution".下列影响因素仅适用于 ...
- BZOJ4518: [Sdoi2016]征途(dp+斜率优化)
Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 1875 Solved: 1045[Submit][Status][Discuss] Descript ...
- WordPress瀑布流主题PinThis中文版v1.6.8
PinThis主题来源于英语网站http://pinthis.pixelbeautify.com/的汉化(语言文件+控制面板),中文版采用的是翻译器手工核对,并不完美,只对主题中文化,其他没做任何更改 ...
- Web移动端常见问题
一.按钮点击时出现黑色背景 解决方法: .class { -webkit-tap-highlight-color:rgba(0,0,0,0);} .class { -webkit-appearance ...
- (转)基于MVC4+EasyUI的Web开发框架形成之旅--附件上传组件uploadify的使用
http://www.cnblogs.com/wuhuacong/p/3343967.html 大概一年前,我还在用Asp.NET开发一些行业管理系统的时候,就曾经使用这个组件作为文件的上传操作,在随 ...
- What's Dead & Exploded in Swift's exception stack?
The Swift compiler marks function arguments for a number of reasons, mostly related to internal opti ...
- Java中数组遍历
就是将数组中的每个元素分别获取出来,就是遍历.遍历也是数组操作中的基石. 数组的索引是 0 到 lenght-1 ,可以作为循环的条件出现 public class ArrayDemo4 { publ ...
- Visual Studio 2015中 安卓环境 cannot find adb.exe in specified sdk path
安装完成后 发现 C:\Program Files (x86)\Android\android-sdk\platforms 是空的,用SDK Manager进行安装时发现 它需要往C:\Program ...
- 在fedora「27」下,安装mysql 问题总结
有时会出现,没有mysql.sock,不存在的问题, Can't connect to local MySQL server through socket '/var/lib/mysql/mysql. ...