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. 阶段1 语言基础+高级_1-3-Java语言高级_03-常用API第二部分_第1节 Object类_1_Object类的toString方法

    任何一个类都可以使用Object类中的方法 ## 1.1 概述 `java.lang.Object`类是Java语言中的根类,即所有类的父类.它中描述的所有方法子类都可以使用.在对象实例化的时候,最终 ...

  2. Week 8 - 338.Counting Bits & 413. Arithmetic Slices

    338.Counting Bits - Medium Given a non negative integer number num. For every numbers i in the range ...

  3. PHP多图片上传 并检查 加水印 源码

    参数说明:$max_file_size : 上传文件大小限制, 单位BYTE$destination_folder : 上传文件路径$watermark : 是否附加水印(1为加水印,其他为不加水印) ...

  4. 【ABAP系列】SAP ABAP 关于ALV布局保存选项的讲解

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP ABAP 关于ALV布局保存 ...

  5. js数组,运算符

  6. char数组初始化

    初始化 char str[10]="Hello"; char str[10]={'H','e','l','l','o','\0'}; char str[10]={'H'}; cha ...

  7. 手把手教你用Pytorch-Transformers——实战(二)

    本文是<手把手教你用Pytorch-Transformers>的第二篇,主要讲实战 手把手教你用Pytorch-Transformers——部分源码解读及相关说明(一) 使用 PyTorc ...

  8. Java对象多态性——对象的转型(引用类型之间的类型转换)

    Java引用变量有两个类型:编译时类型和运行时类型.编译时类型由声明该变量时使用的类型决定,运行时类型由实际赋给该变量的对象决定. 若编译时类型和运行时类型不一致,就出现多态 正常的方法调用(本态调用 ...

  9. java类从加载、连接到初始化过程

    类加载器 在了解Java的机制之前,需要先了解类在JVM(Java虚拟机)中是如何加载的,这对后面理解java其它机制将有重要作用. 每个类编译后产生一个Class对象,存储在.class文件中,JV ...

  10. A.Gennady and a Card Game

    http://m3.codeforces.com/contest/1097/problem/A Gennady and a Card Game time limit per test 1 second ...