TZOJ 1594 Optimal Milking(二分+最大流)
描述
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.
输入
* 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.
输出
A single line with a single integer that is the minimum possible total distance for the furthest walking cow.
样例输入
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
样例输出
2
题意
给你(K+C)*(K+C)的图,K个牛奶机,每个牛奶机最多供M头牛,一共C头牛,问所有方案中使得距离牛奶机器最远的牛的距离最小
题解
先把牛奶机连汇点T流量M,牛连源点S流量1,牛和牛奶机连流量1,如果C牛都能有饮料机,则说明汇点T=C
然后是怎么连牛和牛奶机的问题,可以知道答案求的是最大值最小
直接二分答案[0,200*(K+C)]
每次把距离<=mid的边加进去,如果T=C,则说明可行,r=mid
否则l=mid
代码
#include<bits/stdc++.h>
using namespace std; const int maxn=1e5+;
const int maxm=2e5+;
int n,m,S,T;
int deep[maxn],q[];
int FIR[maxn],TO[maxm],CAP[maxm],COST[maxm],NEXT[maxm],tote; void add(int u,int v,int cap)
{
TO[tote]=v;
CAP[tote]=cap;
NEXT[tote]=FIR[u];
FIR[u]=tote++; TO[tote]=u;
CAP[tote]=;
NEXT[tote]=FIR[v];
FIR[v]=tote++;
}
bool bfs()
{
memset(deep,,sizeof deep);
deep[S]=;q[]=S;
int head=,tail=;
while(head!=tail)
{
int u=q[++head];
for(int v=FIR[u];v!=-;v=NEXT[v])
{
if(CAP[v]&&!deep[TO[v]])
{
deep[TO[v]]=deep[u]+;
q[++tail]=TO[v];
}
}
}
return deep[T];
}
int dfs(int u,int fl)
{
if(u==T)return fl;
int f=;
for(int v=FIR[u];v!=-&&fl;v=NEXT[v])
{
if(CAP[v]&&deep[TO[v]]==deep[u]+)
{
int Min=dfs(TO[v],min(fl,CAP[v]));
CAP[v]-=Min;CAP[v^]+=Min;
fl-=Min;f+=Min;
}
}
if(!f)deep[u]=-;
return f;
}
int maxflow()
{
int ans=;
while(bfs())
ans+=dfs(S,<<);
return ans;
}
void init()
{
tote=;
memset(FIR,-,sizeof FIR);
}
int K,C,N,M,a[][];
int main()
{
cin>>K>>C>>M;
N=K+C;
for(int i=;i<=N;i++)
for(int j=;j<=N;j++)
{
scanf("%d",&a[i][j]);
if(i!=j&&!a[i][j])a[i][j]=0x3f3f3f3f;
}
for(int k=;k<=N;k++)
for(int i=;i<=N;i++)
for(int j=;j<=N;j++)
if(a[i][j]>a[i][k]+a[k][j])
a[i][j]=a[i][k]+a[k][j];
int l=,r=*N;
S=,T=K+C+;
while(r-l>)
{
int mid=(l+r)>>;
init();
for(int i=;i<=K;i++)
add(S,i,M);
for(int i=K+;i<=N;i++)
add(i,T,);
for(int i=;i<=K;i++)
for(int j=K+;j<=N;j++)
if(a[i][j]&&a[i][j]<=mid)
add(i,j,a[i][j]);
int sum=maxflow();
if(sum==C)r=mid;
else l=mid;
}
printf("%d\n",r);
return ;
}
TZOJ 1594 Optimal Milking(二分+最大流)的更多相关文章
- POJ 2112 Optimal Milking (二分 + 最大流)
题目大意: 在一个农场里面,有k个挤奶机,编号分别是 1..k,有c头奶牛,编号分别是k+1 .. k+c,每个挤奶机一天最让可以挤m头奶牛的奶,奶牛和挤奶机之间用邻接矩阵给出距离.求让所有奶牛都挤到 ...
- POJ2112 Optimal Milking 【最大流+二分】
Optimal Milking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 12482 Accepted: 4508 ...
- POJ 2112 Optimal Milking (二分+最短路+最大流)
<题目链接> 题目大意: 有K台挤奶机和C头奶牛,都被视为物体,这K+C个物体之间存在路径.给出一个 (K+C)x(K+C) 的矩阵A,A[i][j]表示物体i和物体j之间的距离,有些物体 ...
- POJ 2112 Optimal Milking ( 经典最大流 && Floyd && 二分 )
题意 : 有 K 台挤奶机器,每台机器可以接受 M 头牛进行挤奶作业,总共有 C 头奶牛,机器编号为 1~K,奶牛编号为 K+1 ~ K+C ,然后给出奶牛和机器之间的距离矩阵,要求求出使得每头牛都能 ...
- POJ-2112 Optimal Milking(floyd+最大流+二分)
题目大意: 有k个挤奶器,在牧场里有c头奶牛,每个挤奶器可以满足m个奶牛,奶牛和挤奶器都可以看成是实体,现在给出两个实体之间的距离,如果没有路径相连,则为0,现在问你在所有方案里面,这c头奶牛需要走的 ...
- POJ 2112 Optimal Milking(最大流)
题目链接:http://poj.org/problem?id=2112 Description FJ has moved his K (1 <= K <= 30) milking mach ...
- POJ 2112.Optimal Milking (最大流)
时间限制:2s 空间限制:30M 题意: 有K台挤奶机(编号1~K),C头奶牛(编号K+1~K+C),给出各点之间距离.现在要让C头奶牛到挤奶机去挤奶,每台挤奶机只能处理M头奶牛,求使所走路程最远的奶 ...
- POJ2112 Optimal Milking(最大流)
先Floyd求牛到机器最短距离,然后二分枚举最长的边. #include<cstdio> #include<cstring> #include<queue> #in ...
- poj2112Optimal Milking(二分+最大流)
链接 floyd求出牛到机器的最短距离,二分距离,小于当前距离的边容量设为1,求出满容量下的最短距离. EK算法 #include <iostream> #include<cstdi ...
随机推荐
- vue 监听state 任意值变化、监听mutations actions
// store.watch((state) => state.count + 1, (newCount) => { // console.log(' 监听') // }) // stor ...
- SSL证书没有绿锁您与此网站建立的连接并非完全安全解决办法
为什么我新建的网站配置好SSL后,网站https旁边提示不安全,没有小绿锁了? 不少国内空间的新手站长,当使用了SSL证书之后,发现浏览器有https效果了,但是没有绿锁,谷歌浏览器提示“您与此网站建 ...
- ReactiveX 学习笔记(8)错误处理和 To 操作符
Error Handling Operators Operators to Convert Observables 本文的主题为对 Observable 进行错误处理的操作符以及转换 Observab ...
- Haskell语言学习笔记(85)Async
安装 async $ cabal install async async-2.2.1 installed async / wait / concurrently async :: IO a -> ...
- RabbitMQ.Net 应用(2)
//生产者 using RabbitMQ.Client; using System; using System.Collections.Generic; using System.Linq; usin ...
- ARP工作过程、ARP欺骗的原理和现象、如何防范ARP欺骗
地址解析协议(Address Resolution Protocol,ARP)是在仅知道主机的IP地址时确定其物理地址的一种协议. 下面假设在一个局域网内,主机A要向主机B发送IP数据报. ARP ...
- SQL Server 优化---为什么索引视图(物化视图)需要with(noexpand)强制查询提示
本文出处:http://www.cnblogs.com/wy123/p/6694933.html 第一次通过索引视图优化SQL语句,以及遇到的一些问题,记录一下. 语句分析 最近开发递交过来一个查询统 ...
- Android sdk测试方法链接
https://blog.csdn.net/u013059441/article/details/79030998?utm_source=blogxgwz0
- 吴裕雄 python神经网络 水果图片识别(4)
# coding: utf-8 # In[1]:import osimport numpy as npfrom skimage import color, data, transform, io # ...
- C++读取txt和保存到txt
哇,今天又重新用C++来写了一些代码发现自己竟然在类的使用和文件读取和保存上面特别头疼,于是,各种问度娘+各种翻看之前的代码.不禁感叹,自己的代码还是写的太少了,对这些一点都不熟悉.于是,今晚!一定! ...