poj 2112(二分+网络流)
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 15749 | Accepted: 5617 | |
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
题意:有K台挤奶机,C头奶牛,每台挤奶机可以容纳M头奶牛,挤奶机和奶牛两两之间都有个距离,现在问在保证所有的奶牛都可以产奶的情况下,走到挤奶机需要走最远的奶牛的最短要走的距离是多少?
题解:先用floyed算法算出每头奶牛和挤奶机之间的最短路径,在保证所有奶牛都能够产奶的情况下二分求解,设立超级源点S,S向每台挤奶机之间连容量为M的边,每台挤奶机向奶牛连容量为1的边,所有奶牛
向超级汇点连容量为1的边,求解最大流。
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <string.h>
#include <math.h>
#include <iostream>
#include <math.h>
using namespace std;
const int N = ;
const int INF = ;
struct Edge
{
int v,next;
int w;
} edge[N*N];
int head[N];
int level[N];
int tot;
void init()
{
memset(head,-,sizeof(head));
tot=;
}
void addEdge(int u,int v,int w,int &k)
{
edge[k].v = v,edge[k].w=w,edge[k].next=head[u],head[u]=k++;
edge[k].v = u,edge[k].w=,edge[k].next=head[v],head[v]=k++;
}
int BFS(int src,int des)
{
queue<int >q;
memset(level,,sizeof(level));
level[src]=;
q.push(src);
while(!q.empty())
{
int u = q.front();
q.pop();
if(u==des) return ;
for(int k = head[u]; k!=-; k=edge[k].next)
{
int v = edge[k].v;
int w = edge[k].w;
if(level[v]==&&w!=)
{
level[v]=level[u]+;
q.push(v);
}
}
}
return -;
}
int dfs(int u,int des,int increaseRoad)
{
if(u==des||increaseRoad==) return increaseRoad;
int ret=;
for(int k=head[u]; k!=-; k=edge[k].next)
{
int v = edge[k].v,w=edge[k].w;
if(level[v]==level[u]+&&w!=)
{
int MIN = min(increaseRoad-ret,w);
w = dfs(v,des,MIN);
if(w > )
{
edge[k].w -=w;
edge[k^].w+=w;
ret+=w;
if(ret==increaseRoad) return ret;
}
else level[v] = -;
if(increaseRoad==) break;
}
}
if(ret==) level[u]=-;
return ret;
}
int Dinic(int src,int des)
{
int ans = ;
while(BFS(src,des)!=-) ans+=dfs(src,des,INF);
return ans;
}
int graph[N][N];
int k,c,m;
int floyed(int n)
{
int MAX=-;
for(int k=; k<=n; k++)
{
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
graph[i][j] = min(graph[i][j],graph[i][k]+graph[k][j]);
}
}
}
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(graph[i][j]!=INF)
MAX = max(MAX,graph[i][j]);
}
}
return MAX;
}
int build(int v){
init();
int src = ,des = k+c+;
for(int i=;i<=k;i++) addEdge(src,i,m,tot);
for(int i=k+;i<=k+c;i++) addEdge(i,des,,tot);
for(int i=;i<=k;i++){
for(int j=k+;j<=k+c;j++){
if(graph[i][j]<=v) addEdge(i,j,,tot);
}
}
return Dinic(src,des);
}
int main()
{
while(scanf("%d%d%d",&k,&c,&m)!=EOF)
{
for(int i=;i<=k+c;i++){
for(int j=;j<=k+c;j++){
scanf("%d",&graph[i][j]);
if(graph[i][j]==&&i!=j) graph[i][j] = INF;
}
}
int MAX = floyed(k+c);
int l=,r = MAX;
int ans = MAX;
while(l<=r){
int mid = (l+r)>>;
if(build(mid)==c) {
ans = mid;
r = mid-;
}
else l =mid+;
}
printf("%d\n",ans);
}
}
poj 2112(二分+网络流)的更多相关文章
- poj 2112(二分+多重匹配)
题目链接:http://poj.org/problem?id=2112 思路:由于要求奶牛走的最远距离的最短路程,显然我们可以二分距离,如果奶牛与挤奶器的距离小于等于limit的情况下,能够满足,则在 ...
- POJ 2112 二分+最大流
Optimal Milking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 17297 Accepted: 6203 ...
- POJ 2455 二分+网络流
题意: 思路: 莫名其妙TLE 啊woc我A了一坨题的网络流模板有问题 !!!! 在常数上会慢 (一个等于号 啊啊啊) 改了所有网络流有关的文章- .... //By SiriusRen #inclu ...
- POJ 2112 Optimal Milking (二分+最短路径+网络流)
POJ 2112 Optimal Milking (二分+最短路径+网络流) Optimal Milking Time Limit: 2000MS Memory Limit: 30000K To ...
- 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 (二分 + 最大流)
题目大意: 在一个农场里面,有k个挤奶机,编号分别是 1..k,有c头奶牛,编号分别是k+1 .. k+c,每个挤奶机一天最让可以挤m头奶牛的奶,奶牛和挤奶机之间用邻接矩阵给出距离.求让所有奶牛都挤到 ...
- POJ 2112—— Optimal Milking——————【多重匹配、二分枚举答案、floyd预处理】
Optimal Milking Time Limit:2000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u Sub ...
- Poj 2112 Optimal Milking (多重匹配+传递闭包+二分)
题目链接: Poj 2112 Optimal Milking 题目描述: 有k个挤奶机,c头牛,每台挤奶机每天最多可以给m头奶牛挤奶.挤奶机编号从1到k,奶牛编号从k+1到k+c,给出(k+c)*(k ...
- POJ 2455 Secret Milking Machine(搜索-二分,网络流-最大流)
Secret Milking Machine Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9658 Accepted: ...
随机推荐
- python入门:输出1-100之内的所有奇数和偶数
#!/usr/bin/env python # -*- coding:utf-8 -*- #输出1-100之内的所有奇数和偶数 """ 给start赋值等于1,while ...
- paper:synthesizable finit state machine design techniques using the new systemverilog 3.0 enhancements之onehot coding styles(encoded-parameter style with registered outputs不推荐但是经常有人写这样的代码)
这样写法,不利与综合,case语句中比较也是full-vector比较.
- Linux 常用命令(三)
一.less --分页查看文件:方面查阅(编辑)大文件 说明:支持方向键盘和鼠标向上向下浏览 -N 显示行号 二.head --output the first part of files 默认显示 ...
- 树莓派编译ncnn
1.从github上下载ncnn git clone --recursive https://github.com/Tencent/ncnn 2.在ncnn根目录下创建build目录,安装cmake编 ...
- Python9-进程-day36
import osfrom multiprocessing import Processimport timedef func(args,args2): print(args,args2) time. ...
- Linux学习-SELinux 初探
什么是 SELinux 什么是 SELinux 呢?其实他是『 Security Enhanced Linux 』的缩写,字面上的意义就是安全强化的 Linux 之意! 当初设计的目标:避免资源的误用 ...
- TCP缓冲区大小及限制
这个问题在前面有的部分已经涉及,这里在重新总结下.主要参考UNIX网络编程. (1)数据报大小IPv4的数据报最大大小是65535字节,包括IPv4首部.因为首部中说明大小的字段为16位.IPv6的数 ...
- 《Scrum实战》第1课【知易行难】全团课后任务汇总
1组 孟帅(班长) kecyru 2017-7-5 http://kecyru.blog.163.com/blog/static/27416617320176411513013 htt ...
- MongoDB学习-->Spring Data Mongodb框架之Repository
application-dev.yml server: port: 8888 mongo: host: localhost port: 27017 timeout: 60000 db: mamabik ...
- MyInt的简单实现
#include <iostream> using namespace std; class CMyInt{ private: int value; public: CMyInt(int ...