【poj2391】Ombrophobic Bovines 二分+最大流+floyd

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


题意纠结了半天…

每个点有a[i]个奶牛,每个点有可以容纳b[i]个奶牛,点之间的边的边权表示经过所需时间,所有的牛同时移动,求所有的牛被容纳的最小时间。

二分答案mid,拆点构建模型,源点连出点,容量为a[i],入点连汇点,容量为b[i],若< i,j >最短路小于等于mid,则i的出点向b的入点连边,然后最大流判断是否等于 a[i]

正确性显然。

不要忘了开longlong!不要忘了判无解!
不要忘了开longlong!不要忘了判无解!
不要忘了开longlong!不要忘了判无解!
重要的事情说三遍

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;

typedef long long LL;

const LL INF = 10000000000010;
const int SZ = 100010;

int head[SZ],nxt[SZ],tot = 1,s,e;
int n,m;
LL sum = 0;

struct edge{
    int t;
    LL d;
}l[SZ];

void build(int f,int t,LL d)
{
    l[++ tot].t = t;
    l[tot].d = d;
    nxt[tot] = head[f];
    head[f] = tot;
}

void insert(int f,int t,LL d)
{
    build(f,t,d); build(t,f,0);
}

int deep[SZ];
queue<int> q;

bool bfs()
{
    memset(deep,0,sizeof(deep));
    deep[s] = 1;
    while(q.size()) q.pop();
    q.push(s);
    while(q.size())
    {
        int f = q.front(); q.pop();
        for(int i = head[f];i;i = nxt[i])
        {
            int v = l[i].t;
            if(!deep[v] && l[i].d)
            {
                deep[v] = deep[f] + 1;
                q.push(v);
                if(v == e) return true;
            }
        }
    }
    return false;
}


LL dfs(int u,LL flow)
{
    if(u == e || flow == 0) return flow;
    LL rest = flow;
    for(int i = head[u];i;i = nxt[i])
    {
        int v = l[i].t;
        if(deep[v] == deep[u] + 1 && l[i].d)
        {
            LL f = dfs(v,min(rest,l[i].d));
            if(f > 0)
            {
                l[i].d -= f;
                l[i ^ 1].d += f;
                rest -= f;
                if(rest == 0) break;
            }
            else deep[v] = 0;
        }
    }
    if(flow == rest) deep[u] = 0;
    return flow - rest;
}

LL dinic()
{
    LL ans = 0;
    while(bfs())
    {
        LL tmp = dfs(s,INF);
        if(!tmp) break;
        ans += tmp;
    }
    return ans;
}

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

int a[SZ],b[SZ];
LL dist[233][233];

bool check(LL mid)
{
    init();
    for(int i = 1;i <= n;i ++)
    {
        insert(s,i,a[i]); insert(i + n,e,b[i]);
    }
    for(int i = 1;i <= n;i ++)
        for(int j = 1;j <= n;j ++)
            if(dist[i][j] <= mid)
            {
                insert(i,j + n,INF);
            //  insert(j,i + n,INF);
            }
    return dinic() == sum;
}

LL div()
{
    LL l = -1,r = INF;
    while(r - l > 1)
    {
        LL mid = (l + r) >> 1;
        if(check(mid)) r = mid;
        else l = mid;
    }
    return r == INF ? -1 : r;
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i = 1;i <= n;i ++)
    {
        scanf("%d%d",&a[i],&b[i]);
        sum += a[i];
    }
    memset(dist,63,sizeof(dist));
    for(int i = 1;i <= n;i ++) dist[i][i] = 0;
    for(int i = 1;i <= m;i ++)
    {
        int x,y;
        LL z;
        scanf("%d%d%lld",&x,&y,&z);
        dist[x][y] = dist[y][x] = min(dist[x][y],z);
    }
    for(int k = 1;k <= n;k ++)
        for(int i = 1;i <= n;i ++)
            for(int j = 1;j <= n;j ++)
                dist[i][j] = min(dist[i][j],dist[i][k] + dist[k][j]);
    s = n * 2 + 1,e = n * 2 + 2;
    printf("%lld",div());
    return 0;   
}
/*
4 4
7 2
0 4
2 6
1 0
1 2 40
3 2 70
2 3 90
1 3 120
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值