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

Ombrophobic Bovines
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 15705 Accepted: 3425

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

题意不再赘述  整个的思路跟poj 2112 差不多

主要是一开始每个点有若干奶牛 而且每个点最多能容纳的奶牛数量是不一样的 

所以在建边时  边的容量是不一样的

另外这道题的数据实在变态  数组不能开的太大 容易tle

还有跟路径长度有关的值是long long类型 

折腾了好久 终于过了。。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define eps 1e-8
#define op operator
#define MOD  10009
#define MAXN  41000
#define INF   0x3f3f3f3f
#define MEM(a,x)    memset(a,x,sizeof a)
#define ll long long

using namespace std;

ll dis[210][210];
int in[210],out[210];
const ll inf=1e16;

struct Dinic
{
    struct Edge
    {
        int from,to,cap,flow;
        Edge(){}
        Edge(int from,int to,int cap,int flow):from(from),to(to),cap(cap),flow(flow){}
    };
    vector<Edge> edges;
    vector<int> G[MAXN];
    bool vis[MAXN];
    int d[MAXN];
    int cur[MAXN];
    int n,m,s,t,maxflow;

    void init(int n)
    {
        this->n=n;
        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()
    {
        MEM(vis,0);
        MEM(d,-1);
        queue<int> q;
        q.push(s);
        d[s]=maxflow=0;
        vis[s]=1;
        while(!q.empty())
        {
            int u=q.front(); q.pop();
            int sz=G[u].size();
            for(int i=0;i<sz;i++)
            {
                Edge e=edges[G[u][i]];
                if(!vis[e.to]&&e.cap>e.flow)
                {
                    d[e.to]=d[u]+1;
                    vis[e.to]=1;
                    q.push(e.to);
                }
            }
        }
        return vis[t];
    }

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

    int Maxflow(int s,int t)
    {
        this->s=s; this->t=t;
        int flow=0;
        while(bfs())
        {
            MEM(cur,0);
            flow+=dfs(s,INF);
        }
        return flow;
    }
}Dic;



int main()
{
//    freopen("ceshi.txt","r",stdin);
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int sum=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&in[i],&out[i]);
            sum+=in[i];
        }
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                dis[i][j]=inf;
        for(int i=0;i<m;i++)
        {
            int u,v;ll w;
            scanf("%d%d%lld",&u,&v,&w);
            if(w<dis[u][v])
                dis[u][v]=dis[v][u]=w;
        }
        int s=0,t=2*n+1;
        ll ma=-1;
        for(int k=1;k<=n;k++)
        {
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    if(dis[i][j]>dis[i][k]+dis[k][j])
                        dis[i][j]=dis[i][k]+dis[k][j];
                    if(dis[i][j]>ma&&dis[i][j]!=inf)
                        ma=dis[i][j];
                }
            }
        }//floyd
        ll ans=-1,low=0,high=ma+1;
        while(low<=high)
        {
            ll mid=(low+high)/2;
            Dic.init(t);
            for(int i=1;i<=n;i++)
            {
                Dic.addedge(s,i,in[i]);
                Dic.addedge(i+n,t,out[i]);
            }
            for(int i=1;i<=n;i++)
                Dic.addedge(i,i+n,INF);
            for(int i=1;i<=n;i++)
            {
                for(int j=i+1;j<=n;j++)
                {
                    if(dis[i][j]<=mid)
                    {
                        Dic.addedge(i,j+n,INF);
                        Dic.addedge(j,i+n,INF);
                    }
                }
            }
            int mx=Dic.Maxflow(s,t);
            if(mx>=sum)
            {
                ans=mid;
                high=mid-1;
            }
            else low=mid+1;
        }
        printf("%lld\n",ans);
    }
    return 0;
}









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值