POJ2391:Ombrophobic Bovines(最大流+Floyd+二分)
Ombrophobic Bovines
Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 21660Accepted: 4658
题目链接:http://poj.org/problem?id=2391
Description:
FJ's cows really hate getting wet so much that the mere thought of getting caught in the rain makes them shake in their hooves. They have decided to put a rain siren on the farm to let them know when rain is approaching. They intend to create a rain evacuation plan so that all the cows can get to shelter before the rain begins. Weather forecasting is not always correct, though. In order to minimize false alarms, they want to sound the siren as late as possible while still giving enough time for all the cows to get to some shelter.
The farm has F (1 <= F <= 200) fields on which the cows graze. A set of P (1 <= P <= 1500) paths connects them. The paths are wide, so that any number of cows can traverse a path in either direction.
Some of the farm's fields have rain shelters under which the cows can shield themselves. These shelters are of limited size, so a single shelter might not be able to hold all the cows. Fields are small compared to the paths and require no time for cows to traverse.
Compute the minimum amount of time before rain starts that the siren must be sounded so that every cow can get to some shelter.
Input:
* Line 1: Two space-separated integers: F and P
* Lines 2..F+1: Two space-separated integers that describe a field. The first integer (range: 0..1000) is the number of cows in that field. The second integer (range: 0..1000) is the number of cows the shelter in that field can hold. Line i+1 describes field i.
* Lines F+2..F+P+1: Three space-separated integers that describe a path. The first and second integers (both range 1..F) tell the fields connected by the path. The third integer (range: 1..1,000,000,000) is how long any cow takes to traverse it.
Output:
* Line 1: The minimum amount of time required for all cows to get under a shelter, presuming they plan their routes optimally. If it not possible for the all the cows to get under a shelter, output "-1".
Sample Input:
3 4
7 2
0 4
2 6
1 2 40
3 2 70
2 3 90
1 3 120
Sample Output:
110
题意:
给出n个点,m条无向路径,现在每个点都有一定数量的牛,然后我们知道每个点都能承装的最多的牛,以及经过一条路径需要的时间。
现在每头牛都要迁徙,问最少需要多少时间所有的牛都可以成功跑进点中,满足题中给出的条件。
题解:
考虑网络流,将点拆开,每两个点之间连一条边,容量为这两个点之间的花费最小时间。最小花费由floyd易求。
源点连点,容量为这个点有多少头牛;汇点连点,容量为这个点最多能容纳多少头牛。
显然时间越多牛就越可能全部到达,但我们这里要求的最大时间最小,可以跑个最小费用最大流,但时间复杂度有点高。
我们就考虑二分时间,然后利用二分的时间来限制前往哪些点,之后跑最大流就好了。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#define s 0
#define t 2*n+1
#define INF 1e9
using namespace std;
typedef long long ll;
const int N = , M = ;
int n,m,tot,need;
int lim[N],has[N],head[N],d[N];
ll mp[N][N];
struct Edge{
int v,next,c;
}e[(N*N)<<];
void adde(int u,int v,int c){
e[tot].v=v;e[tot].c=c;e[tot].next=head[u];head[u]=tot++;
e[tot].v=u;e[tot].c=;e[tot].next=head[v];head[v]=tot++;
}
bool bfs(int S,int T){
memset(d,,sizeof(d));d[S]=;
queue <int > q;q.push(S);
while(!q.empty()){
int u=q.front();q.pop();
for(int i=head[u];i!=-;i=e[i].next){
int v=e[i].v;
if(!d[v] && e[i].c>){
d[v]=d[u]+;
q.push(v);
}
}
}
return d[T]!=;
}
int dfs(int S,int a){
int flow=,f;
if(S==t || a==) return a;
for(int i=head[S];i!=-;i=e[i].next){
int v=e[i].v;
if(d[v]!=d[S]+) continue ;
f=dfs(v,min(a,e[i].c));
if(f){
e[i].c-=f;
e[i^].c+=f;
flow+=f;
a-=f;
if(a==) break;
}
}
if(!flow) d[S]=-;
return flow;
}
int Dinic(){
int max_flow=;
while(bfs(,t)) max_flow+=dfs(,INF);
return max_flow;
}
bool check(ll x){
memset(head,-,sizeof(head));tot=;
for(int i=;i<=n;i++) adde(s,i,has[i]);
for(int i=n+;i<=*n;i++) adde(i,t,lim[i-n]);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(mp[i][j]<=x) adde(i,j+n,INF);
int flow = Dinic();
for(int i=head[s];i!=-;i=e[i].next){
int now = e[i].c;
if(now>) return false;
}
return true ;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) scanf("%d%d",&has[i],&lim[i]);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
mp[i][j]=i==j ? : 1e18;
for(int i=;i<=m;i++){
int u,v;ll w;
scanf("%d%d%I64d",&u,&v,&w);
ll tmp = mp[u][v];
mp[u][v]=mp[v][u]=min(tmp,w);
}
for(int k=;k<=n;k++)
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(mp[i][j]>mp[i][k]+mp[k][j] && mp[i][k]!=1e18 && mp[k][j]!=1e18)
mp[i][j]=mp[i][k]+mp[k][j];
ll l = ,r = 1e18,mid;
while(l<r){
mid = (l+r)>>;
if(check(mid)) r=mid;
else l=mid+;
}
if(r==1e18) cout<<-;
else cout<<l<<endl;
return ;
}
POJ2391:Ombrophobic Bovines(最大流+Floyd+二分)的更多相关文章
- poj 2391 Ombrophobic Bovines(最大流+floyd+二分)
Ombrophobic Bovines Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 14519Accepted: 3170 De ...
- POJ2391 Ombrophobic Bovines 网络流拆点+二分+floyed
题目链接: id=2391">poj2391 题意: 有n块草地,每块草地上有一定数量的奶牛和一个雨棚,并给出了每一个雨棚的容(牛)量. 有m条路径连接这些草地 ,这些路径是双向的, ...
- poj 2391 Ombrophobic Bovines, 最大流, 拆点, 二分, dinic, isap
poj 2391 Ombrophobic Bovines, 最大流, 拆点, 二分 dinic /* * Author: yew1eb * Created Time: 2014年10月31日 星期五 ...
- poj2391 Ombrophobic Bovines 拆点+二分法+最大流
/** 题目:poj2391 Ombrophobic Bovines 链接:http://poj.org/problem?id=2391 题意:有n块区域,第i块区域有ai头奶牛,以及一个可以容纳bi ...
- POJ 2391 Ombrophobic Bovines ( 经典最大流 && Floyd && 二分 && 拆点建图)
题意 : 给出一些牛棚,每个牛棚都原本都有一些牛但是每个牛棚可以容纳的牛都是有限的,现在给出一些路与路的花费和牛棚拥有的牛和可以容纳牛的数量,要求最短能在多少时间内使得每头牛都有安身的牛棚.( 这里注 ...
- POJ2391 Ombrophobic Bovines
Ombrophobic Bovines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19359 Accepted: 4 ...
- POJ2391 Ombrophobic Bovines(网络流)(拆点)
Ombrophobic Bovines Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- poj2391 Ombrophobic Bovines 题解
http://poj.org/problem?id=2391 floyd+网络流+二分 题意:有一个有向图,里面每个点有ai头牛,快下雨了牛要躲进雨棚里,每个点有bi个雨棚,每个雨棚只能躲1头牛.牛可 ...
- poj2112 最大流+floyd+二分
题意:给一堆点,一部分是牛,一部分是机器,每头牛必须要走到一个机器,每个点之间有距离,要求每头牛都能找得到一台机器(机器有最大容量)的情况下,走的最远的牛距离最小 题解:二分答案,小于该距离的边才能加 ...
随机推荐
- python3爬虫之开篇
写在前面的话: 折腾爬虫也有一段时间了,从一开始的懵懵懂懂,到现在的有一定基础,对于这一路的跌跌撞撞,个人觉得应该留下一些文字性的东西,毕竟好记性不如烂笔头,而且毕竟这是吃饭的家伙,必须用心对待才可以 ...
- python正则表达式+正则大量实例
正则表达式 正则表达式内部函数详解http://www.runoob.com/python/python-reg-expressions.html 正则表达式是一个特殊的字符序列,它能帮助你方便的检查 ...
- Redis缓存数据库的安装与配置(1)
1.安装 tarxf redis-3.2.5.tar.gz cd redis-3.2.5 make mkdir -p /usr/local/redis/bin src目录下这些文件作用如下 redis ...
- 02---Nginx
Nginx .caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { border: ...
- idea启动spring boot无法加载或找不到主类
问题产生原因:moudle名称修改,导致项目启动不了 在Terminal界面中执行以下三个命令,我在执行第一个命令的时候报了一个找不到dependency的错误,把那个报错的dependency删了就 ...
- 笔记-twisted-adbapi-scrapy
笔记-twisted-adbapi-scrapy-mysql 1. 异步插入mysql 在爬虫中需要insert到mysql,但有一个问题是在爬虫环境中commit的及时性与性能冲突. 一般 ...
- 前端学习webpack
### 模块化- 为了保证代码充分解耦,一个大的项目拆分成互相依赖的一个一个的小的模块,最后再通过简单的方式合并在一起- 每一个js文件都可以看成一个单独的模块在node这边(服务器端),提出Comm ...
- 环境变量 - JDK
Linux 1. 备份并编辑配置文件 # cp /etc/profile /etc/profile.bak # vi /etc/profile 2. 设置JDK环境变量 export JAVA_HOM ...
- LeetCode题目解答
LeetCode题目解答——Easy部分 Posted on 2014 年 11 月 3 日 by 四火 [Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复 ...
- (4)分布式下的爬虫Scrapy应该如何做-规则自动爬取及命令行下传参
本次探讨的主题是规则爬取的实现及命令行下的自定义参数的传递,规则下的爬虫在我看来才是真正意义上的爬虫. 我们选从逻辑上来看,这种爬虫是如何工作的: 我们给定一个起点的url link ,进入页面之后提 ...