Optimal Milking
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 18083   Accepted: 6460
Case Time Limit: 1000MS

Description

FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among the C (1 <= C <= 200) cows. A set of paths of various lengths runs among the cows and the milking machines. The milking machine locations are named by ID numbers 1..K; the cow locations are named by ID numbers K+1..K+C.

Each milking point can "process" at most M (1 <= M <= 15) cows each day.

Write a program to find an assignment for each cow to some milking machine so that the distance the furthest-walking cow travels is minimized (and, of course, the milking machines are not overutilized). At least one legal assignment is possible for all input data sets. Cows can traverse several paths on the way to their milking machine.

Input

* Line 1: A single line with three space-separated integers: K, C, and M.

* Lines 2.. ...: Each of these K+C lines of K+C space-separated integers describes the distances between pairs of various entities. The input forms a symmetric matrix. Line 2 tells the distances from milking machine 1 to each of the other entities; line 3 tells the distances from machine 2 to each of the other entities, and so on. Distances of entities directly connected by a path are positive integers no larger than 200. Entities not directly connected by a path have a distance of 0. The distance from an entity to itself (i.e., all numbers on the diagonal) is also given as 0. To keep the input lines of reasonable length, when K+C > 15, a row is broken into successive lines of 15 numbers and a potentially shorter line to finish up a row. Each new row begins on its own line.

Output

A single line with a single integer that is the minimum possible total distance for the furthest walking cow.

Sample Input

2 3 2
0 3 2 1 1
3 0 3 2 0
2 3 0 1 0
1 2 1 0 2
1 0 0 2 0

Sample Output

2
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=;
int head[N],dis[N],tot,K,C,M,st,ed,mp[N][N];
struct node
{
int to,next,w;
} e[N*N*];
void add(int u,int v,int w)
{
e[tot].to=v;
e[tot].next=head[u];
e[tot].w=w;
head[u]=tot++;
}
void init()
{
tot=;
memset(head,-,sizeof(head));
}
bool bfs()
{
queue<int>Q;
memset(dis,-,sizeof(dis));
Q.push();
dis[]=;
while(!Q.empty())
{
int u=Q.front();
Q.pop();
for(int i=head[u]; i+; i=e[i].next)
{
int v=e[i].to;
if(dis[v]==-&&e[i].w>)
{
dis[v]=dis[u]+;
Q.push(v);
if(v==K+C+) return true;
}
}
}
return false;
}
int dfs(int s,int low)
{
if(s==ed||low==) return low;
int ans=low,a;
for(int i=head[s]; i+; i=e[i].next)
{
int v=e[i].to;
if(e[i].w>&&dis[v]==dis[s]+&&(a=dfs(v,min(ans,e[i].w))))
{
ans-=a;
e[i].w-=a;
e[i^].w+=a;
if(!ans) return low;
}
}
if(ans==low) dis[s]=-;
return low-ans;
}
void build(int lim)
{
init();
for(int i=; i<=K; ++i) for(int j=K+; j<ed; ++j) if(mp[i][j]<=lim&&mp[i][j])
{
add(i,j,);
add(j,i,);
}
for(int i=; i<=K; ++i) add(,i,M),add(i,,);
for(int i=K+; i<ed; ++i) add(i,ed,),add(ed,i,);
}
bool Ju()
{
int ans=;
while(bfs()) ans+=dfs(,);
if(ans==C) return true;
return false;
}
void Floyd()
{
for(int k=; k<ed; ++k) for(int i=; i<ed; ++i) if(mp[i][k]) for(int j=; j<ed; ++j) if(mp[k][j])
{
if(mp[i][j]==) mp[i][j]=mp[i][k]+mp[k][j];
else if(mp[i][j]>mp[i][k]+mp[k][j]) mp[i][j]=mp[i][k]+mp[k][j];
}
}
int main()
{
while(scanf("%d%d%d",&K,&C,&M)!=EOF)
{
st=;
ed=K+C+;
for(int i=; i<ed; ++i) for(int j=; j<ed; ++j) scanf("%d",&mp[i][j]);
Floyd();
int r=,l=;
while(r!=l)
{
int mid=(r+l)>>;
build(mid);
if(Ju()) r=mid;
else l=mid+;
}
printf("%d\n",(l+r)>>);
}
}

poj2112 网络流+二分答案的更多相关文章

  1. CQOI跳舞(网络流+二分答案)

    题面见 https://www.luogu.org/problemnew/show/P3153 题意简述:有n个男生,n个女生,每一首歌时两位男女配对,然后同一对男女只能跳一场,一个人只会与不喜欢的人 ...

  2. BZOJ 3993 [SDOI2015]星际战争 | 网络流 二分答案

    链接 BZOJ 3993 题解 这道题挺棵的-- 二分答案t,然后源点向武器连t * b[i], 武器向能攻击的敌人连1, 敌人向汇点连a[i],如果最大流等于所有敌人的a[i]之和则可行. #inc ...

  3. BZOJ 2406: 矩阵 [上下界网络流 二分答案]

    2406: 矩阵 题意:自己去看吧,最小化每行每列所有元素与给定矩阵差的和的绝对值中的最大值 又带绝对值又带max不方便直接求 显然可以二分这个最大值 然后判定问题,给定矩阵每行每列的范围和每个元素的 ...

  4. BZOJ 1733: [Usaco2005 feb]Secret Milking Machine 神秘的挤奶机 网络流 + 二分答案

    Description Farmer John is constructing a new milking machine and wishes to keep it secret as long a ...

  5. 洛谷P2402 奶牛隐藏(网络流,二分答案,Floyd)

    洛谷题目传送门 了解网络流和dinic算法请点这里(感谢SYCstudio) 题目 题目背景 这本是一个非常简单的问题,然而奶牛们由于下雨已经非常混乱,无法完成这一计算,于是这个任务就交给了你.(奶牛 ...

  6. 【BZOJ3993】星际战争(网络流,二分答案)

    [BZOJ3993]星际战争(网络流,二分答案) 题面 Description 3333年,在银河系的某星球上,X军团和Y军团正在激烈地作战.在战斗的某一阶段,Y军团一共派遣了N个巨型机器人进攻X军团 ...

  7. 【BZOJ5251】【八省联考2018】劈配(网络流,二分答案)

    [BZOJ5251][八省联考2018]劈配(网络流,二分答案) 题面 洛谷 BZOJ Description 一年一度的综艺节目<中国新代码>又开始了. Zayid从小就梦想成为一名程序 ...

  8. bzoj1305: [CQOI2009]dance跳舞(二分答案+网络流)

    1305: [CQOI2009]dance跳舞 题目:传送门 题解: 一眼网络流基础建模...然后就GG了 二分答案+拆点建边+最大流判断: 把男女生拆为男1,男2,女1,女2 1.男1和男2还有女1 ...

  9. 稳定的奶牛分配 && 二分图多重匹配+二分答案

    题意: 农夫约翰有N(1<=N<=1000)只奶牛,每只奶牛住在B(1<=B<=20)个奶牛棚中的一个.当然,奶牛棚的容量有限.有些奶牛对它现在住的奶牛棚很满意,有些就不太满意 ...

随机推荐

  1. C++操作Kafka使用Protobuf进行跨语言数据交互

    C++操作Kafka使用Protobuf进行跨语言数据交互 Kafka 是一种分布式的,基于发布 / 订阅的消息系统.主要设计目标如下: 以时间复杂度为 O(1) 的方式提供消息持久化能力,即使对 T ...

  2. 吞吐量(TPS)、QPS、并发数、响应时间(RT)

    1. 响应时间(RT)  响应时间是指系统对请求作出响应的时间.直观上看,这个指标与人对软件性能的主观感受是非常一致的,因为它完整地记录了整个计算机系统处理请求的时间.由于一个系统通常会提供许多功能, ...

  3. 7.JUC线程高级-生产消费问题&虚假唤醒

    描述 生产消费问题在java多线程的学习中是经常遇到的问题 ,多个线程共享通一个资源的时候会出现各种多线程中经常出现的各种问题. 实例说明 三个类:售货员Clerk,工厂Factory,消费者Cons ...

  4. angular2相关

    脚手架安装一个项目 1.全局安装angular脚手架 npm install -g @angular/cli 2.初始化一个文件夹 ng new my-angular-demo 3.进入文件夹 cd ...

  5. C++--浅谈开发系统的经验

    最近写了不少类了,从垃圾代码爬坑,虽然还是很垃圾,但是照葫芦画瓢,有几分神韵.在这里总结一下,写类的经验教训. 第一步 分析: 当拿到一个要求时,要先去考虑怎样一个类到底该实现什么样的功能,有什么样的 ...

  6. CF786B Legacy(线段树优化建边)

    模板题CF786B Legacy 先说算法 如果需要有n个点需要建图 给m个需要建边的信息,从单点(或区间内所有点)向一区间所有点连边 如果暴力建图复杂度\(mn^2\) 以单点连向区间为例,在n个点 ...

  7. ubuntu 15.04 的安装遇到的问题及其解决方法

    在Ubuntu15.04 的安装(U盘)中 遇到的问题1:安装后设置电脑从U盘启动,启动失败,屏幕上显示:Failed to load ldlinux.c32 解决方法:当时是参考这篇文章 http: ...

  8. HC32F003C4PA GPIO Output

    1.打开启动文件,找到并跳转至SystemInit函数 void SystemInit(void) { stc_clk_systickcfg_t stcCfg; // TODO load trim f ...

  9. numpy数组的分割与合并

    合并 np.newaxis import numpy as np a=np.array([1,2,3])[:,np.newaxis]#变成列向量 b=np.array([4,5,6])[:,np.ne ...

  10. 【Spark】SparkStreaming和Kafka的整合

    文章目录 Streaming和Kafka整合 概述 使用0.8版本下Receiver DStream接收数据进行消费 步骤 一.启动Kafka集群 二.创建maven工程,导入jar包 三.创建一个k ...