poj2112 网络流+二分答案
| Time Limit: 2000MS | Memory Limit: 30000K | |
| Total Submissions: 18083 | Accepted: 6460 | |
| 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
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=;
int head[N],dis[N],tot,K,C,M,st,ed,mp[N][N];
struct node
{
int to,next,w;
} e[N*N*];
void add(int u,int v,int w)
{
e[tot].to=v;
e[tot].next=head[u];
e[tot].w=w;
head[u]=tot++;
}
void init()
{
tot=;
memset(head,-,sizeof(head));
}
bool bfs()
{
queue<int>Q;
memset(dis,-,sizeof(dis));
Q.push();
dis[]=;
while(!Q.empty())
{
int u=Q.front();
Q.pop();
for(int i=head[u]; i+; i=e[i].next)
{
int v=e[i].to;
if(dis[v]==-&&e[i].w>)
{
dis[v]=dis[u]+;
Q.push(v);
if(v==K+C+) return true;
}
}
}
return false;
}
int dfs(int s,int low)
{
if(s==ed||low==) return low;
int ans=low,a;
for(int i=head[s]; i+; i=e[i].next)
{
int v=e[i].to;
if(e[i].w>&&dis[v]==dis[s]+&&(a=dfs(v,min(ans,e[i].w))))
{
ans-=a;
e[i].w-=a;
e[i^].w+=a;
if(!ans) return low;
}
}
if(ans==low) dis[s]=-;
return low-ans;
}
void build(int lim)
{
init();
for(int i=; i<=K; ++i) for(int j=K+; j<ed; ++j) if(mp[i][j]<=lim&&mp[i][j])
{
add(i,j,);
add(j,i,);
}
for(int i=; i<=K; ++i) add(,i,M),add(i,,);
for(int i=K+; i<ed; ++i) add(i,ed,),add(ed,i,);
}
bool Ju()
{
int ans=;
while(bfs()) ans+=dfs(,);
if(ans==C) return true;
return false;
}
void Floyd()
{
for(int k=; k<ed; ++k) for(int i=; i<ed; ++i) if(mp[i][k]) for(int j=; j<ed; ++j) if(mp[k][j])
{
if(mp[i][j]==) mp[i][j]=mp[i][k]+mp[k][j];
else if(mp[i][j]>mp[i][k]+mp[k][j]) mp[i][j]=mp[i][k]+mp[k][j];
}
}
int main()
{
while(scanf("%d%d%d",&K,&C,&M)!=EOF)
{
st=;
ed=K+C+;
for(int i=; i<ed; ++i) for(int j=; j<ed; ++j) scanf("%d",&mp[i][j]);
Floyd();
int r=,l=;
while(r!=l)
{
int mid=(r+l)>>;
build(mid);
if(Ju()) r=mid;
else l=mid+;
}
printf("%d\n",(l+r)>>);
}
}
poj2112 网络流+二分答案的更多相关文章
- CQOI跳舞(网络流+二分答案)
题面见 https://www.luogu.org/problemnew/show/P3153 题意简述:有n个男生,n个女生,每一首歌时两位男女配对,然后同一对男女只能跳一场,一个人只会与不喜欢的人 ...
- BZOJ 3993 [SDOI2015]星际战争 | 网络流 二分答案
链接 BZOJ 3993 题解 这道题挺棵的-- 二分答案t,然后源点向武器连t * b[i], 武器向能攻击的敌人连1, 敌人向汇点连a[i],如果最大流等于所有敌人的a[i]之和则可行. #inc ...
- BZOJ 2406: 矩阵 [上下界网络流 二分答案]
2406: 矩阵 题意:自己去看吧,最小化每行每列所有元素与给定矩阵差的和的绝对值中的最大值 又带绝对值又带max不方便直接求 显然可以二分这个最大值 然后判定问题,给定矩阵每行每列的范围和每个元素的 ...
- BZOJ 1733: [Usaco2005 feb]Secret Milking Machine 神秘的挤奶机 网络流 + 二分答案
Description Farmer John is constructing a new milking machine and wishes to keep it secret as long a ...
- 洛谷P2402 奶牛隐藏(网络流,二分答案,Floyd)
洛谷题目传送门 了解网络流和dinic算法请点这里(感谢SYCstudio) 题目 题目背景 这本是一个非常简单的问题,然而奶牛们由于下雨已经非常混乱,无法完成这一计算,于是这个任务就交给了你.(奶牛 ...
- 【BZOJ3993】星际战争(网络流,二分答案)
[BZOJ3993]星际战争(网络流,二分答案) 题面 Description 3333年,在银河系的某星球上,X军团和Y军团正在激烈地作战.在战斗的某一阶段,Y军团一共派遣了N个巨型机器人进攻X军团 ...
- 【BZOJ5251】【八省联考2018】劈配(网络流,二分答案)
[BZOJ5251][八省联考2018]劈配(网络流,二分答案) 题面 洛谷 BZOJ Description 一年一度的综艺节目<中国新代码>又开始了. Zayid从小就梦想成为一名程序 ...
- bzoj1305: [CQOI2009]dance跳舞(二分答案+网络流)
1305: [CQOI2009]dance跳舞 题目:传送门 题解: 一眼网络流基础建模...然后就GG了 二分答案+拆点建边+最大流判断: 把男女生拆为男1,男2,女1,女2 1.男1和男2还有女1 ...
- 稳定的奶牛分配 && 二分图多重匹配+二分答案
题意: 农夫约翰有N(1<=N<=1000)只奶牛,每只奶牛住在B(1<=B<=20)个奶牛棚中的一个.当然,奶牛棚的容量有限.有些奶牛对它现在住的奶牛棚很满意,有些就不太满意 ...
随机推荐
- java中的Volatile关键字使用
文章目录 什么时候使用volatile Happens-Before java中的Volatile关键字使用 在本文中,我们会介绍java中的一个关键字volatile. volatile的中文意思是 ...
- Linux中的常用快捷键
tab 命令或路径补全键,linux里面最有用的快捷键,如果tab不到路径或命令,就代表没有这个路径或者命令,还有可能是权限不对 ctrl+c 终止当前任务命令或程序 ctrl+d 退出当前用户环境, ...
- 【Linux常见命令】vi,vim命令
所有的 Unix Like 系统都会内建 vi 文书编辑器,其他的文书编辑器则不一定会存在. 但是目前我们使用比较多的是 vim 编辑器. vim 具有程序编辑的能力,可以主动的以字体颜色辨别语法的正 ...
- JavaScript HTMlL DOM对象(上)
Dom:document.相当于把所有的html文件,转换成了文档对象. 之前说过:html-裸体的人:css-穿上衣服:js-让人动起来. 让人动起来,就得先找到他,再修改它内容或属性. 找到标签 ...
- Docker学习之搭建MySql容器服务
描述 MySQL 5.6 SQL数据库服务器Docker镜像,此容器映像包含用于OpenShift的MySQL 5.6 SQL数据库服务器和一般用法.用户可以选择RHEL和基于CentOS的图像.然后 ...
- 利用Mysqlbinlog恢复数据库数据
关于binlog的详解请参考:http://zlyang.blog.51cto.com/1196234/1833062 binlog日志用于记录所有更新了数据或者已经潜在更新了数据的所有语句.语句以& ...
- 图论--双连通E-DCC缩点模板
// tarjan算法求无向图的桥.边双连通分量并缩点 #include<iostream> #include<cstdio> #include<cstring> ...
- python(数据类型)
一.基本数据类型 (1)numbers 数字 整型 int a = 1 print (type(a)) 长整型 long python3.x 中无此类型 >>> 2 ** 100 1 ...
- Nacos作为配置中心时,多个服务共用一个dataId的配置
写在前面 本文是对我之前一篇文章<Spring Cloud+nacos+Feign,实现注册中心及配置中心>的补充.此文章中简单写了如何将Nacos作为配置中心.在使用配置中心时,我们会遇 ...
- 自己封装函数,实现数组的内置方法indexOf的功能
在学习或开发过程中,经常会有朋友需要使用到一个数组方法-indexOf,这里我们先来谈谈它的功能:返回指定数据所在的索引,如果没有则返回-1. 那么我们在使用时通常是直接使用它这个数组内置方法 今天这 ...