[USACO4.2]完美的牛栏The Perfect Stall
https://www.luogu.org/problemnew/show/P1894
题解:网络流+最大流
/*
*@Author: STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=400+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int s,t,n,m,k,p,l,r,u,v,w,c2;
int ans,cnt,flag,temp,sum;
int dis[N];
struct node{int u,v,c;node(){};node(int form,int to,int cap):u(form),v(to),c(cap){}
};
vector<node>edge;
vector<int> G[N];
void Addedge(int u,int v,int cap){edge.push_back({u,v,cap});edge.push_back({v,u,0});int sz=edge.size();G[u].push_back(sz-2);G[v].push_back(sz-1);
}
bool bfs(int u){memset(dis,-1,sizeof(dis));dis[u]=0;queue<int>q;q.push(u);while(!q.empty()){int u=q.front();q.pop();for(int i=0;i<G[u].size();i++){node e=edge[G[u][i]];//cout<<u<<" "<<e.v<<endl;if(dis[e.v]<0&&e.c>0){dis[e.v]=dis[u]+1;q.push(e.v);}}}return dis[t]>0;
}
int dfs(int u,int flow){if(u==t)return flow;int now;for(int i=0;i<G[u].size();i++){node e=edge[G[u][i]];if(e.c>0&&dis[u]+1==dis[e.v]&&(now=dfs(e.v,min(flow,e.c)))){edge[G[u][i]].c-=now;edge[G[u][i]^1].c+=now;return now;}}return 0;
}
void dinic(){while(bfs(s)){int res=0;while(res=dfs(s,INF)){ans+=res;}}
}
void init(){s=0;t=n+m+1;for(int i=1;i<=t;i++)G[i].clear();edge.clear();ans=0;
}
int main()
{
#ifdef DEBUGfreopen("input.in", "r", stdin);//freopen("output.out", "w", stdout);
#endif//ios::sync_with_stdio(false);//cin.tie(0);//cout.tie(0);//scanf("%d",&t);//int T=0;while(~scanf("%d%d",&n,&m)){init();for(int i=1;i<=n;i++){//scanf("%d%d%d",&u,&v,&w);u=i;scanf("%d",&k);for(int j=1;j<=k;j++){scanf("%d",&v);Addedge(u,v+n,INF);}}for(int i=1;i<=n;i++)Addedge(0,i,1);for(int i=1;i<=m;i++)Addedge(i+n,t,1);dinic();printf("%d\n",ans);}#ifdef DEBUGprintf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif//cout << "Hello world!" << endl;return 0;
}
[USACO4.2]完美的牛栏The Perfect Stall相关推荐
- poj 1274 The Perfect Stall
2019独角兽企业重金招聘Python工程师标准>>> The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total ...
- [ACM_图论] The Perfect Stall 完美的牛栏(匈牙利算法、最大二分匹配)
描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星期,农夫约翰随便地让奶牛们进入牛栏,但是问题很快地显露出来:每头奶牛都只愿意在她们 ...
- [USACO 4.2.2] The Perfect Stall 完美的牛栏
题目链接 匈牙利算法模板题 #include <iostream> #include <cstring> #include <cstdlib> #include & ...
- [USACO 4.2] 完美的牛栏
★★☆ 输入文件:stall4.in 输出文件:stall4.out 简单对比 时间限制:1 s 内存限制:128 MB USACO/stall4(译by Felicia Crazy) ...
- OpenJudge 2810(1543) 完美立方 / Poj 1543 Perfect Cubes
1.链接地址: http://bailian.openjudge.cn/practice/2810/ http://bailian.openjudge.cn/practice/1543/ http:/ ...
- usaco The Perfect Stall(二分匹配模板)
其实二分匹配都是很早之前看的不过又忘了现在再看看比第一次好理解多了. /* ID:jinbo wu TASK:stall4 LANG:C++ */ #include<bits/stdc++.h& ...
- Poj_1274 The Perfect Stall -二分图裸题
题目:给牛找棚,每个棚只能容一只牛,牛在对应的棚才能产奶,问最多能让几只牛产奶. /************************************************ Author :D ...
- POJ1274 The Perfect Stall(二分图)
题意: 一些奶牛只有在特定的围栏中才能产奶,要求合理安排使能产奶的奶牛数达到最大. 要点: 二分图裸题,最近刚学了二分图,看下面的参考博客,写的比较好: 参考博客:匈牙利算法 15479500 Sea ...
- 【Step1】【二分图匹配】poj 1274-The Perfect Stall
题目链接 题目大意 输入数据中,第一行给出n,表示n个奶牛. 接下来n行,每行一个x,xi表示第i头奶牛可以选择x个谷仓中的一个进行匹配.接下来x个数,表示谷仓的编号(1~n之间) 一个谷仓也只能有一 ...
最新文章
- java repaint_java repaint()无效
- React Native Windows 环境搭建(适合有Android 开发基础的同学)
- [20190401]跟踪dbms_lock.sleep调用.txt
- 【读书笔记】代码可为维护性标准(一)
- python教程:封装、多态、反射
- 如何在 Linux 最小化安装中设置互联网
- 和大家探讨一下“虚拟光驱”原理
- servlet里面转发与重定向
- java数据结构图_java总结数据结构和算法
- svn 使用TortoiseSVN server搭建本地SVN服务器
- FTP 编写 3:同时为多个客户端服务(多线程)
- @EnableConfigurationProperties
- 1037. 在霍格沃茨找零钱(20)-PAT乙级真题
- java堆 数据结构 堆_快速堆数据结构
- day08—css布局解决方案之多列布局
- 免费好用的 PDF 编辑器推荐
- 网络操作系统和分布式操作系统的区别
- 为什么很多人愿意去下载社交APP?
- 华为Mate 20 Pro更新EMUI9.1系统,系统流畅度稳步提高
- html5在微信浏览器下调用复制功能
热门文章
- ajax配置要求,AJAX浅谈
- jdbc连接数据scanip_java数据库连接_jdbc
- linux boost 卸载,Ubuntu下boost库的编译安装步骤及卸载方法详解
- 网上选课系统的php代码,网上选课系统(论文+程序)
- java核心api_Java核心API之字符流使用介绍
- python中字符移位加密_1.1 移位密码加密解密python实现
- 【小白学习keras教程】二、基于CIFAR-10数据集训练简单的MLP分类模型
- pytorch 中 Autograd(四)
- kafka监控linux脚本,zabbix监控kafka
- ICLR 2022 | 在注意力中重新思考Softmax,商汤提出cosFormer实现多项SOTA