POJ 2391 Ombrophobic Bovines (網絡流)

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

題意:給定一個無向圖,點 i 處有 Ai 頭牛,點 i 處的牛棚能容納 Bi 頭牛,求一個最短時
間 T 使得在 T 時間內所有的牛都能進到某一牛棚裏去。 (1 <= N <= 200, 1 <= M <=
1500, 0 <= Ai <= 1000, 0 <= Bi <= 1000, 1 <= Dij <= 1,000,000,000),此題題目告訴了所有的牛都是同時跑的,要求的就是最慢找到牛棚的那頭牛用時最少是多少。

分析:由於n只有200,那麼我們就可以用floyd預處理每兩個點的最少用時(就是最短路)存在數組dis[i][j]裏面。由於每個點有兩個限制條件:牛的頭數以及牛棚能容納牛的頭數,顯然就是拆爲兩個點x和y,x點連接源點,容量爲牛的頭數,y點連接匯點,容量爲能容納牛的頭數。接下來就是其他點之間的連接問題,因爲此題要求的是用時最大的值最小,直接建圖不行,看到最大值最小,我們很容易想到二分答案,對於每個二分的答案mid,我們把dis[i][j]<=mid的兩個點互相連一條邊,容量爲inf,然後跑最大流,如果最大流等於所有牛的頭數,那麼這個答案ok,單調性很明顯:如果最大流跑出來小於牛的頭數,意思就是你給的時間mid太少了,人家還沒跑完,所以增大時間,要是最大流跑出來等於了牛的頭數,雖然這個答案可行,但我們要的是最優的,所以繼續減少限制的時間看看行不行就可以了。注意這個題每次二分的mid並不代表mid就可以用來更新答案ans,因爲建圖的時候條件是dis[i][j]<=mid,那麼就有可能所有的dis都小於mid,那麼更新答案時就是所有小於mid中的最大的那個值。

dinic模板爲大白書上的模板:

#include<stdio.h>
#include<string.h>
#include<vector>
#include<queue>
#include<algorithm>
#define maxn 2000
#define LL long long
#define inf 0x7fffffff
#define INF 1e18
using namespace std;
struct edge
{
    int from,to,cap,flow;
};
vector<edge> edges;
vector<int> G[maxn];
int d[maxn],cur[maxn];
bool vis[maxn];
int s,t;
void init()
{
    for(int i=0;i<maxn;i++) G[i].clear();
    edges.clear();
}
void add(int from,int to,int cap)
{
    edges.push_back((edge){from,to,cap,0});
    edges.push_back((edge){to,from,0,0});
    int m=edges.size();
    G[from].push_back(m-2);
    G[to].push_back(m-1);
}
bool bfs()
{
    memset(vis,false,sizeof(vis));
    memset(d,0,sizeof(d));
    queue<int> q;
    q.push(s);
    d[s]=0,vis[s]=true;
    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(!vis[e.to]&&e.cap>e.flow)
            {
                vis[e.to]=true;
                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<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;
}
int maxflow()
{
    int flow=0;
    while(bfs())
    {
        memset(cur,0,sizeof(cur));
        flow+=dfs(s,inf);
    }
    return flow;
}
int cnt[300],cap[300];
LL dis[300][300],ans;
int F,P;
void build(LL mid)
{
    ans=0;
    for(int i=1;i<=F;i++)
    {
        add(s,i,cnt[i]);
        add(i+F,t,cap[i]);
    }
    for(int i=1;i<=F;i++)
    {
        for(int j=1;j<=F;j++)
        {
            if(dis[i][j]<=mid)
            {
                ans=max(ans,dis[i][j]);
                add(i,j+F,inf);
            }
        }
    }
}
int main()
{
    while(scanf("%d%d",&F,&P)==2)
    {
        s=0,t=2*F+1;
        int sum=0;
        for(int i=1;i<=F;i++)
        {
            scanf("%d%d",&cnt[i],&cap[i]);
            sum+=cnt[i];
        }
        for(int i=1;i<=F;i++)
            for(int j=1;j<=F;j++)
            dis[i][j]=INF;
        for(int i=1;i<=F;i++) dis[i][i]=0;
        for(int i=1;i<=P;i++)
        {
            int x,y;
            LL v;
            scanf("%d%d%lld",&x,&y,&v);
            if(v<dis[x][y]) dis[x][y]=dis[y][x]=v;
        }
        LL L=INF,R=0,res=INF;
        for(int k=1;k<=F;k++)
            for(int i=1;i<=F;i++)
            for(int j=1;j<=F;j++)
            dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
        for(int i=1;i<=F;i++)
            for(int j=1;j<=F;j++)
        {
            L=min(L,dis[i][j]);
            R=max(R,dis[i][j]);
        }
        while(L<=R)
        {
            LL mid;
            mid=(L+R)/2;
            init();
            build(mid);
            int tem=maxflow();
            if(tem!=sum) L=mid+1;
            else if(tem==sum)
            {
                res=min(res,ans);
                R=mid-1;
            }
        }
        if(res<INF) printf("%lld\n",res);
        else puts("-1");
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章