poj 2391 Ombrophobic Bovines

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
inline int read(){
    int sum=0,x=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')
            x=0;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
        sum=(sum<<1)+(sum<<3)+(ch^48),ch=getchar();
    return x?sum:-sum;
}
inline void write(ll x){
    if(x<0)
        putchar('-'),x=-x;
    if(x>9)
        write(x/10);
    putchar(x%10+'0');
}
const int M=404;
const int inf=0x3f3f3f3f;
const ll inff=(1ll<<60);
struct node{
    int v,w,nextt;
}e[200005];
int head[M],cur[M],deep[M],x[M],y[M],n,m,tot,s,t;
ll mp[202][202];
void addedge(int u,int v,int w){
    e[tot].v=v;
    e[tot].w=w;
    e[tot].nextt=head[u];
    head[u]=tot++;
    e[tot].v=u;
    e[tot].w=0;
    e[tot].nextt=head[v];
    head[v]=tot++;
}
bool bfs(){
    for(int i=0;i<=t;i++)
        deep[i]=0;
    queue<int>que;
    que.push(s);
    deep[s]=1;
    while(!que.empty()){
        int u=que.front();
        que.pop();
        for(int i=head[u];~i;i=e[i].nextt){
            int v=e[i].v;
            if(e[i].w>0&&deep[v]==0){
                deep[v]=deep[u]+1;
                if(v==t)
                    return true;
                que.push(v);
            }
        }
    }
    return deep[t]!=0;
}
int dfs(int u,int fl){
    if(u==t)
        return fl;
    int x,ans=0;
    for(int i=cur[u];~i;i=e[i].nextt){
        int v=e[i].v;
        if(e[i].w>0&&deep[v]==deep[u]+1){
            x=dfs(v,min(fl-ans,e[i].w));
            e[i].w-=x;
            e[i^1].w+=x;
            if(e[i].w)
                cur[u]=i;
            ans+=x;
            if(ans==fl)
                return fl;
        }
    }
    if(!ans)
        deep[u]=0;
    return ans;
}
int dinic(){
    int ans=0;
    while(bfs()){
        for(int i=0;i<=t;i++)
            cur[i]=head[i];
        ans+=dfs(s,inf);
    }
    return ans;
}
void init(){
    for(int i=0;i<=t;i++)
        head[i]=-1;
    tot=0;
}
int solve(ll midd){
    init();
    for(int i=1;i<=n;i++){
        addedge(s,i,x[i]);
        addedge(i+n,t,y[i]);
        for(int j=1;j<=n;j++)
            if(mp[i][j]<=midd)
                addedge(i,j+n,inf);
    }
    return dinic();
}
int main(){
    while(~scanf("%d%d",&n,&m)){
        int sum=0;
        s=0,t=(n<<1)+1;
        for(int i=1;i<=n;i++){
            x[i]=read(),y[i]=read();
            sum+=x[i];
        }
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                if(i!=j)
                    mp[i][j]=inff;
        while(m--){
            int u=read(),v=read(),w=read();
            mp[u][v]=mp[v][u]=min(mp[u][v],w*1ll);
        }
        //floy
        for(int k=1;k<=n;k++)
            for(int i=1;i<=n;i++)
                for(int j=1;j<=n;j++)
                    if(mp[i][j]>mp[i][k]+mp[k][j])
                        mp[i][j]=mp[i][k]+mp[k][j];
        ll l=0,r=0,ans,midd;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++)
                if(mp[i][j]!=inff)
                    r=max(r,mp[i][j]);
        }
        /*for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++)
                cout<<mp[i][j]<<" ";
            cout<<endl;
        } 
        cout<<t<<endl;*/
        if(solve(r)!=sum){
            puts("-1");
            continue;
        }
        while(l<=r){
            midd=(l+r)>>1;
            if(solve(midd)==sum)
                ans=midd,r=midd-1;
            else
                l=midd+1;
        }
        write(ans);
        putchar('\n');
    }
    return 0;
}
View Code

 题:有N个点,每个点都有一些老鼠和一些能容纳老鼠的地方,现在,下午了,老鼠要在规定时间内都找到自己的容纳点,每个点到每个点都有一定的到达时间,现在问你最少要多少时间才能完成这项工作,如果不能完成,输出“-1”

posted @ 2019-05-28 23:30  starve_to_death  阅读(145)  评论(0编辑  收藏  举报