POJ 2112 Optimal Milking (Dinic + Floyd + 二分)
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 19456 | Accepted: 6947 | |
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 思路:
直接想到了Floyd和二分,可是就做不下去了,因为不知道怎么把二分的值,应用到图里面。没想到我竟然如此地菜呀。
这题要用到网络流。
先跑一个Floyd,求出各点的最短路。然后,二分答案,假如现在的二分值为mid,那么我们建立一个新图,图的边有以下部分(原图的标号是1->k+c):
1.原图(Floyd之后)距离小于mid,并且,起点是奶牛,终点是收奶机的边,每条边权值为1。
2.源点,也是就0号点,到每个奶牛的边,权值为1
3.每个收奶机,到汇点,也就是c+k+1点的边,每条边的权值为m
其中,源点与汇点都不是原图中存在的点。
然后,求最大流,判断最大流是否小于奶牛数。
代码:
#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
int n,k,c,m;
bool vis[300];
int num[300];
bool outflag;
struct node
{
int v;
int ser;
};
int mp[300][300];
const int inf = 99999999;
int mmp[300][300];
void init()
{
scanf("%d%d%d",&k,&c,&m);
n=k+c;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
scanf("%d",&mp[i][j]);
if(i!=j&&mp[i][j]==0){mp[i][j]=inf;}
}
}
} int floyd()
{
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(mp[i][j]>mp[i][k]+mp[k][j]){
mp[i][j]=mp[i][k]+mp[k][j];
}
}
}
}
} void build(int maxn)
{
memset(mmp,0,sizeof(mmp));
for(int i=k+1;i<=n;i++){
mmp[0][i]=1;
}
for(int i=1;i<=k;i++){
mmp[i][n+1]=m;
}
for(int i=k+1;i<=n;i++){
for(int j=1;j<=k;j++){
if(mp[i][j]<=maxn){
mmp[i][j]=1;
}
}
}
} bool bfs(int s,int t)
{
queue<node>q;
memset(vis,0,sizeof(vis));
q.push(node{s,1});
node cur;vis[s]=true;
while(!q.empty()){
cur=q.front();q.pop();
num[cur.v]=cur.ser;
vis[cur.v]=true;
for(int i=0;i<=n+1;i++){
if(mmp[cur.v][i]>0&&!vis[i]){
q.push(node{i,cur.ser+1});
}
}
}
if(num[t]){return true;}
else return false;
} int dfs(int s,int t,int f)
{
if(s==t){return f;}
vis[s]=true;
int d;
for(int i=0;i<=n+1;i++){
if(!vis[i]&&mmp[s][i]>0&&num[s]==num[i]-1){
d=dfs(i,t,min(f,mmp[s][i]));
mmp[s][i]-=d;
mmp[i][s]+=d;
if(d!=0){return d;}
}
}
return 0;
} int dinic()
{
memset(num,0,sizeof(num));
int ans=0;
while(bfs(0,n+1)){
int d;
memset(vis,0,sizeof(vis));
while(d=dfs(0,n+1,inf)){
ans+=d;
memset(vis,0,sizeof(vis));
}
memset(num,0,sizeof(num));
} return ans;
} int solve()
{
int l=0,r=inf,mid;
while(r>=l){
mid=(r+l)>>1;
if(mid==2){outflag=1;}
build(mid); if(dinic()>=c){
r=mid-1;
}
else{
l=mid+1;
}
}
return l;
} int main()
{
init();
floyd();
printf("%d\n",solve());
}
POJ 2112 Optimal Milking (Dinic + Floyd + 二分)的更多相关文章
- POJ 2112 Optimal Milking(Floyd+多重匹配+二分枚举)
题意:有K台挤奶机,C头奶牛,每个挤奶机每天只能为M头奶牛服务,下面给的K+C的矩阵,是形容相互之间的距离,求出来走最远的那头奶牛要走多远 输入数据: 第一行三个数 K, C, M 接下来是 ...
- POJ 2112 Optimal Milking【网络流+二分+最短路】
求使所有牛都可以被挤牛奶的条件下牛走的最长距离. Floyd求出两两节点之间的最短路,然后二分距离. 构图: 将每一个milking machine与源点连接,边权为最大值m,每个cow与汇点连接,边 ...
- POJ 2112 Optimal Milking 最短路 二分构图 网络流
题意:有C头奶牛,K个挤奶站,每个挤奶器最多服务M头奶牛,奶牛和奶牛.奶牛和挤奶站.挤奶站和挤奶站之间都存在一定的距离.现在问满足所有的奶牛都能够被挤奶器服务到的情况下,行走距离的最远的奶牛的至少要走 ...
- POJ 2112 Optimal Milking(最大流+二分)
题目链接 测试dinic模版,不知道这个模版到底对不对,那个题用这份dinic就是过不了.加上优化就WA,不加优化TLE. #include <cstdio> #include <s ...
- 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 (二分+最短路径+网络流) Optimal Milking Time Limit: 2000MS Memory Limit: 30000K To ...
- 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 (二分 + 最大流)
题目大意: 在一个农场里面,有k个挤奶机,编号分别是 1..k,有c头奶牛,编号分别是k+1 .. k+c,每个挤奶机一天最让可以挤m头奶牛的奶,奶牛和挤奶机之间用邻接矩阵给出距离.求让所有奶牛都挤到 ...
随机推荐
- mongodb3的使用
1.在windows下载安装mongodb 将下载好的zip压缩文件解压并重命名为mongo-3.0.6,并在根目录下新建文件夹data用于存放数据 2.启动mongod守护进程 使用命令mongod ...
- python time模块介绍(日期格式化 时间戳)
import time # 1.time.time() 用于获取当前时间的时间戳, ticks = time.time() print(ticks) # 1545617668.8195682 浮点数 ...
- String 常见的十种方法!
public class ZiFuChuan { public static void main(String[] args) { ZiFuChuanFangFa f=new ZiFuChuanFan ...
- easyui datagrid动态修改editor时动态绑定combobox的数据
需求在 datagrid 编辑框中开启一个combobox ,但是里面的数据需要开启的时候才会知道,数据会根据其他因数变更 参考原文 :http://blog.csdn.net/donggua369 ...
- gym-101350H
题意:给你一个字符串,判断是否为镜像串,镜像串的定义:是一个回文串且只能由对称的字母组成,比如W,M,这些,因为要镜像对称: 解题思路:首先判断一下这个字符串是不是全由对称字母组成,不是就不用继续了, ...
- vi简短教程
1.模式 命令行模式:光标的移动.内容删除移动复制操作 插入模式:文字输入,即编辑状态 底行模式:文件保存或退出vi,设置编辑环境 2.基本操作 vi myfile,输入vi 文件名,则进入vi. 3 ...
- Nginx 阻塞与非阻塞
L:32
- 如何创建djiago项目和djiago连接数据库
介绍 主要介绍在python中如何使用pycharm创建djiago项目以及如何将djiago项目和mysal数据库连接起来 创建djiago项目 1.使用pycharm创建djiao项目 点击pyc ...
- .resx文件与.cs文件的自动匹配
图中myCommands.Resx是<DependentUpon> myCommands.cs文件的. 如何为其他的.cs文件添加类似的资源文件呢? 其实挺简单, 添加与.cs文件同名的资 ...
- winserver 2008 R2服务器安装IIS
winserver 2008 R2 IIS7 安装IIS 打开服务器管理器 选择“角色”,右击添加角色 点击“下一步” 勾选”Web服务器(IIS)“,点击”下一步“ 勾选”常见Http功能.应用程序 ...