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: ...
随机推荐
- 如何下载并安装 robomongo 到Ubuntu 系统
官网下载软件,https://robomongo.org/download wget https://download.robomongo.org/1.2.1/linux/robo3t-1.2.1-l ...
- mysql 编程
一.存储函数 相当于php或者js中有返回值的函数 --完成一定“计算”后返回单个的数据值 定义: create function 函数名(parameter p1 value_type, param ...
- 使用laravel框架的eloquent\DB模型连接多个数据库
1.配置.env文件 DB_HOST_TRAILER=127.0.0.1DB_PORT_TRAILER=3306DB_DATABASE_TRAILER=htms_trailerDB_USERNAME_ ...
- Java并发编程的艺术 记录(二)
volatile的应用 volatile的定义如下:Java编程语言允许线程访问共享变量,为了确保共享变量能被准确和一致地更新,线程应该确保通过排他锁单独获得这个变量.Java语言提供了volatil ...
- usb driver编写 (转)
在开头补上LDD3的一句话:如果 USB 驱动没有和另一种处理用户和设备交互的子系统(例如 input, tty, video, 等待)关联, 驱动可使用 USB 主编号为了使用传统的和用户空间之间的 ...
- German Collegiate Programming Contest 2015 计蒜课
// Change of Scenery 1 #include <iostream> #include <cstdio> #include <algorithm> ...
- UVa 1366 DP Martian Mining
网上的题解几乎都是一样的: d(i, j, 0)表示前i行前j列,第(i, j)个格子向左运输能得到的最大值. d(i, j, 1)是第(i, j)个格子向上运输能得到的最大值. 但是有一个很关键的问 ...
- php msql 表单
http://www.cnblogs.com/webers/p/3849707.html
- css 透明度使用
设置元素整体透明度: div{ opacity: 0.5; } 设置背景色透明度 div{ background: rgba(0,0,0,0.5); }
- Mybatis中接口和对应的mapper文件位置配置详解
Mybatis中接口和对应的mapper文件位置配置详解 原链接为:https://blog.csdn.net/fanfanzk1314/article/details/71480954 今天遇到一个 ...