POJ2391_Ombrophobic Bovines

有F个地方,每个地方有一定数量的牛,能够容纳一定数量的牛,某些地方之间有边,表示走两点之间需要消耗的时间。

现在求使得所有的牛都被容纳所需要的最少的时间。

由于时间是一个不确定的因素,我们需要二分。

假设当前二分的时间为t,那么从某一点出发距离不要超过t的点都是可以连边的,于是最后只需要跑最大流验证是否满流即可。

此题给的数据居然爆int,真是深坑啊。

召唤代码君:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#define maxn 1122
#define maxm 555555
typedef long long ll;
using namespace std;

ll inf=~0U>>2;
ll to[maxm],next[maxm],c[maxm],first[maxn],edge;
ll cow[maxn],hold[maxn],d[maxn],tag[maxn],TAG=520;
ll Q[maxm],bot,top;
ll dis[maxn][maxn],f[maxn],g[maxn][maxn];
bool can[maxn];
ll n,m,sumcow,ans,s,t;

void _init()
{
    sumcow=0;
    for (int i=1; i<=n; i++)
        for (int j=1; j<=n; j++) dis[i][j]=-1;
}

void spfa(ll x)
{
    for (int i=1; i<=n; i++) f[i]=inf;
    Q[bot=top=1]=x,f[x]=0;
    while (bot<=top)
    {
        ll cur=Q[bot++];
        for (int i=1; i<=n; i++)
            if (dis[cur][i]!=-1 && f[cur]+dis[cur][i]<f[i])
            {
                Q[++top]=i;
                f[i]=f[cur]+dis[cur][i];
            }
    }
    for (int i=1; i<=n; i++) g[x][i]=f[i];
}

void _input()
{
    ll U,V,W;
    for (int i=1; i<=n; i++) scanf("%I64d%I64d",&cow[i],&hold[i]),sumcow+=cow[i];
    while (m--)
    {
        scanf("%I64d%I64d%I64d",&U,&V,&W);
        if (dis[U][V]==-1) dis[U][V]=dis[V][U]=W;
            else if (W<dis[U][V]) dis[U][V]=dis[V][U]=W;
    }
    for (int i=1; i<=n; i++) if (cow[i]>0) spfa(i);
}

void addedge(ll U,ll V,ll W)
{
    edge++;
    to[edge]=V,c[edge]=W,next[edge]=first[U],first[U]=edge;
    edge++;
    to[edge]=U,c[edge]=0,next[edge]=first[V],first[V]=edge;
}

bool bfs()
{
    Q[bot=top=1]=t,d[t]=0,tag[t]=++TAG;
    while (bot<=top)
    {
        ll cur=Q[bot++];
        for (int i=first[cur]; i!=-1; i=next[i])
        {
            if (c[i^1]>0 && tag[to[i]]!=TAG)
            {
                tag[to[i]]=TAG,Q[++top]=to[i];
                d[to[i]]=d[cur]+1,can[to[i]]=false;
                if (to[i]==s) return true;
            }
        }
    }
    return false;
}

ll dfs(ll cur,ll num)
{
    if (cur==t) return num;
    ll tmp=num,k;
    for (int i=first[cur]; i!=-1; i=next[i])
        if (c[i]>0 && d[to[i]]==d[cur]-1 && tag[to[i]]==TAG && !can[to[i]])
        {
            k=dfs(to[i],min(c[i],num));
            if (k) num-=k,c[i]-=k,c[i^1]+=k;
            if (num==0) break;
        }
    if (num) can[cur]=true;
    return tmp-num;
}

bool check(ll x)
{
    s=0,t=2*n+1,edge=-1,ans=0;
    for (int i=s; i<=t; i++) first[i]=-1;
    for (int i=1; i<=n; i++)
    {
        if (cow[i]>0)
        {
            addedge(s,i,cow[i]);
            for (int j=1; j<=n; j++)
                if (g[i][j]<=x) addedge(i,j+n,cow[i]);
        }
        addedge(i+n,t,hold[i]);
    }
    while (bfs()) ans+=dfs(s,inf);
    return ans==sumcow;
}

int main()
{
    inf*=inf;
    while (scanf("%I64d%I64d",&n,&m)!=EOF)
    {
        _init();
        _input();
        if (!check(inf-1))
        {
            puts("-1");
            continue;
        }
        ll l=0,r=inf-2,mid;
        while (l<r)
        {
            mid=l/2+r/2;
            if (l&r&1) mid++;
            if (check(mid)) r=mid;
                else l=mid+1;
        }
        printf("%I64d\n",l);
    }
    return 0;
}

POJ2391_Ombrophobic Bovines

时间: 2024-08-25 18:04:14

POJ2391_Ombrophobic Bovines的相关文章

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

BZOJ 1738: [Usaco2005 mar]Ombrophobic Bovines 发抖的牛( floyd + 二分答案 + 最大流 )

一道水题WA了这么多次真是.... 统考终于完 ( 挂 ) 了...可以好好写题了... 先floyd跑出各个点的最短路 , 然后二分答案 m , 再建图. 每个 farm 拆成一个 cow 点和一个 shelter 点, 然后对于每个 farm x : S -> cow( x ) = cow( x ) 数量 , shelter( x ) -> T = shelter( x ) 容量 ; 对于每个dist( u , v ) <= m 的 cow( u ) -> shelter( v

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 th

POJ2391 Ombrophobic Bovines(网络流)(拆点)

Ombrophobic Bovines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18205   Accepted: 3960 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 2391Ombrophobic Bovines 【蛮简单的网络流】

...第一个网络流的题目 牛淋雨什么的,建图,用模板之 #include <cstdio> #include <cstdlib> #include <iostream> #include <algorithm> #include <cstring> #include <cmath> #include <vector> using namespace std; long long F,P; long long cowsum;

POJ 2391-Ombrophobic Bovines(网络流_最大流+floyd+二分)

Ombrophobic Bovines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15485   Accepted: 3361 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 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【拆点+Floyd+Dinic优化+二分答案】网络最大流

链接:http://poj.org/problem?id=2391 题意:有f个草场,每个草场当前有一定数目的牛在吃草,下雨时它可以让一定数量的牛在这里避雨,f个草场间有m条路连接,每头牛通过一条路从一点到另一点有一定的时间花费,现在要下雨了,农场主发出警报牛就会立即去避雨.现在告诉每个草场的情况,以及m条边的信息.农场主至少需要提前多久发出警报才能保证所有牛都能避雨?如果不是所有牛都能成功避雨输出-1. 思路:这道题需要拆点,是看到魏神的博客才知道的,我们把原图拆成一个二分图,避免突破最大距离

POJ 2391Ombrophobic Bovines(二分+最短路+网络流之最大流)

题目地址:http://poj.org/problem?id=2391 这个题WA了一晚上,原因是数组开小了,然后又TLE了一天,原因是数组改的过大了....不多说什么了... 思路不难,建图也不难,二分时间,然后把每个田地之间的最短距离用floyd最短路求出来.然后建立一个源点与汇点,将田地拆分成两个点,在距离之内的进行连边,要单向连边.然后将源点与田地相连,权值为每个田地的牛的数目,再把另一边的田地与汇点相连,权值为每个田地最大可避雨的牛的数目.拆开的田地之间权值可以为无穷大. 代码如下: