1669 DINIC+二分
题意:
给你一些名单,和每个名单可以放在哪个分组里,现在要求你把所有的人都分到一个他属于的分组(之一),然后问你分组中最多的那个人数最少是多少...
思路:
二分最多的最少,然后用最大流去判断分组是否合理,建图如下
s 到 所有人连一条边 流量是1
所有分组 到 t 连一条边,流量是mid
然后把所有人和自己属于的分组连边,流量1
跑一边最大流,如果答案等于 N(人数) 那么当前二分满足
up = mid - 1 ........
#include<stdio.h>
#include<string.h>
#include<queue> #define N_node 1500 + 50
#define N_edge 2000000 + 10000
#define INF 1000000000
using namespace std; typedef struct
{
int to ,next ,cost;
}STAR; typedef struct
{
int x ,t;
}DEP; typedef struct
{
int a ,b;
}EDGE; STAR E[N_edge];
DEP xin ,tou;
EDGE edge[N_edge];
int list[N_node] ,tot;
int list1[N_node];
int deep[N_node];
char str[1000000]; void add(int a ,int b ,int c)
{
E[++tot].to = b;
E[tot].cost = c;
E[tot].next = list[a];
list[a] = tot; E[++tot].to = a;
E[tot].cost = 0;
E[tot].next = list[b];
list[b] = tot;
} int minn(int x ,int y)
{
return x < y ? x : y;
} bool BFS_deep(int s ,int t ,int n)
{
memset(deep ,255 ,sizeof(deep));
xin.x = s;
xin.t = 0;
deep[s] = 0;
queue<DEP>q;
q.push(xin);
while(!q.empty())
{
tou = q.front();
q.pop();
for(int k = list[tou.x] ;k ;k = E[k].next)
{
xin.x = E[k].to;
xin.t = tou.t + 1;
if(!E[k].cost || deep[xin.x] != -1)
continue;
deep[xin.x] = xin.t;
q.push(xin);
}
}
for(int i = 0 ;i <= n ;i ++)
list1[i] = list[i];
return deep[t] != -1;
} int DFS_flow(int s ,int t ,int flow)
{
if(s == t) return flow;
int nowflow = 0;
for(int k = list1[s] ;k ;k = E[k].next)
{
list1[s] = k;
int to = E[k].to;
int c = E[k].cost;
if(deep[to] != deep[s] + 1 || ! E[k].cost)
continue;
int tmp = DFS_flow(to ,t ,minn(c ,flow - nowflow));
nowflow += tmp;
E[k].cost -= tmp;
E[k^1].cost += tmp;
if(nowflow == flow) break;
}
if(!nowflow) deep[s] = 0;
return nowflow;
} int DINIC(int s ,int t ,int n)
{
int ans = 0;
while(BFS_deep(s ,t ,n))
{
ans += DFS_flow(s ,t ,INF);
}
return ans;
} bool ok(int mid ,int tt ,int n ,int m)
{
memset(list ,0 ,sizeof(list));
tot = 1;
int i;
for(i = 1 ;i <= n ;i ++)
add(0 ,i ,1);
for(i = 1 ;i <= m ;i ++)
add(n + i ,n + m + 1 ,mid);
for(i = 1 ;i <= tt ;i ++)
add(edge[i].a ,edge[i].b + n ,1);
return DINIC(0 ,n + m + 1 ,n + m + 1) == n;
} int main ()
{
int n ,m ,i ,j;
while(~scanf("%d %d" ,&n ,&m) && n + m)
{
getchar();
int tt = 0;
for(i = 1 ;i <= n ;i ++)
{
gets(str);
int l = strlen(str) - 1;
j = 0;
while(str[j] != ' ')
j++;
int num = 0;
for(j++ ;j <= l ;j ++)
{
if(str[j] == ' ')
{
edge[++tt].a = i;
edge[tt].b = num + 1;
num = 0;
continue;
}
num = num * 10 + str[j] - 48;
}
edge[++tt].a = i;
edge[tt].b = num + 1;
} int low ,up ,mid;
low = 0 ,up = n;
int ans;
while(low <= up)
{
mid = (low + up) >> 1;
if(ok(mid ,tt ,n ,m))
{
ans = mid;
up = mid - 1;
}
else
low = mid + 1;
}
printf("%d\n" ,ans);
}
return 0;
}
1669 DINIC+二分的更多相关文章
- POJ 2391 Ombrophobic Bovines (Floyd + Dinic +二分)
Ombrophobic Bovines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11651 Accepted: 2 ...
- POJ--2112--Optimal Milking【Floyd+Dinic+二分答案】
链接:http://poj.org/problem?id=2112 题意:有k个挤奶器.编号1~k,c头牛,编号k+1~k+c,每一个挤奶器最多能给m头牛挤奶,给你一个k+c的邻接矩阵.要求每头牛都能 ...
- hdu 1669(二分+多重匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1669 思路:由于要求minimize the size of the largest group,由此 ...
- poj 2112 floyd+Dinic最大流+二分最小值
题目大意是: K台挤奶机器,C头牛,K不超过30,C不超过200,每台挤奶机器最多可以为M台牛工作,给出这些牛和机器之间,牛和牛之间,机器与机器之间的距离,在保证让最多的牛都有机器挤奶的情况下,给出其 ...
- POJ2112_Optimal Milking(网洛流最大流Dinic+最短路Flody+二分)
解题报告 农场有k个挤奶机和c头牛,每头牛到每一台挤奶机距离不一样,每台挤奶机每天最多挤m头牛的奶. 寻找一个方案,安排每头牛到某一挤奶机挤奶,使得c头牛须要走的全部路程中的最大路程的最小值. 要使每 ...
- POJ 2112 Optimal Milking (Dinic + Floyd + 二分)
Optimal Milking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 19456 Accepted: 6947 ...
- luogu3305/bzoj3130 费用流 (二分答案+dinic)
Bob肯定想挑一个流量最大的边,然后把所有的费用都加给它呗 那Alice就让流量最大的边尽量小呗 那就二分一下答案再dinic呗 #include<bits/stdc++.h> #defi ...
- HDU 1669 Jamie's Contact Groups(多重匹配+二分枚举)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1669 题目大意: 给你各个人可以属于的组,把这些人分组,使这些组中人数最多的组人数最少,并输出这个人数 ...
- Optimal Milking(POJ2112+二分+Dinic)
题目链接:http://poj.org/problem?id=2112 题目: 题意:有k台挤奶机,c头奶牛,每台挤奶机每天最多生产m的奶,给你每个物品到其他物品的距离(除了物品到自己本省的距离为0外 ...
随机推荐
- Visual Studio Code运行Python代码
目录 步骤 参考 用Pycharm开发Python程序是最好的选择,就是有点贵.基于这个背景,我就尝试一下别的IDE,看到很多人在用免费.开源的Visual Studio Code,下面是配置并运行P ...
- uni-app创建项目
下载 HBuilderX 下载地址(https://www.dcloud.io/hbuilderx.html) HBuilderX是通用的前端开发工具,但为uni-app做了特别强化. 创建uni ...
- 虚拟机测试cobbler,网络安装加载最后出现 dracut:/#
1.cobbler的几个重要概念: distro:发行版系统容,我理解为镜像来源,提供了kernel 和 initrd 文件以及repo源 profile:kickstart文件,用于定制系统,定制安 ...
- SQL驱动限制,导致插入失败
insert into TB_IF_ORDERS (DC_CD,JOB_DT,SEQ_NO,ORDER_KEY,ORDER_ID,ORDER_LINE_NUM,COMPANY_CD,CUST_CD,S ...
- 在 .NET 中使用 Flurl 高效处理Http请求
简介 官方介绍,Flurl是一个现代的,流利的,支持异步的,可测试的,可移植的,URL增强和Http客户端组件. Url构建 现在有一个登录的接口,地址如下: https://www.some-api ...
- 在Windows10搭建WebAssembly开发环境
最近研究WebAssembly技术,准备用WebAssembly编译C/C++代码供前端调用.网上看了很多文章,收获很大,现在就遇到的问题做一个记录. 官网关于windows开发环境搭建基本上几句话, ...
- MySQL语法基础
一.通用语法 1.MySQL数据库的SQL语句不区分大小写 2.可以用/**/完成注释 3.常用数据类型 类型 描述 int 整型 double 浮点型 varchar 字符串型 date 日期类型, ...
- P1162_填涂颜色(JAVA语言)(速看!全洛谷最暴力解法!QAQ)
思路:看了看数据n<=30,于是我们可以暴力求解(主要是BFS学的不咋地~2333).枚举每个0的位置,看上下左右四个方向上是否都有1.都有1的话说明被1包围,即在闭合圈的内部,开个数组标记一下 ...
- 攻防世界 reverse 进阶 15-Reversing-x64Elf-100
15.Reversing-x64Elf-100 这题非常简单, 1 signed __int64 __fastcall sub_4006FD(__int64 a1) 2 { 3 signed int ...
- HTML特殊标签
一,HTML特殊标签 二,换行标签 <br>标签用来将内容换行,其在HTML网页上的效果相当于我们平时使用word编辑文档时使用回车换行. 三,分割线 <hr>标签用来在HTM ...