Ombrophobic Bovines

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

题意:牛牛一开始分散在各个草场,每个草场存在一个有容纳上限的避难所,草场之间有道路连接(双向),并且路程耗时和道路长度成正比。问当下雨时,所有牛转移至避难所的最短时间。

思路:二分求答案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(最大流(拆点)+二分+最短路)的更多相关文章

  1. poj 2391 Ombrophobic Bovines, 最大流, 拆点, 二分, dinic, isap

    poj 2391 Ombrophobic Bovines, 最大流, 拆点, 二分 dinic /* * Author: yew1eb * Created Time: 2014年10月31日 星期五 ...

  2. poj 2391 Ombrophobic Bovines(最大流+floyd+二分)

    Ombrophobic Bovines Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 14519Accepted: 3170 De ...

  3. POJ 2391 Ombrophobic Bovines(Floyd+二分+最大流)

    题目链接 题意:农场有F(1 <= F <= 200)片草地用于放牛,这些草地有P(1 <= P <= 1500)连接,农场的草地上有一些避雨点,奶牛们可以在避雨点避雨,但是避 ...

  4. POJ 2391 Ombrophobic Bovines

    Ombrophobic Bovines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18623   Accepted: 4 ...

  5. POJ 2391 Ombrophobic Bovines (Floyd + Dinic +二分)

    Ombrophobic Bovines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11651   Accepted: 2 ...

  6. POJ 2391 Ombrophobic Bovines(二分+拆点+最大流)

    http://poj.org/problem?id=2391 题意: 给定一个无向图,点i处有Ai头牛,点i处的牛棚能容纳Bi头牛,求一个最短时间T,使得在T时间内所有的牛都能进到某一牛棚里去. 思路 ...

  7. POJ 2391 Ombrophobic Bovines ★(Floyd+二分+拆点+最大流)

    [题意]有n块草地,一些奶牛在草地上吃草,草地间有m条路,一些草地上有避雨点,每个避雨点能容纳的奶牛是有限的,给出通过每条路的时间,问最少需要多少时间能让所有奶牛进入一个避雨点. 和POJ2112很类 ...

  8. poj 2391 Ombrophobic Bovines 最短路 二分 最大流 拆点

    题目链接 题意 有\(n\)个牛棚,每个牛棚初始有\(a_i\)头牛,最后能容纳\(b_i\)头牛.有\(m\)条道路,边权为走这段路所需花费的时间.问最少需要多少时间能让所有的牛都有牛棚可待? 思路 ...

  9. POJ 2391 Ombrophobic Bovines ( 经典最大流 && Floyd && 二分 && 拆点建图)

    题意 : 给出一些牛棚,每个牛棚都原本都有一些牛但是每个牛棚可以容纳的牛都是有限的,现在给出一些路与路的花费和牛棚拥有的牛和可以容纳牛的数量,要求最短能在多少时间内使得每头牛都有安身的牛棚.( 这里注 ...

  10. POJ 2391.Ombrophobic Bovines (最大流)

    实际上是求最短的避雨时间. 首先将每个点拆成两个,一个连接源点,一个连接汇点,连接源点的点的容量为当前单的奶牛数,连接汇点的点为能容纳的奶牛数. floyd求任意两点互相到达的最短时间,二分最长时间, ...

随机推荐

  1. Docker报错:“WARNING: IPv4 forwarding is disabled. Networking will not work.”解决。

    问题阐述 一次停电之后,服务器停机,然后ip莫名被占用,修改新的ip之后,ssh能够连接上去,但是web服务访问不了,数据库访问不了,除了22端口,其它服务端口都不能telnet. 防火前.IPtab ...

  2. MFC下一个通用非阻塞的等待执行结束的对话框类

    头文件:CPictureEx用于显示一个等待动画 #pragma once #include "afxwin.h" #include "resource.h" ...

  3. [转载]借助openssl解析ECC公钥

    void GetPubKey(const char* FilePath, char* PubKey) { unsigned ]; unsigned char *pTmp = NULL; FILE *f ...

  4. KETTLE——(例)简单的字段转换

    一个简单的小例子:结合数据抽取.简单的字段转换.数据输出. 资源库连接.数据输入.数据输出.参见之前的文章. ​ 基本的转换结构是这样的,我们从表中输入,选择我们需要的字段,已经对应的名称,然后将数据 ...

  5. centos yum 安装php5.6

    centos yum 安装php5.6 卸载 php之前的版本: yum remove -y php-common 配置源 CentOS 6.5的源 rpm -Uvh http://ftp.iij.a ...

  6. Java基础/利用fastjson反序列化json为对象和对象数组

    利用fastjson反序列化json为对象和对象数组 利用 fastjosn 将 .json文件 反序列化为 java.class 和 java.util.List fastjson 是一个性能很好的 ...

  7. sql select as

    as 可理解为:用作.当成,作为:一般式重命名列名或者表名.例如有表table, 列 column_1,column_2 你可以写成 select column_1 as 列1,column_2 as ...

  8. web 前端2 CSS

    CSS CSS是Cascading Style Sheets的简称,中文称为层叠样式表,用来控制网页数据的表现,可以使网页的表现与数据内容分离. 一 css的四种引入方式 1.行内式          ...

  9. HDU-2571 命运(搜索,我才不是为了插图呢!哼!)

    看到这题其实感觉就是搜索题,广搜的话看讨论区里已经有人内存超限了,所以我选择了深搜,有两种思路,第一种是从起点出发,依次更新每一个格子的最大值,这样dp[n][m]就是最后的结果了,第二种是从起点试探 ...

  10. 从excel表中生成批量SQL

    excel表格中有许多数据,需要将数据导入数据库中,又不能一个一个手工录入,可以生成SQL,来批量操作.   ="insert into Log_loginUser (LogID, Logi ...