POJ2391:Ombrophobic Bovines

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条无向路径,现在每个点都有一定数量的牛,然后我们知道每个点都能承装的最多的牛,以及经过一条路径需要的时间。

现在每头牛都要迁徙,问最少需要多少时间所有的牛都可以成功跑进点中,满足题中给出的条件。

题解:

考虑网络流,将点拆开,每两个点之间连一条边,容量为这两个点之间的花费最小时间。

源点连点,容量为这个点有多少头牛;汇点连点,容量为这个点最多能容纳多少头牛。

显然时间越多牛就越可能全部到达,但我们这里要求的最大时间最小,可以跑个最小费用最大流,但时间复杂度有点高。

我们就考虑二分时间,然后利用二分的时间来限制前往哪些点,之后跑最大流就好了。

代码如下:

#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 = 505, M = 1505;
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)<<1];
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=0;e[tot].next=head[v];head[v]=tot++;
}
bool bfs(int S,int T){
    memset(d,0,sizeof(d));d[S]=1;
    queue <int > q;q.push(S);
    while(!q.empty()){
        int u=q.front();q.pop();
        for(int i=head[u];i!=-1;i=e[i].next){
            int v=e[i].v;
            if(!d[v] && e[i].c>0){
                d[v]=d[u]+1;
                q.push(v);
            }
        }
    }
    return d[T]!=0;
}
int dfs(int S,int a){
    int flow=0,f;
    if(S==t || a==0) return a;
    for(int i=head[S];i!=-1;i=e[i].next){
        int v=e[i].v;
        if(d[v]!=d[S]+1) continue ;
        f=dfs(v,min(a,e[i].c));
        if(f){
            e[i].c-=f;
            e[i^1].c+=f;
            flow+=f;
            a-=f;
            if(a==0) break;
        }
    }
    if(!flow) d[S]=-1;
    return flow;
}
int Dinic(){
    int max_flow=0;
    while(bfs(0,t)) max_flow+=dfs(0,INF);
    return max_flow;
}
bool check(ll x){
    memset(head,-1,sizeof(head));tot=0;
    for(int i=1;i<=n;i++) adde(s,i,has[i]);
    for(int i=n+1;i<=2*n;i++) adde(i,t,lim[i-n]);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            if(mp[i][j]<=x) adde(i,j+n,INF);
    int flow = Dinic();
    for(int i=head[s];i!=-1;i=e[i].next){
        int now = e[i].c;
        if(now>0) return false;
    }
    return true ;
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) scanf("%d%d",&has[i],&lim[i]);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            mp[i][j]=i==j ? 0 : 1e18;
    for(int i=1;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=1;k<=n;k++)
        for(int i=1;i<=n;i++)
            for(int j=1;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 = 0,r = 1e18,mid;
    while(l<r){
        mid = (l+r)>>1;
        if(check(mid)) r=mid;
        else l=mid+1;
    }
    if(r==1e18) cout<<-1;
    else cout<<l<<endl;
    return 0;
}

原文地址:https://www.cnblogs.com/heyuhhh/p/10230400.html

时间: 2024-10-10 17:49:56

POJ2391:Ombrophobic Bovines的相关文章

解题报告 之 POJ2391 Ombrophobic Bovines

解题报告 之 POJ2391 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 rai

poj2391 Ombrophobic Bovines

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

POJ2391 Ombrophobic Bovines(网络流)(拆点)

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

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

BZOJ 1738: [Usaco2005 mar]Ombrophobic Bovines 发抖的牛( floyd + 二分答案 + 最大流 )

一道水题WA了这么多次真是.... 统考终于完 ( 挂 ) 了...可以好好写题了... 先floyd跑出各个点的最短路 , 然后二分答案 m , 再建图. 每个 farm 拆成一个 cow 点和一个 shelter 点, 然后对于每个 farm x : S -> cow( x ) = cow( x ) 数量 , shelter( x ) -> T = shelter( x ) 容量 ; 对于每个dist( u , v ) <= m 的 cow( u ) -> shelter( v

Ombrophobic Bovines (poj 2391 网络流+二分+Floyd)

Language: Default Ombrophobic Bovines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15733   Accepted: 3434 Description FJ's cows really hate getting wet so much that the mere thought of getting caught in the rain makes them shake in th

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