POJ 2391-Ombrophobic Bovines(网络流_最大流+floyd+二分)

Ombrophobic Bovines

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15485   Accepted: 3361

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

Hint

OUTPUT DETAILS:

In 110 time units, two cows from field 1 can get under the shelter in that field, four cows from field 1 can get under the shelter in field 2, and one cow can get to field 3 and join the cows from that field under the shelter in field 3. Although there are
other plans that will get all the cows under a shelter, none will do it in fewer than 110 time units.

题意:有F块田,P条小路,点 i 处有 Ai 头牛,点 i 处的牛棚能容纳 Bi 头牛,求一个最短时 间 T 使得在 T 时间内所有的牛都能进到某一牛棚里去。

思路:显然建模是从源点S连接每一个牛棚,容量为当前牛数,再从每一个点连接一条边到汇点T,容量为每一个牛棚的容量,求最短时间采用的是二分搜索算法,核心是枚举可能的时间然后求一次最大流,如果最大流结果刚好等于总牛数,那么这个时间显然符合题意就将他记录下来,我们要找到最小的时间,故将时间向小的地方调整,如果不等于说明对于当前时间有牛不能走到汇点,故将二分向大的地方调整,就这样一直二分下去,这里需要进行拆点,将某一个点i拆成i和i+n,然后在每一次二分开始前重建网络,如果i和i+n之间的最短路径小于等于当前值,说明在当前时间下可以走到汇点,也就是说这个牛棚可以被装满,那就连接一条边从i->i+n容量为无穷大,建完图后求一次最大流,然后加入二分进行比较,得到的最终结果就是题目所求

PS:因为一个dp初始化的问题 wa了一晚上,sad

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <set>
#include <queue>
#include <map>

using namespace std;

const long long maxxint=1e16;
const int inf=0x3f3f3f3f;
int head[100010],num[1010],d[1010],cur[1010],pre[10010],q[1010];
long long dp[1010][1010],n,sum,cnt,s,t,nv;
int maxint=inf;
struct node {
    int u,v,cap;
    int next;
} edge[10000010];
struct filed {
    int x,y;
} p[1010];
void add(int u, int v, int cap)
{
    edge[cnt].v=v;
    edge[cnt].cap=cap;
    edge[cnt].next=head[u];
    head[u]=cnt++;

    edge[cnt].v=u;
    edge[cnt].cap=0;
    edge[cnt].next=head[v];
    head[v]=cnt++;
}
void floyd()
{
    int i, j, k;
    for(k=1; k<=n; k++)
        for(i=1; i<=n; i++)
            for(j=1; j<=n; j++)
                dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]);
}
void bfs()
{
    memset(num,0,sizeof(num));
    memset(d,-1,sizeof(d));
    int f1=0,f2=0,i;
    q[f1++]=t;
    num[0]=1;
    d[t]=0;
    while(f1>=f2) {
        int u=q[f2++];
        for(i=head[u]; i!=-1; i=edge[i].next) {
            int v=edge[i].v;
            if(d[v]!=-1) continue;
            d[v]=d[u]+1;
            num[d[v]]++;
            q[f1++]=v;
        }
    }
}
int isap()
{
    memcpy(cur,head,sizeof(cur));
    int flow=0,u=pre[s]=s,i;
    bfs();
    while(d[s]<nv) {
        if(u==t) {
            int f=maxint, pos;
            for(i=s; i!=t; i=edge[cur[i]].v) {
                if(f>edge[cur[i]].cap) {
                    f=edge[cur[i]].cap;
                    pos=i;
                }
            }
            for(i=s; i!=t; i=edge[cur[i]].v) {
                edge[cur[i]].cap-=f;
                edge[cur[i]^1].cap+=f;
            }
            flow+=f;
            if(flow>=sum)
                return flow;
            u=pos;
        }
        for(i=cur[u]; i!=-1; i=edge[i].next) {
            if(d[edge[i].v]+1==d[u]&&edge[i].cap)
                break;
        }
        if(i!=-1) {
            cur[u]=i;
            pre[edge[i].v]=u;
            u=edge[i].v;
        } else {
            if((--num[d[u]])==0) break;
            int mind=nv;
            for(i=head[u]; i!=-1; i=edge[i].next) {
                if(mind>d[edge[i].v]&&edge[i].cap) {
                    mind=d[edge[i].v];
                    cur[u]=i;
                }
            }
            d[u]=mind+1;
            num[d[u]]++;
            u=pre[u];
        }
    }
    return flow;
}
int main()
{
    long long m,i,j;
    long long a,b,c;
    scanf("%lld %lld",&n,&m);
    sum=0;
    for(i=0; i<=n; i++)
        for(j=0; j<=n; j++) {
            if(i==j)
                dp[i][j]=0;
            else
                dp[i][j]=maxxint;//因为这个地方初始化的是inf 导致wa了一晚上,应该初始化为maxxint,因为在下面二分的时候的上界是2^11
        }
    for(i=1; i<=n; i++) {
        scanf("%d %d",&p[i].x,&p[i].y);
        sum+=p[i].x;
    }
    while(m--) {
        scanf("%lld %lld %lld",&a,&b,&c);
        if(dp[a][b]>c) {
            dp[a][b]=dp[b][a]=c;
        }
    }
    floyd();
    int x;
    long long low=1,high=2000000000000,mid,ans=-1;
    while(low<=high) {
        mid=(high+low)/2;
        cnt=0;
        s=0;
        t=2*n+1;
        nv=t+1;
        memset(head,-1,sizeof(head));
        for(i=1; i<=n; i++) {
            add(s,i,p[i].x);
            add(i+n,t,p[i].y);
        }
        for(i=1; i<=n; i++)
            for(j=1; j<=n; j++) {
                if(dp[i][j]<=mid)
                    add(i,j+n,inf);
            }
        x=isap();
        if(x>=sum) {
            ans=mid;
            high=mid-1;
        } else
            low=mid+1;
    }
    printf("%lld\n",ans);
    return 0;
}
时间: 2024-11-16 03:35:42

