POJ 2391 floyd二分+拆点+最大流

Ombrophobic Bovines

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 20904   Accepted: 4494

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

解析 先求一遍两点之间的最短距离  然后二分答案mid,每次二分的时候构建一个网络 两点之间的距离<=mid 连一条有向边 不过要拆点 保证使它是单向的,避免不可达的可达,

跑一边最大流 如果等于牛的总数 说明mid时间内可以的到达 继续二分 出最优答案。

我为什么感觉可以费用流解决。。。有时间试一试

#include<iostream>
#include<stdio.h>
#include<vector>
#include<string.h>
#include<queue>
using namespace std;
typedef long long ll;
const int maxn=1e3+20,mod=1e9+7;
const ll inf=1e16;
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define huan printf("\n");
#define debug(a,b) cout<<a<<" "<<b<<" ";
ll min(ll a,ll b){return a>b?b:a;}
struct edge
{
    int from,to,c,f;
    edge(int u,int v,int c,int f):from(u),to(v),c(c),f(f) {}
};
int n,m,N;
vector<edge> edges;
vector<int> g[maxn];
int d[maxn];//从起点到i的距离
int cur[maxn];//当前弧下标
ll dp[maxn][maxn];
ll a[maxn],b[maxn],sum;
void init(int n)
{
    for(int i=0; i<=N; i++) g[i].clear();
    edges.clear();
}
void addedge(int from,int to,int c) //加边 支持重边
{
    edges.push_back(edge(from,to,c,0));
    edges.push_back(edge(to,from,0,0));
    int siz=edges.size();
    g[from].push_back(siz-2);
    g[to].push_back(siz-1);
}
int bfs(int s,int t) //构造一次层次图
{
    memset(d,-1,sizeof(d));
    queue<int> q;
    q.push(s);
    d[s]=0;
    while(!q.empty())
    {
        int x=q.front();q.pop();
        for(int i=0;i<g[x].size();i++)
        {
            edge &e=edges[g[x][i]];
            if(d[e.to]<0&&e.f<e.c) //d[e.to]=-1表示没访问过
            {
                d[e.to]=d[x]+1;
                q.push(e.to);
            }
        }
    }
    return d[t];
}
int dfs(int x,int a,int t) // a表示x点能接收的量
{
    if(x==t||a==0)return a;
    int flow=0,f;//flow总的增量 f一条增广路的增量
    for(int &i=cur[x];i<g[x].size();i++)//cur[i] &引用修改其值 从上次考虑的弧
    {
        edge &e=edges[g[x][i]];
        if(d[x]+1==d[e.to]&&(f=dfs(e.to,min(a,e.c-e.f),t))>0)    //按照层次图增广 满足容量限制
        {
            e.f+=f;
            edges[g[x][i]^1].f-=f;  //修改流量
            flow+=f;
            a-=f;
            if(a==0) break;
        }
    }
    return flow;
}
int maxflow(int s,int t)
{
    int flow=0;
    while(bfs(s,t)!=-1)
    {
        memset(cur,0,sizeof(cur));
        flow+=dfs(s,0x3f3f3f3f,t);
    }
    return flow;
}
void build(ll x)
{
    init(N);
    for(int i=1;i<=n;i++)
    {
        addedge(0,i,a[i]);
        addedge(i+n,N,b[i]);
        addedge(i,i+n,0x3f3f3f3f);
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            if(dp[i][j]<=x)
            {
                addedge(i,j+n,0x3f3f3f3f);
                addedge(j,i+n,0x3f3f3f3f);
            }
        }
    }
}
ll solve()
{
    ll ans=-1;
    ll l=0,r=inf-1;
    while(l<=r)
    {
        ll mid=(l+r)/2;
        build(mid);
        int temp=maxflow(0,N);
        //cout<<mid<<" "<<temp<<endl;
        if(temp>=sum)
        {
            ans=mid;
            r=mid-1;
        }
        else l=mid+1;
    }
    return ans;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        N=n*2+1;sum=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%ld%ld",&a[i],&b[i]);
            sum+=a[i];
        }
        //====================floyd==========================//
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)  //初始化长度
            {
                if(i==j)
                    dp[i][j]=0;
                else
                    dp[i][j]=inf;
            }
        ll x,y,d;
        for(int i=0;i<m;i++)
        {
            scanf("%lld%lld%lld",&x,&y,&d);
            if(dp[x][y]>d)
                dp[x][y]=dp[y][x]=d;
        }
        for(int k=1; k<=n; k++)
            for(int i=1; i<=n; i++)
                if(dp[i][k]!=inf)
                    for(int j=1; j<=n; j++)
                        dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]);
        //=========================================================//
        //init(n);
        cout<<solve()<<endl;
    }
}

原文地址:https://www.cnblogs.com/stranger-/p/9368406.html

时间: 2024-11-12 12:23:13

POJ 2391 floyd二分+拆点+最大流的相关文章

POJ2391.Ombrophobic Bovines(不喜欢雨的奶牛)——floyd+二分+拆点+最大流

http://poj.org/problem?id=2391 写的挫的最大流会超时~~~ 题目描述: Jack 农场主的奶牛实在是太讨厌被淋湿了.决定在农场设置降雨警报,这样在快要下 雨的时候可以让奶牛们都知道.他们设置设计了一个下雨撤退计划,这样在下雨之前每头奶牛都 能躲到避雨点.然而,天气预报并不总是准确的.为了使得错误的天气预报影响尽可能小,他们 希望尽可能晚地拉响警报,只要保证留有足够的时间让所有的奶牛都能回到避雨点就可以了. 农场有F 块草地,1≤F≤200,奶牛们在草地上吃草.这些草

UVAlive--4529--Dangerous Tunnels(二分+拆点最大流)

 Dangerous Tunnels Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Somewhere in the world, there are two tribes separated by mountains. The two tribes are named Kulolo and Gulolo, respectively, wh

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

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

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

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