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 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.

Source

USACO 2005 March Gold

题意:农场有F块草地,奶牛们在草地上吃草。这些草地间有P条路相连,这些路足够宽,再多的牛也能在路上行走。有些草地上有避雨点,奶牛在避雨点避雨。避雨点的容量是有限的。奶牛要在下雨前全部到达某个避雨点,计算报警至少要提前多少时间拉响,以保证所有的奶牛能够到达一个避雨点。

思路:先预处理floyd求出各点之间的最短路,拆点,将每个点拆成两个,注意第i个点连第i+F个点时保证单向,防止回流,权值为inf,另外是无向边,要建两条边(这个wa了我二十多发),网络流+二分,二分时间来判断两个地方能不能连边,求最大流,如果最大流>=牛的总数,则 r=mid-1;否则l=mid+1。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 1005
#define MAXN 855
#define MAXM 1000001
#define mod 1000000009
const int inf=1<<30;
#define INF 10000000000000
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

int tol,f,p,s,t,sum;
ll mp[MAXN][MAXN],maxxx;
int date[MAXN][2];

struct Edge
{
    int to,next,cap,flow;
}edge[MAXM];

int head[MAXN];
int gap[MAXN],dep[MAXN],pre[MAXN],cur[MAXN];

void init()
{
    tol=0;
    memset(head,-1,sizeof(head));
}

//加边,单向图三个参数,双向图四个参数
void addedge(int u,int v,int w,int rw=0)
{
    edge[tol].to=v; edge[tol].cap=w; edge[tol].next=head[u];
    edge[tol].flow=0; head[u]=tol++;
    edge[tol].to=u; edge[tol].cap=rw; edge[tol].next=head[v];
    edge[tol].flow=0; head[v]=tol++;
}

//输入参数:起点,终点,点的总数
//点的编号没有影响,只要输入点的总数
int sap(int start,int end,int N)
{
    memset(gap,0,sizeof(gap));
    memset(dep,0,sizeof(dep));
    memcpy(cur,head,sizeof(head));
    int u=start;
    pre[u]=-1;
    gap[0]=N;
    int ans=0;
    while (dep[start]<N)
    {
        if (u==end)
        {
            int Min=INF;
            for (int i=pre[u];i!=-1;i=pre[edge[i^1].to])
                if (Min>edge[i].cap-edge[i].flow)
                    Min=edge[i].cap-edge[i].flow;
            for (int i=pre[u];i!=-1;i=pre[edge[i^1].to])
            {
                edge[i].flow+=Min;
                edge[i^1].flow-=Min;
            }
            u=start;
            ans+=Min;
            continue;
        }
        bool flag=false;
        int v;
        for (int i=cur[u];i!=-1;i=edge[i].next)
        {
            v=edge[i].to;
            if (edge[i].cap-edge[i].flow && dep[v]+1==dep[u])
            {
                flag=true;
                cur[u]=pre[v]=i;
                break;
            }
        }
        if (flag)
        {
            u=v;
            continue;
        }
        int Min=N;
        for (int i=head[u];i!=-1;i=edge[i].next)
            if (edge[i].cap-edge[i].flow && dep[edge[i].to]<Min)
            {
                Min=dep[edge[i].to];
                cur[u]=i;
            }
        gap[dep[u]]--;
        if (!gap[dep[u]]) return ans;
        dep[u]=Min+1;
        gap[dep[u]]++;
        if (u!=start) u=edge[pre[u]^1].to;
    }
    return ans;
}
void floyd()   //floyd求最短路
{
    maxxx=-1;
    for(int k=1;k<=f;k++){
        for(int i=1;i<=f;i++){
            for(int j=1;j<=f;j++){
                if(mp[i][k]+mp[k][j]<mp[i][j]) mp[i][j]=mp[i][k]+mp[k][j];
            }
        }
    }
    for(int i=1;i<=f;i++){   //找出最短路中的最大值
        for(int j=1;j<=f;j++){
            if(mp[i][j]!=INF && mp[i][j]>maxxx) maxxx=mp[i][j];
        }
    }
}

void Build_Graph(ll mid)  //建图(邻接表)
{
    int i,j;
    init();
    FRE(i,1,f){
        addedge(s,i,date[i][0]);
        addedge(f+i,t,date[i][1]);
    }
    FRE(i,1,f)
    FRE(j,1,f)
    if (mp[i][j]<=mid)
        addedge(i,j+f,inf);
}

bool ok(ll mid) //二分判断
{
    Build_Graph(mid);
    int ans=sap(s,t,t+1);
    if (ans>=sum) return true;
    return false;
}

void solve()  //二分
{
    int i,j;
    ll l=0,r=maxxx,ans=-1;
    while (l<=r)
    {
        ll mid=(l+r)/2;
        if (ok(mid)) {
                r=mid-1;
                ans=mid;
        }
        else l=mid+1;
    }
    printf("%lld\n",ans);
}

int main()
{
    int i,j;
    while(~scanf("%d%d",&f,&p))
    {
        sum=0;
        for(i=1;i<=f;i++){
            for(j=1;j<=f;j++)
                mp[i][j]=INF;
            mp[i][i]=0;
        }
        int u,v;
        ll w;
        s=0,t=2*f+1;
        FRE(i,1,f)
        {
            scanf("%d%d",&date[i][0],&date[i][1]);
            sum+=date[i][0];
        }
        FRE(i,1,p)
        {
            scanf("%d%d%lld",&u,&v,&w);
            if (w<mp[u][v])
                mp[u][v]=mp[v][u]=w;
        }
        floyd();
        solve();
    }
    return 0;
}
/*
3 4
7 2
0 4
2 6
1 2 40
3 2 70
2 3 90
1 3 120
4 3
4 0
2 2
0 2
0 2
1 2 2
2 3 2
2 4 2
*/
时间: 2025-01-08 10:40:34

Ombrophobic Bovines (poj 2391 网络流+二分+Floyd)的相关文章

Ombrophobic Bovines - POJ 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 cr

Optimal Milking (poj 2112 网络流+二分+floyd)

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

POJ 3228 网络流+二分&amp;并查集

点击打开链接 题意:有n个城镇,第一行是金矿和金子数量,然后第二行是装金子的地方和能装的数量,在下面是m条道路,问你选择的道路中最大值最小,使得所有金子运到装金子的地方 思路:最大值最小,根本不用考虑一看就是二分,然后想了想就是个网络流的模型嘛,很简单,被坑了几次道路是双向的,改过之后A掉,然后看了看讨论还可以用并查集写,这里两种方法都写了,先是网络流的直接二分最大值,然后满足条件的边建模型,跑一边就行了 #include <queue> #include <vector> #in

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 不喜欢雨的奶牛 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 (二分,最短路径,网络流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+二分)

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

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, 最大流, 拆点, 二分, 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