LA 3268 号码簿分组(最大流+二分)
https://vjudge.net/problem/UVALive-3268
题意:
有n个人和m个组。一个人可能属于很多组。现在请你从某些组中去掉几个人,使得每个人只属于一个组,并使得人数最多的组中人员数目为最小值。
思路:
建立超级源汇点,源点和每个人相连,容量为1,说明每个人最多只能在一个组中。每个人和可以属于的组相连,容量为1。接下来枚举组的最大容量值,将每组和汇点相连,容量为枚举值,二分跑最大流即可。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn = + ; vector<int> g[maxn]; struct Edge
{
int from,to,cap,flow;
Edge(int u,int v,int w,int f):from(u),to(v),cap(w),flow(f){}
}; struct Dinic
{
int n,m,s,t;
vector<Edge> edges;
vector<int> G[maxn];
bool vis[maxn];
int cur[maxn];
int d[maxn]; void init(int n)
{
this->n=n;
for(int i=;i<n;++i) G[i].clear();
edges.clear();
} void AddEdge(int from,int to,int cap)
{
edges.push_back( Edge(from,to,cap,) );
edges.push_back( Edge(to,from,,) );
m=edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
} bool BFS()
{
queue<int> Q;
memset(vis,,sizeof(vis));
vis[s]=true;
d[s]=;
Q.push(s);
while(!Q.empty())
{
int x=Q.front(); Q.pop();
for(int i=;i<G[x].size();++i)
{
Edge& e=edges[G[x][i]];
if(!vis[e.to] && e.cap>e.flow)
{
vis[e.to]=true;
d[e.to]=d[x]+;
Q.push(e.to);
}
}
}
return vis[t];
} int DFS(int x,int a)
{
if(x==t || a==) return a;
int flow=, f;
for(int &i=cur[x];i<G[x].size();++i)
{
Edge &e=edges[G[x][i]];
if(d[e.to]==d[x]+ && (f=DFS(e.to,min(a,e.cap-e.flow) ) )>)
{
e.flow +=f;
edges[G[x][i]^].flow -=f;
flow +=f;
a -=f;
if(a==) break;
}
}
return flow;
} int Maxflow(int s,int t)
{
this->s=s; this->t=t;
int flow=;
while(BFS())
{
memset(cur,,sizeof(cur));
flow +=DFS(s,INF);
}
return flow;
}
}DC; int n, m;
int src, dst; bool solve(int x)
{
DC.init(dst+);
for(int i=;i<=n;i++)
{
DC.AddEdge(src,i,);
for(int j=;j<g[i].size();j++)
{
DC.AddEdge(i,g[i][j]+n+,);
}
}
for(int i=;i<=m;i++)
DC.AddEdge(i+n,dst,x); if(DC.Maxflow(src,dst)==n) return true;
else return false;
} int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d%d",&n,&m) && (n+m))
{
for(int i=;i<=n;i++) g[i].clear();
src=, dst=n+m+; for(int i=;i<=n;i++)
{
char name[]; char c; int x; cin>>name;
while(~scanf("%d%c",&x,&c))
{
g[i].push_back(x);
if(c=='\n') break;
}
} int ans=;
int L=,R=n;
while(L<=R)
{
int mid=(L+R)/;
if(solve(mid)) {ans=mid;R=mid-;}
else L=mid+;
}
printf("%d\n",ans);
}
return ;
}
LA 3268 号码簿分组(最大流+二分)的更多相关文章
- HDU 3081 Marriage Match II (网络流,最大流,二分,并查集)
HDU 3081 Marriage Match II (网络流,最大流,二分,并查集) Description Presumably, you all have known the question ...
- Risk UVA - 12264 拆点法+最大流+二分 最少流量的节点流量尽量多。
/** 题目:Risk UVA - 12264 链接:https://vjudge.net/problem/UVA-12264 题意:给n个点的无权无向图(n<=100),每个点有一个非负数ai ...
- POJ2289 Jamie's Contact Groups —— 二分图多重匹配/最大流 + 二分
题目链接:https://vjudge.net/problem/POJ-2289 Jamie's Contact Groups Time Limit: 7000MS Memory Limit: 6 ...
- BZOJ-3130 费用流 (听题目胡扯丶裸最大流) 二分判定+最大流+实数精度乱搞
DCrusher爷喜欢A我做的水题,没办法,只能A他做不动的题了.... 3130: [Sdoi2013]费用流 Time Limit: 10 Sec Memory Limit: 128 MBSec ...
- BZOJ-1305 dance跳舞 建图+最大流+二分判定
跟随YveH的脚步又做了道网络流...%%% 1305: [CQOI2009]dance跳舞 Time Limit: 5 Sec Memory Limit: 162 MB Submit: 2119 S ...
- poj 2112 floyd+Dinic最大流+二分最小值
题目大意是: K台挤奶机器,C头牛,K不超过30,C不超过200,每台挤奶机器最多可以为M台牛工作,给出这些牛和机器之间,牛和牛之间,机器与机器之间的距离,在保证让最多的牛都有机器挤奶的情况下,给出其 ...
- hdu 3228 (最大流+二分)
题意:一共有N个城市,一些城市里有金矿,一些城市里有仓库,金矿和仓库都有一个容量,有M条边,每条边是双向的,有一个权值,求将所有金矿里的储量都运送到仓库中,所需要经过的道路中,使最大的权值最小 思路: ...
- BZOJ2406矩阵——有上下界的可行流+二分答案
题目描述 输入 第一行两个数n.m,表示矩阵的大小. 接下来n行,每行m列,描述矩阵A. 最后一行两个数L,R. 输出 第一行,输出最小的答案: 样例输入 2 2 0 1 2 1 0 1 样例输出 1 ...
- bzoj 3597 [Scoi2014] 方伯伯运椰子 - 费用流 - 二分答案
题目传送门 传送门 题目大意 给定一个费用流,每条边有一个初始流量$c_i$和单位流量费用$d_i$,增加一条边的1单位的流量需要花费$b_i$的代价而减少一条边的1单位的流量需要花费$a_i$的代价 ...
随机推荐
- 配置项目使用weblogic的JNDI数据源
项目结构构:Spring Framework + SpirngMVC + SpringData JPA + Maven 1.在weblogic的控制台界面中配置数据源(假定我们配置的数据源的名称为da ...
- EUI组件之HScrollBar VScrollBar (动态设置滑块图片)
一.常规使用 官网教程里没有这个组件的使用方法 这个组件配合Scroller使用 拖动一个scroller到exml上.scroller上已经默认存在了HScrollBar和VScrollBar 当图 ...
- ShowDoc 搭建 (未成功....)
官方教程:https://www.showdoc.cc/help?page_id=13732 下载了showdoc,服务器映射本地磁盘的时候,服务器用户名和密码忘了... 远程服务器用户名和密码修改 ...
- eclipse导入maven-jeecg项目
参考内容:http://blog.csdn.net/zhangdaiscott/article/details/50915206 [技术文档]jeecg3.7.1-maven搭建环境入门 由于ma ...
- zeptojs使用及介绍,手机端为什么一定要用zeptojs?
zepto是和jQuery和类似的框架,由于jQuery内置了很多解决浏览器兼容性的代码,导致jQuery过于臃肿,对于移动端来说,不需要太过于考虑浏览器的兼容,所以就诞生zeptojs. 总的来说: ...
- python安装whl文件的注意事项(windows系统)
首先给大家来一波福利,在没有连接外网(互联网)的情况下,只有公司内网或者断网情况下,需要安装python的一些依赖,不会操作的同学可能就会遇到麻烦.这里教大家离线安装python依赖. 方法:使用.w ...
- Python开发【模块】:logging日志
logging模块 很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式 ...
- Day19 客户关系系统实战
day19 今日内容 Service事务 客户关系管理系统 Service事务 在Service中使用ThreadLocal来完成事务,为将来学习Spring事务打基础! 1 DAO中的事务 ...
- python+Nginx+uWSGI使用说明
安装环境 Remote: CentOS 7.4 x64 (django.example.com) Python: Python3.6.5 Django: Django 2.0.4 nWSGI: uw ...
- eclipse+maven+tomcat构建web工程
我们要利用Maven构建一个web应用,开发环境为eclipse+tomcat.构建过程如下: 1.工具准备 eclipse:版本为eclipse 4.2(Juno Service),maven插件的 ...