首页 > 代码库 > POJ 2391 Ombrophobic Bovines 不喜欢雨的奶牛 Floyd+二分枚举+最大流

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 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条路相连,路足够宽,可以同时通过无限头奶牛。有些草地有避雨点,奶牛们可以在此避雨。避雨点容量有限,一个避雨点不能容纳所有奶牛。草地与路相比很小,奶牛通过草地时不需要时间。求所有奶牛都回到避雨点所花的最小时间。如果不能保证都能到避雨点,则输出-1。

分析:

这道题和POJ 2112挺像。不过要难一些。

首先需要用Floyd算出所有点之间的最短距离。然后二分枚举距离求最大流,求到的满足最大流等于牛头数的那个最小的距离即为答案。

但是出现了一个问题,结点现在也有了容量,因而我们需要拆点,把一个原始结点u分裂成u1和u2两个结点,中间连一条有向弧,容量等于结点容量。原先到达u的弧改成到达u1,原先从u出发的弧改成u2出发。

构图:

找一个源点s,一个汇点t,s和每个草地连一条边,容量为草地上的牛数,每个避雨点和t连一条边,容量为避雨点容纳牛头数。

拆点问题如上所述。

如果一块草地到另一块草地有最短路,且这个最短路小于当前枚举到的路径长度,那么这两块草地连一条边,容量为无穷,因为可以走无限头。

求解枚举满足最小值。

代码:

写了好多遍都T了,最后把vector和queue都换成了普通数组,然后过了,只给了1秒,实在太坑了。

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

#define maxn 410
#define maxm 80080
#define INF 0x3f3f3f3f
#define LL long long

struct Edge
{
    int from, to, cap;
}EG[maxm];

//vector<Edge> EG;
int G[maxn][maxm];
int n, f, p, s, t, sum, cow[maxn], bi[maxn], d[maxn], cur[maxn], cntE;
LL mp[maxn][maxn];
int cntg[maxn];
int que[2*maxn];
void addEdge(int from, int to, int cap)
{
    EG[cntE].from = from;
    EG[cntE].to = to;
    EG[cntE].cap = cap;
    cntE++;
    EG[cntE].from = to;
    EG[cntE].to = from;
    EG[cntE].cap = 0;
    cntE++;
    G[from][cntg[from]] = cntE-2;
    cntg[from]++;
    G[to][cntg[to]] = cntE-1;
    cntg[to]++;
}

bool bfs()
{
    memset(d, -1, sizeof(d));
    que[0] = s;
    d[s] = 0;
    int tt = 1, ff = 0;
    while(ff < tt)
    {
        int x = que[ff++];
        for(int i = 0; i < cntg[x]; i++)
        {
            Edge& e = EG[G[x][i]];
            if(d[e.to] == -1 && e.cap > 0)
            {
                d[e.to] = d[x]+1;
                que[tt++] = e.to;
            }
        }
    }
    return (d[t]!=-1);
}

int dfs(int x, int a)
{
    if(x == t || a == 0) return a;
    int flow = 0, f;
    for(int& i = cur[x]; i < cntg[x]; i++)
    {
        Edge& e = EG[G[x][i]];
        if(d[x]+1 == d[e.to] && (f = dfs(e.to, min(a, e.cap))) > 0)
        {
            e.cap -= f;
            EG[G[x][i]^1].cap += f;
            flow += f;
            a -= f;
            if(a == 0) break;
        }
    }
    return flow;
}
int Dinic()
{
    int ans = 0;
    while(bfs())
    {
        memset(cur, 0, sizeof(cur));
        ans += dfs(s, INF);
    }
    return ans;
}

void Floyd()
{
    for(int k = 1; k <= f; k++)
        for(int i = 1; i <= f; i++)
            for(int j = 1; j <= f; j++)
                mp[i][j] = min(mp[i][j], mp[i][k]+mp[k][j]);
}

void build(LL mid)
{
    cntE = 0;
    memset(cntg, 0, sizeof(cntg));
    for(int i = 1; i <= f; i++)
    {
        if(cow[i] != 0)
            addEdge(s, i, cow[i]);	// s和每个草地连边
        if(bi[i] != 0)
            addEdge(i+f, t, bi[i]);	// 每个草地的拆点和避雨点连边
    }
    for(int i = 1; i <= f; i++)
        for(int j = 1; j <= f; j++)
            if(mp[i][j] <= mid)
                addEdge(i, j+f, INF);	// 草地和草地连边
}

void solve()
{
    LL l = 0, r = 1LL*INF*INF, mid, ans = -1;	// 二分枚举,注意长度尽可能大
    while( l <= r)
    {
        mid = (l+r)>>1ll;
        build(mid);			// 构图
        int k = Dinic();	// 求最大流
        if(sum == k) ans = mid, r = mid-1;
        else l = mid+1;
    }
    printf("%lld\n", ans);
}

int main()
{
    //freopen("poj_2391.txt", "r", stdin);
    int u, v;
    LL w;
    scanf("%d%d", &f, &p);
    sum = 0;
    s = 0; t = 2*f+1;
    n = 2*f+2;
    for(int i = 1; i <= f; i++)
    {
        scanf("%d%d", &cow[i], &bi[i]);
        sum += cow[i];
    }
    memset(mp, INF, sizeof(mp));	// 设一个尽量大的值,但其实不是INF, memset还是挺管用的,字节填充
    for(int i = 1; i <= p; i++)
    {
        scanf("%d%d%lld", &u, &v, &w);
        if(mp[u][v] > w)
            mp[u][v] = mp[v][u] = w;
    }
    for(int i = 0; i <= 2*f; i++)	//注意自己是可以到自己的
        mp[i][i] = 0;
    Floyd();		//求最短路
    solve();
    return 0;
}


T了好多次,把STL删掉越来越好,也是醉了:

Run IDUserProblemResultMemoryTimeLanguageCode LengthSubmit Time
13481032acmer2391Accepted5112K610MSG++3159B2014-09-26 22:37:24
13480087acmer2391Accepted5112K625MSG++3203B2014-09-26 17:43:11
13480059acmer2391Accepted5136K641MSG++3215B2014-09-26 17:33:17
13480007acmer2391Accepted5240K704MSG++3199B2014-09-26 17:20:58
13479803acmer2391Accepted3344K922MSG++2349B2014-09-26 16:39:45



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