poj 2391 Ombrophobic Bovines(二分枚举+floyd+最大流+拆点)

46 篇文章 0 订阅
Ombrophobic Bovines
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12743 Accepted: 2806

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.


题意:有n块土地,每块土地都有一个牛棚,牛棚各自都有一个可以容纳牛的最大的数目,下雨时牛要到牛棚里避雨。现在给出n块土地里的牛的数目、牛棚能容纳的牛的数目、土地之间的每条路径所花费的时间,求要使所有牛都能到牛棚里避雨,所用的最小时间。

思路:先用floyd预处理每两块土地之间的最短路径。最后答案肯定是需要通过的路中最长的那个时间,可以用二分最大流得出。这题注意的是要拆点,为什么要拆点?如果不拆点,比如1到2是40,2到3是70,这样当mid值是70时,1的某个牛可以跑到2,然后再跑到3,求出的最大流就被认为是可行的,但这是不可行的,因为实际上跑了110。  还有就是注意要用long long ,而且不存在的边不能设为INF,因为存在的边的长度可能比INF还要大。

AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <cmath>
#include <cstdlib>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define ll __int64
#define eps 1e-6

using namespace std;

const int INF = 1000000007;
const int maxn = 500;
struct Edge{
    int u, v, cap, flow, next;
}et[maxn*maxn*100];
struct node{
    int cow,cap;
}field[maxn];
int cnt[maxn], pre[maxn], cur[maxn], eh[maxn], dis[maxn], low[maxn];
ll G[maxn][maxn],maxe;
int n, m, s, t, num;
void init(){
    memset(eh, -1, sizeof(eh));
    num = 0;
}
void add(int u, int v ,int cap, int flow){
    Edge e = {u, v, cap, flow, eh[u]};
    et[num] = e;
    eh[u] = num++;
}
void addedge(int u, int v, int cap){
    add(u, v, cap, 0);
    add(v, u, 0, 0);
}
void build_graph(ll d)
{
    init();
    for(int i = 1; i <= n; i++)
    {
        addedge(i, i + n, INF);
        addedge(s, i, field[i].cow);
        addedge(i+n, t, field[i].cap);
    }
    for(int i = 1; i <= n; i++)
    for(int j = i + 1; j <= n; j++)
    if(G[i][j] <= d && G[i][j]) {
        addedge(i, j+n, INF);
        addedge(j, i+n, INF);
    }
}
int isap(int s, int t, int nv)
{
    int u, v, now, flow = 0;
    memset(low, 0, sizeof(low));
    memset(dis, 0, sizeof(dis));
    memset(cnt, 0, sizeof(cnt));
    for(u = 0; u <= nv; u++) cur[u] = eh[u];
    low[s] = INF, cnt[0] = nv, u = s;
    while(dis[s] < nv)
    {
        for(now = cur[u]; now != -1; now = et[now].next)
        if(et[now].cap - et[now].flow && dis[u] == dis[v = et[now].v] + 1) break;
        if(now != -1)
        {
            cur[u] = pre[v] = now;
            low[v] = min(et[now].cap - et[now].flow, low[u]);
            u = v;
            if(u == t)
            {
                for(; u != s; u = et[pre[u]].u)
                {
                    et[pre[u]].flow += low[t];
                    et[pre[u]^1].flow -= low[t];
                }
                flow += low[t];
                low[s] = INF;
            }
        }
        else
        {
            if(--cnt[dis[u]] == 0) break;
            dis[u] = nv, cur[u] = eh[u];
            for(now = eh[u]; now != -1; now = et[now].next)
            if(et[now].cap - et[now].flow && dis[u] > dis[et[now].v] + 1)
            dis[u] = dis[et[now].v] + 1;
            cnt[dis[u]]++;
            if(u != s) u = et[pre[u]].u;
        }
    }
    return flow;
}
void floyd()
{
    for(int k = 1; k <= n; k++)
    for(int i = 1; i <= n; i++)
    for(int j = 1; j <= n; j++)
    if(G[i][k] && G[k][j])
    {
        if(!G[i][j] || G[i][j] > G[i][k] + G[k][j])
        G[i][j] = G[i][k] + G[k][j];
        maxe = max(maxe, G[i][j]);
    }
}
int main()
{
    int a,b;
    ll c;
    while(~scanf("%d%d", &n, &m))
    {
        s = 0;
        t = 2 * n + 1;
        int sum = 0;
        memset(G, 0 ,sizeof(G));
        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d", &field[i].cow, &field[i].cap);
            sum += field[i].cow;
        }
        while(m--)
        {
            scanf("%d%d%I64d",&a, &b, &c);
            if(!G[a][b] || c < G[a][b])
            G[a][b] = G[b][a] = c;
        }
        maxe = 0;
        floyd();
        ll low = 0, high = maxe + 1, ans = -1, mid;
        while(low <= high)
        {
            mid = (low + high)>>1;
            build_graph(mid);
            if(isap(s, t, t+1) == sum)
            {
                ans = mid;
                high = mid -1;
            }
            else low = mid + 1;
        }
        printf("%I64d\n", ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值