POJ 2112 Optimal Milking (二分+最短路径+网络流)
POJ 2112 Optimal Milking (二分+最短路径+网络流)
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 10176 | Accepted: 3698 | |
Case Time Limit: 1000MS |
Description
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
* 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
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
Source
告诉它们间的路径,问所有牛中,走得最远的那头牛,走了多远?
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstdlib>
using namespace std; const int maxn=300;
const int inf=1<<28;
struct edge{
int u,v,next,f;
edge(int u0=0,int v0=0,int f0=0,int next0=0){
u=u0,v=v0,f=f0,next=next0;
}
}e[maxn*maxn];
int head[maxn*2],visited[maxn*2],path[maxn*2],a[maxn][maxn];
int cnt,from,to,marked,K,C,M; void ini(){
from=0;to=K+C+1;
} void initial(){
cnt=0;marked=1;
for(int i=0;i<=to;i++){
head[i]=-1;
visited[i]=0;
}
} void adde(int u,int v,int f){
e[cnt].u=u,e[cnt].v=v,e[cnt].f=f,e[cnt].next=head[u],head[u]=cnt++;
e[cnt].u=v,e[cnt].v=u,e[cnt].f=0,e[cnt].next=head[v],head[v]=cnt++;
} void input(){
int c0,n=K+C;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
scanf("%d",&c0);
if(c0>0) a[i][j]=c0;
else a[i][j]=inf;
}
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
if(a[i][k]+a[k][j]<a[i][j]) a[i][j]=a[i][k]+a[k][j];
}
} void build(int x){
initial();
for(int i=1;i<=K;i++) adde(from,i,M); //机器 1 - K 编号
for(int i=K+1;i<=K+C;i++) adde(i,to,1); //牛 K+1 - K+C 编号
for(int i=1;i<=K;i++){
for(int j=K+1;j<=K+C;j++){
if(a[i][j]<=x) adde(i,j,1);
}
}
} bool bfs(){
int s=from,d;
queue <int> q;
q.push(s);
marked++;
visited[s]=marked;
while(!q.empty()){
s=q.front();
q.pop();
for(int i=head[s];i!=-1;i=e[i].next){
d=e[i].v;
if(visited[d]!=marked && e[i].f>0){
visited[d]=marked;
path[d]=i;
q.push(d);
if(d==to) return true;
}
}
}
return false;
} int maxf(int x){
build(x);
int maxflow=0;
while(bfs() ){
int offflow=inf;
for(int i=to;i!=from;i=e[path[i]].u){
offflow=min(e[path[i]].f,offflow);
}
for(int i=to;i!=from;i=e[path[i]].u){
e[path[i]].f-=offflow;
e[path[i]^1].f+=offflow;
}
maxflow+=offflow;
}
return maxflow;
} void computing(){
int l=1,r=200000;
while(l<r){
int mid=(l+r)/2;
if(maxf(mid)>=C) r=mid;
else l=mid+1;
}
cout<<r<<endl;
} int main(){
while(cin>>K>>C>>M){
ini();
input();
computing();
}
return 0;
}
POJ 2112 Optimal Milking (二分+最短路径+网络流)的更多相关文章
- POJ 2112 Optimal Milking (二分 + 最大流)
题目大意: 在一个农场里面,有k个挤奶机,编号分别是 1..k,有c头奶牛,编号分别是k+1 .. k+c,每个挤奶机一天最让可以挤m头奶牛的奶,奶牛和挤奶机之间用邻接矩阵给出距离.求让所有奶牛都挤到 ...
- POJ 2112 Optimal Milking (二分+最短路+最大流)
<题目链接> 题目大意: 有K台挤奶机和C头奶牛,都被视为物体,这K+C个物体之间存在路径.给出一个 (K+C)x(K+C) 的矩阵A,A[i][j]表示物体i和物体j之间的距离,有些物体 ...
- POJ 2112 Optimal Milking (二分 + floyd + 网络流)
POJ 2112 Optimal Milking 链接:http://poj.org/problem?id=2112 题意:农场主John 将他的K(1≤K≤30)个挤奶器运到牧场,在那里有C(1≤C ...
- Poj 2112 Optimal Milking (多重匹配+传递闭包+二分)
题目链接: Poj 2112 Optimal Milking 题目描述: 有k个挤奶机,c头牛,每台挤奶机每天最多可以给m头奶牛挤奶.挤奶机编号从1到k,奶牛编号从k+1到k+c,给出(k+c)*(k ...
- POJ 2112—— Optimal Milking——————【多重匹配、二分枚举答案、floyd预处理】
Optimal Milking Time Limit:2000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u Sub ...
- POJ 2112 Optimal Milking (Dinic + Floyd + 二分)
Optimal Milking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 19456 Accepted: 6947 ...
- POJ 2112: Optimal Milking【二分,网络流】
题目大意:K台挤奶机,C个奶牛,每台挤奶器可以供M头牛使用,给出奶牛和和机器间的距离矩阵,求所有奶牛走最大距离的最小值 思路:最大距离的最小值,明显提示二分,将最小距离二分之后问题转化成为:K台挤奶机 ...
- POJ 2112 Optimal Milking【网络流+二分+最短路】
求使所有牛都可以被挤牛奶的条件下牛走的最长距离. Floyd求出两两节点之间的最短路,然后二分距离. 构图: 将每一个milking machine与源点连接,边权为最大值m,每个cow与汇点连接,边 ...
- POJ 2112 Optimal Milking 最短路 二分构图 网络流
题意:有C头奶牛,K个挤奶站,每个挤奶器最多服务M头奶牛,奶牛和奶牛.奶牛和挤奶站.挤奶站和挤奶站之间都存在一定的距离.现在问满足所有的奶牛都能够被挤奶器服务到的情况下,行走距离的最远的奶牛的至少要走 ...
随机推荐
- 写一个背景渐变的TextView输入框
1:在res文件夹下新建一个drawble文件夹,并新建一个背景文件如writebg.xml <?xml version="1.0" encoding="utf-8 ...
- The model used to open the store is incompatible with the one used to create the store
说什么数据不兼容,,,,这时删除模拟器的应用,,,重新启动测试.
- [Android]Dalvik的BOOTCLASSPATH和dexopt流程
BOOTCLASSPATH简介1.BOOTCLASSPATH是Android Linux的一个环境变量,可以在adb shell下用$BOOTCLASSPATH看到.2.BOOTCLASSPATH于/ ...
- Error creating bean with name 'enableRedisKeyspaceNotificationsInitializer'
@Configuration public class HttpSessionConfig { @Bean public static ConfigureRedisAction configureRe ...
- IM-即时通讯技术概述
IM-即时通讯技术概述 简述 即时通讯技术(IM)支持用户在线实时交谈.如果要发送一条信息,用户需要打开一个小窗口,以便让用户及其朋友在其中输入信息并让交谈双方都看到交谈的内容.大多数常用的即时通讯发 ...
- boost::thread用法
最近在做一个消息中间件里面涉及到多线程编程,由于跨平台的原因我采用了boost线程库.在创建线程时遇到了几种线程创建方式现总结如下: 首先看看boost::thread的构造函数吧,boost::th ...
- 关于PHPExcel类占用内存问题
最近在帮一家公司做后台excel导出功能,使用的工具类是phpexcel,因为这个类功能比较强大.全面. 但是遇到下面一个问题: 当导出数据量达到一定数量级的时候,比如说1000条,服务器出现卡顿.白 ...
- SQLite3的使用(用到了dll)good
1.下载sqlite3相关文件sqlite3.dll.sqlite3.h(可从http://download.csdn.net/detail/mingxia_sui/5249070下载),添加到工程的 ...
- Java--日期的使用
Date 类: 最基础的日期时间类,返回一个相对日期的毫秒数.精确到毫秒,但不支持日期的国际化和分时区显示. Calender类: 相对于Date更加强大的时间类,是抽象类,提供了常规的日期修改功能和 ...
- oschina开源硬件其它开源,开源硬件
硬件驱动/工具 25Linux内核 101桌面环境 40开源字体 58嵌入式操作系统 33输入法 110开源硬件 57开源图书 5开源家居 17物联网 87开源货币/比特币 32NASA 开源项目 1 ...