BZOJ 1738 && POJ 2391 Ombrophobic Bovines

题目链接http://poj.org/problem?id=2391

题意:约翰的牛们非常害怕淋雨,那会使他们瑟瑟发抖.他们打算安装一个下雨报警器,并且安排了一个撤退计划.他们需要计算最少的让所有牛进入雨棚的时间.牛们在农场的F(1 <= F <= 200)个田地上吃草.有P(1<= P<= 1500)条双向路连接着这些田地.路很宽,无限量的牛可以通过.田地上有雨棚,雨棚有一定的容量,牛们可以瞬间从这块田地进入这块田地上的雨棚.请计算最少的时间,让每只牛都进入雨棚.

思路:先跑下floyd求得任意两点间的最短距离,然后二分时间,把每个点拆成两个点,源点向其中一个点连一条容量为牛的数量的边,另一个点向汇点连一条容量为牛棚容量的边,然后对于中间的每一条边,如果(i,j)它们的距离小于等于当前二分的时间就连一条边然后跑最大流看是否满流即可。

拆点依据

#pragma comment(linker, "/STACK:102400000,102400000")
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
#define Key_Value ch[ch[root][1]][0]
#define DBN1(a)           cerr<<#a<<"="<<(a)<<"\n"
#define DBN2(a,b)         cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define DBN3(a,b,c)       cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define DBN4(a,b,c,d)     cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define DBN5(a,b,c,d,e)   cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"
#define DBN6(a,b,c,d,e,f) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<", "<<#f<<"="<<(f)<<"\n"
#define clr(a,x) memset(a,x,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=2000+5;
const ll INF=2000000000000000000LL;
const int P=1000000007;
const double PI=acos(-1.0);
template<typename T>
inline T read(T&x){
    x=0;int _f=0;char ch=getchar();
    while(ch<'0'||ch>'9')_f|=(ch=='-'),ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x=_f?-x:x;
}
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
struct Edge{
    int from,to,cap,flow;
    Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
#define inf 1000000000
struct Dinic{
    int n,m,s,t;
    vector<Edge>edges;
    vector<int>G[maxn];
    bool vis[maxn];
    int d[maxn];
    int cur[maxn];

    void init(int n,int s,int t){
        this->n=n,this->s=s,this->t=t;
        for (int i=0;i<=n;i++) G[i].clear();
        edges.clear();
    }

    void AddEdge(int from,int to,int cap){
        edges.push_back(Edge(from,to,cap,0));
        edges.push_back(Edge(to,from,0,0));
        m=edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }

    bool BFS(){
        memset(vis,0,sizeof(vis));
        queue<int>Q;
        Q.push(s);
        d[s]=0;
        vis[s]=1;
        while (!Q.empty()){
            int x=Q.front();Q.pop();
            for (int i=0;i<(int)G[x].size();i++){
                Edge& e=edges[G[x][i]];
                if (!vis[e.to] && e.cap>e.flow){
                    vis[e.to]=1;
                    d[e.to]=d[x]+1;
                    Q.push(e.to);
                }
            }
        }
        return vis[t];
    }

    int DFS(int x,int a){
        if (x==t || a==0) return a;
        int flow=0,f;
        for (int& i=cur[x];i<(int)G[x].size();i++){
            Edge& e=edges[G[x][i]];
            if (d[x]+1==d[e.to] && (f=DFS(e.to,min(a,e.cap-e.flow)))>0){
                e.flow+=f;
                edges[G[x][i]^1].flow-=f;
                flow+=f;
                a-=f;
                if (a==0) break;
            }
        }
        return flow;
    }

    bool solve(int tot){
        int flow=0;
        while (BFS()){
            memset(cur,0,sizeof(cur));
            flow+=DFS(s,INF);
        }
        return flow==tot;
    }
}A;
int n,m,tot,a[maxn],b[maxn];
ll d[maxn][maxn];
bool ok(ll x){
    A.init(n*2+2,0,n*2+1);
    for (int i=1;i<=n;i++){
        A.AddEdge(0,i,a[i]);
        A.AddEdge(n+i,n*2+1,b[i]);
    }
    for (int i=1;i<=n;i++){
        for (int j=1;j<=n;j++)if (d[i][j]<=x){
            A.AddEdge(i,n+j,inf);
        }
    }
    return A.solve(tot);
}
int main(){
    read(n),read(m);
    for (int i=1;i<=n;i++) read(a[i]),read(b[i]),tot+=a[i];
    for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) d[i][j]=(i==j?0:INF);
    for (int i=1;i<=m;i++){
        int u,v,w;read(u),read(v),read(w);
        d[v][u]=d[u][v]=min(d[u][v],1LL*w);
    }
    for (int k=1;k<=n;k++){
        for (int i=1;i<=n;i++)if (d[i][k]!=INF){
            for (int j=1;j<=n;j++)if (d[k][j]!=INF){
                d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
            }
        }
    }
    ll l=0,r=INF-1,ans=-1;
    while (l<=r){
        ll mid=l+((r-l)>>1);
        if (ok(mid)){
            r=mid-1;
            ans=mid;
        }
        else l=mid+1;
    }
    printf("%lld\n",ans);
    return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注