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 ID User Problem Result Memory Time Language Code Length Submit Time
13481032 acmer 2391 Accepted 5112K 610MS G++ 3159B 2014-09-26 22:37:24
13480087 acmer 2391 Accepted 5112K 625MS G++ 3203B 2014-09-26 17:43:11
13480059 acmer 2391 Accepted 5136K 641MS G++ 3215B 2014-09-26 17:33:17
13480007 acmer 2391 Accepted 5240K 704MS G++ 3199B 2014-09-26 17:20:58
13479803 acmer 2391 Accepted 3344K 922MS G++ 2349B 2014-09-26 16:39:45
时间: 2024-08-06 23:37:48

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

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 + 网络流)

POJ 2391 Ombrophobic Bovines 链接:http://poj.org/problem?id=2391 题目:农场有F 块草地,1≤F≤200,奶牛们在草地上吃草.这些草地之间有P 条路相连,1≤P≤1500,这些路足够宽,再多的奶牛也能同时在路上行走.有些草地上有避雨点,奶牛们可以在此避雨.避雨点的容量是有限的,所以一个避雨点不可能容纳下所有的奶牛.草地与路相比很小,奶牛们通过时不需要花费时间.计算警报至少需要提前多少时间拉响,以保证所有的奶牛都能到达一个避雨点. 思路:

POJ 2391 Ombrophobic Bovines(最大流+拆点)

POJ 2391 Ombrophobic Bovines 题目链接 题意:一些牛棚,有a只牛,现在下雨,每个牛棚容量量变成b,现在有一些道路连接了牛棚,问下雨后牛走到其他牛棚,使得所有牛都有地方躲雨,最后一只牛要走多久 思路:二分答案,然后最大流去判断,建图的方式为,牛棚拆点,源点连向入点,容量为a,出点连向汇点容量为b,中间入点和出点之间根据二分的值判断哪些边是可以加入的 代码: #include <cstdio> #include <cstring> #include <

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

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

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

poj 2391 Ombrophobic Bovines 二分+最大流

同poj 2112. 代码: //poj 2391 //sep9 #include <iostream> #include <queue> #include <algorithm> using namespace std; typedef long long ll; const int maxN=1024; const int maxM=100002; const ll MAX=(1ULL<<63)-1; struct Edge { int v,f,nxt;

POJ 2391 Ombrophobic Bovines(二分+拆点+最大流)

http://poj.org/problem?id=2391 题意: 给定一个无向图,点i处有Ai头牛,点i处的牛棚能容纳Bi头牛,求一个最短时间T,使得在T时间内所有的牛都能进到某一牛棚里去. 思路: 建立一个源点和汇点,源点和牛棚的初始牛量相连,汇点和牛棚容量相连.这样跑最大流,如果最后流量等于牛的总数时,就说明是可以的. 那么,怎么连边呢?二分时间,根据时间来连边,所以首先我们先跑一遍floyd计算出两点距离.然后在该时间下,如果d[i][j],那么就添加边(i,i',INF),表面这段路