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外 ...
随机推荐
- 小心你的个人信息——GitHub 热点速览 v.21.09
作者:HelloGitHub-小鱼干 浏览过必有痕迹,有什么可以抹去社交痕迹的方法呢?social-analyzer 是一个可在 350+ 网站分析特定用户资料的工具,你可以用它来"人肉&q ...
- 在Arch上使用Fcitx5
目录 卸载Fcitx4 安装Fcitx5 配置 修改环境变量 系统登陆后默认启动Fcitx5输入法 配置主题 最终使用效果 参考文档 我是一个Arch+KDE的用户,所以下面的方法可能不适合所有的Li ...
- PAT-1102(Invert a Binary Tree)+二叉树的镜像+层次遍历+中序遍历+已知树的结构构树
Invert a Binary Tree pat-1102 import java.util.Arrays; import java.util.Queue; import java.util.Scan ...
- 普通的一天,说一个普通的XML
什么是XML XML全称是Extensible Markup Language,译为"可扩展标记语言",常用来存储和传输信息. XML的结构 我们经常看到的XML文件是这个样子的: ...
- Azure Front Door(一)为基于.net core 开发的Azure App Service 提供流量转发
一,引言 之前我们讲解到使用 Azure Traffic Manager.Azure LoadBalancer.Azure Application Gateway,作为项目的负载均衡器来分发流量,转发 ...
- 2020年12月-第01阶段-前端基础-表格 table
表格 table(会使用) 为了让我们页面显示的更加整齐,我们需要学习三个表(表格.表单.列表) 理解: 能说出表格用来做什么的 表格的基本结构组成 表格作用: 存在即是合理的. 表格的现在还是较为常 ...
- 【Arduino学习笔记01】关于Arduino引脚的一些笔记
参考链接:https://www.yiboard.com/thread-831-1-1.html Arduino Uno R3 - 引脚图 Arduino Uno R3 - 详细参数 Arduino ...
- cve-2018-2893 weblogic -WLS核心组件反序列化
漏洞分析 https://www.freebuf.com/column/178103.html https://www.freebuf.com/vuls/177868.html 攻击者可以在未授权的情 ...
- C# 基础 - string 和 Datetime
1. string 1. 格式化填充 string str = "this {0} a {1}"; Console.WriteLine(string.Format(str, &qu ...
- arcgis10.2 的安装与离线发布地图
一.ArcGIS for Desktop安装 ArcGIS安装 方法/步骤1:LicenseManager安装 1.首先要下载Arcgis 10.2软件,很大大约有4个多G.下载后可以用虚拟光驱,DA ...