POJ 2391--Ombrophobic Bovines(最大流(拆点)+二分+最短路)
Description
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
* 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
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
题意:牛牛一开始分散在各个草场,每个草场存在一个有容纳上限的避难所,草场之间有道路连接(双向),并且路程耗时和道路长度成正比。问当下雨时,所有牛转移至避难所的最短时间。
思路:二分求答案T。先跑一遍floyd求最短路。
构图方面,将每个点拆分成两个点i, i+n,设置源点s和汇点t, 按照 s-->i, i+n-->t,i<-->j 建立网络,容量分别为一开始牛的只数,避难所上限,INF。
对于每一个T,重新构图, 如果对于点i, j, 最短路dis[i][j]<=T,那么i-->j+n 连一条容量为INF的边,表示这两个点的牛能在T时间内转移。
然后求最大流,如果最大流大于等于牛的总只数,那么这个T是满足条件的,继续二分下去。
。。。这道题改了很长时间,最后发现是函数返回long long 和 int 搞乱了。
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
typedef long long LL;
const int MAXN=2e3+;
const int INF=1e9+;
const LL INFLL=1e16+;
struct Edge{
int to;
int c;
int rev;
};
LL dis[MAXN][MAXN];
int sum=,s,t,n,m;
vector<Edge> vec[MAXN];
int in[MAXN],out[MAXN];
int level[MAXN],iter[MAXN];
void floyd(){
for(int k=;k<=n;k++){
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
dis[i][j]=min(dis[i][j], dis[i][k]+dis[k][j]);
}
}
}
return;
}
void add_edge(int a, int b, int cap){
vec[a].push_back((Edge){b, cap, vec[b].size()});
vec[b].push_back((Edge){a, , vec[a].size()-});
return;
} int dfs(int S, int T, int flow){
if(S==T) return flow; for(int &i=iter[S];i<vec[S].size();i++){
Edge &e=vec[S][i];
if(level[S]<level[e.to]&&e.c>){
int d=dfs(e.to, T, min(e.c, flow));
if(d>){
e.c-=d;
vec[e.to][e.rev].c+=d;
return d;
}
}
}
return ;
}
void rebuild(LL T)
{
for(int i=;i<MAXN;i++)
vec[i].clear();
for(int i=;i<=n;i++){
add_edge(s, i, in[i]);
add_edge(i+n, t, out[i]);
add_edge(i, i+n, INF);
}
for(int i=;i<=n;i++){
for(int j=i+;j<=n;j++){
if(dis[i][j]<=T){
add_edge(i, j+n, INF);
add_edge(j, i+n, INF);
}
}
}
}
void bfs(){
memset(level, -, sizeof(level));
queue<int> q;
level[s]=;
q.push(s);
while(!q.empty()){
int v=q.front();q.pop();
for(int i=;i<vec[v].size();i++){
Edge &e=vec[v][i];
if(e.c>&&level[e.to]<){
level[e.to]=level[v]+;
q.push(e.to);
}
}
}
return;
}
int max_flow(LL T){
rebuild(T);
int res=,flow;
while(){
bfs();
if(level[t]<) return res;
memset(iter, , sizeof(iter));
while((flow=dfs(s, t, INF)) > ){
res+=flow;
}
}
//cout<<res<<endl;
return res;
}
LL solve(LL l, LL r){
LL mid,ans=-;
while(l<=r){
mid=(l+r)/;
if(max_flow(mid)>=sum){//dinic算法
ans=mid;
r=mid-;
}
else l=mid+;
//cout<<ans<<endl;
}
return ans;
}
int main()
{
int a,b,w;
scanf("%d %d", &n, &m);
s=,t=*n+;
for(int i=;i<=n;i++){
scanf("%d %d",&in[i], &out[i]);
sum+=in[i];
}
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
dis[i][j]=(i==j)?:INFLL;
for(int i=;i<m;i++){
scanf("%d %d %d", &a, &b, &w);
if(dis[a][b]>w)
dis[a][b]=dis[b][a]=w;
}
floyd();
LL T=solve(, INFLL-);
printf("%lld\n", T);
return ;
}
POJ 2391--Ombrophobic Bovines(最大流(拆点)+二分+最短路)的更多相关文章
- poj 2391 Ombrophobic Bovines, 最大流, 拆点, 二分, dinic, isap
poj 2391 Ombrophobic Bovines, 最大流, 拆点, 二分 dinic /* * Author: yew1eb * Created Time: 2014年10月31日 星期五 ...
- poj 2391 Ombrophobic Bovines(最大流+floyd+二分)
Ombrophobic Bovines Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 14519Accepted: 3170 De ...
- POJ 2391 Ombrophobic Bovines(Floyd+二分+最大流)
题目链接 题意:农场有F(1 <= F <= 200)片草地用于放牛,这些草地有P(1 <= P <= 1500)连接,农场的草地上有一些避雨点,奶牛们可以在避雨点避雨,但是避 ...
- POJ 2391 Ombrophobic Bovines
Ombrophobic Bovines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18623 Accepted: 4 ...
- POJ 2391 Ombrophobic Bovines (Floyd + Dinic +二分)
Ombrophobic Bovines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11651 Accepted: 2 ...
- POJ 2391 Ombrophobic Bovines(二分+拆点+最大流)
http://poj.org/problem?id=2391 题意: 给定一个无向图,点i处有Ai头牛,点i处的牛棚能容纳Bi头牛,求一个最短时间T,使得在T时间内所有的牛都能进到某一牛棚里去. 思路 ...
- POJ 2391 Ombrophobic Bovines ★(Floyd+二分+拆点+最大流)
[题意]有n块草地,一些奶牛在草地上吃草,草地间有m条路,一些草地上有避雨点,每个避雨点能容纳的奶牛是有限的,给出通过每条路的时间,问最少需要多少时间能让所有奶牛进入一个避雨点. 和POJ2112很类 ...
- poj 2391 Ombrophobic Bovines 最短路 二分 最大流 拆点
题目链接 题意 有\(n\)个牛棚,每个牛棚初始有\(a_i\)头牛,最后能容纳\(b_i\)头牛.有\(m\)条道路,边权为走这段路所需花费的时间.问最少需要多少时间能让所有的牛都有牛棚可待? 思路 ...
- POJ 2391 Ombrophobic Bovines ( 经典最大流 && Floyd && 二分 && 拆点建图)
题意 : 给出一些牛棚,每个牛棚都原本都有一些牛但是每个牛棚可以容纳的牛都是有限的,现在给出一些路与路的花费和牛棚拥有的牛和可以容纳牛的数量,要求最短能在多少时间内使得每头牛都有安身的牛棚.( 这里注 ...
- POJ 2391.Ombrophobic Bovines (最大流)
实际上是求最短的避雨时间. 首先将每个点拆成两个,一个连接源点,一个连接汇点,连接源点的点的容量为当前单的奶牛数,连接汇点的点为能容纳的奶牛数. floyd求任意两点互相到达的最短时间,二分最长时间, ...
随机推荐
- create-react-app 创建react应用环境变量(env)配置
参考:https://facebook.github.io/create-react-app/docs/adding-custom-environment-variables What other . ...
- 【FICO系列】SAP 财务帐与后勤不一致情况
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[FICO系列]SAP 财务帐与后勤不一致情况 ...
- Windows7 系统安装
转载请标明本文链接:(https://www.cnblogs.com/softwarecb/p/11773811.html) 目前微软已经停止支持Windows 7,而且由于芯片组更新的原因,新的硬件 ...
- "SQLServer复制需要有实际的服务器名称才能连接到服务器,请指定实际的服务器名"
最近在学习SQL SERVER的高级复制技术的时候,遇到一个小问题,就是用本地SQL SERVER连接服务器的数据库时,在查看复制功能的发布服务器时,连接不上,弹出一个错误提示框架,如下:原来在自己本 ...
- Redis功能迅速回忆
- lesson1-图的概念和图论模型
说明: 图论专题开设的目的主要是作为本学期复习巩固和分享自己对于图论的理解,主要参考的是老师的PPT.应老师要求,不能共享文件,抱歉! 参考书目:[1] J.A. Bondy, U.S.R. Mur ...
- Varint数值压缩存储方法
coming from http://www.cnblogs.com/smark/archive/2012/05/03/2480034.html 在编写网络通讯的时候我们经常需要把一些数据存储到byt ...
- 创建客户端项目并读取服务化的配置中心(Consul + Spring Cloud Config)
创建客户端项目并读取服务化的配置中心 将配置中心注册到服务中心(Consul) POM文件添加依赖: <dependency> <groupId>org.springframe ...
- Java学习day4 程序流程控制一
一.分支结构 条件语句:if...else if语句: 一个 if 语句包含一个布尔表达式和一条或多条语句,如果布尔表达式的值为 true,则执行 if 语句中的代码块,否则执行 if 语句块后面的代 ...
- arcgis server地图服务切片(10.4.1)
首先要发布地图服务,过程略 首先,熟悉arcgis server的人应该知道,最直接的切片方式操作方法是在“服务属性”中设置切片,但这种方式可操作性太差,很多设置无法实现,因此不推荐 下面正式开始,打 ...