POJ 2391-Ombrophobic Bovines(网络流_最大流+floyd+二分)的相关文章

Poj 2391 Ombrophobic Bovines 网络流 拆点

Poj 2391 Ombrophobic Bovines 网络流 拆点 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 approa

POJ 2391 Ombrophobic Bovines 网络流 建模

[题目大意]给定一个无向图,点i处有Ai头牛,点i处的牛棚能容纳Bi头牛,求一个最短时间T使得在T时间内所有的牛都能进到某一牛棚里去.(1 <= N <= 200, 1 <= M <= 1500, 0 <= Ai <= 1000, 0 <= Bi <= 1000, 1 <= Dij <= 1,000,000,000) 一开始想拆点建图,0到x集合为汇,值为各个区域的牛数量, Y到终点连边,值为各个区域的容量,然后就是看怎么连x和y了 我一开始把可

POJ 2112-Optimal Milking(网络流_最大流+floyd+二分查找)

Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 12810   Accepted: 4632 Case Time Limit: 1000MS Description FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among the C (1 <= C <= 200) co

POJ 2391 Ombrophobic Bovines 不喜欢雨的奶牛 Floyd+二分枚举+最大流

题目链接:POJ 2391 Ombrophobic Bovines Ombrophobic Bovines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15006   Accepted: 3278 Description FJ's cows really hate getting wet so much that the mere thought of getting caught in the rain makes

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

poj 2391 Ombrophobic Bovines, 最大流, 拆点, 二分 dinic /* * Author: yew1eb * Created Time: 2014年10月31日 星期五 15时39分22秒 * File Name: poj2391.cpp */ #include <ctime> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring&g

POJ 2391 Ombrophobic Bovines (二分,最短路径,网络流sap,dinic,预留推进 )

Ombrophobic Bovines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14019   Accepted: 3068 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 h

POJ 2391 Ombrophobic Bovines (二分 + floyd + 网络流)

POJ 2391 Ombrophobic Bovines 链接:http://poj.org/problem?id=2391 题目:农场有F 块草地,1≤F≤200,奶牛们在草地上吃草.这些草地之间有P 条路相连,1≤P≤1500,这些路足够宽,再多的奶牛也能同时在路上行走.有些草地上有避雨点,奶牛们可以在此避雨.避雨点的容量是有限的,所以一个避雨点不可能容纳下所有的奶牛.草地与路相比很小,奶牛们通过时不需要花费时间.计算警报至少需要提前多少时间拉响,以保证所有的奶牛都能到达一个避雨点. 思路:

POJ 2391 Ombrophobic Bovines(最大流+拆点)

POJ 2391 Ombrophobic Bovines 题目链接 题意:一些牛棚,有a只牛,现在下雨,每个牛棚容量量变成b,现在有一些道路连接了牛棚,问下雨后牛走到其他牛棚,使得所有牛都有地方躲雨,最后一只牛要走多久 思路:二分答案,然后最大流去判断,建图的方式为,牛棚拆点,源点连向入点,容量为a,出点连向汇点容量为b,中间入点和出点之间根据二分的值判断哪些边是可以加入的 代码: #include <cstdio> #include <cstring> #include <

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

Ombrophobic Bovines Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 14519Accepted: 3170 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 de