POJ - 2391 Ombrophobic Bovines (Floyd + 二分 +ISAP)

Ombrophobic Bovines

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

Hint

OUTPUT DETAILS:

In 110 time units, two cows from field 1 can get under the shelter in that field, four cows from field 1 can get under the shelter in field 2, and one cow can get to field 3 and join the cows from that field under the shelter in field 3. Although there are other plans that will get all the cows under a shelter, none will do it in fewer than 110 time units.

題目鏈接:http://poj.org/problem?id=2391

題目大意:有F個田地P條路,接下來F行ai,bi表示第i個田地現在有ai頭牛,有bi頭牛能在這裏避雨,接下來是P行ui,vi,wi,表示第i條路在ui和vi之間,牛走過這條路需要花費wi的時間。問所有牛都能避雨要花費的最短時間,如果有牛不能避雨就輸出-1

思路:先用Floyd求出每兩點間的最小花費時間d[ i ][ j ],二分花費時間,再建圖,拆點,超級源點到點 i 之間建邊 流量爲ai,超級匯點到點 i+F 之間建邊 流量爲bi, 如果d[i][j] <= mid, i 到 j+F 建邊,流量爲inf,然後就是ISAP模板求最大流

代碼:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
#define ll long long
const int N=505;
const int M=100005;
const int inf=0x3f3f3f;
const ll INF=0x3f3f3f3f3f3f;
ll mp[N][N];
int g[N],h[N],pre[N];
int first[N],tot,sum,st,ed;
int dis[N],vis[N],a[N],b[N],n,m;
struct node
{
    int v,next;
    int cap,flow;
    node(){};
    node(int tv,int tc,int tnext)
    {
        v=tv,cap=tc,next=tnext,flow=0;
    }
}e[M*2];
void adde(int u,int v,int c)
{
    e[tot]=node(v,c,first[u]);
    first[u]=tot++;
    e[tot]=node(u,0,first[v]);
    first[v]=tot++;
}
void set_h(int t)
{
    memset(h,-1,sizeof(h));
    memset(g,0,sizeof(g));
    queue<int>q;
    h[t]=0;
    q.push(t);
    while(!q.empty())
    {
        int v=q.front();
        q.pop();
        g[h[v]]++;
        for(int i=first[v];~i;i=e[i].next)
        {
            int u=e[i].v;
            if(h[u]==-1)
            {
                h[u]=h[v]+1;
                q.push(u);
            }
        }
    }
}
int ISAP(int s,int t,int n) //ISAP模板
{
    set_h(t);
    int ans=0,u=s,d;
    while(h[s]<n)
    {
        int i=first[u];
        if(u==t) d=inf;
        for(;~i;i=e[i].next)
        {
            int v=e[i].v;
            if(e[i].cap>e[i].flow&&h[u]==h[v]+1)
            {
                u=v;
                pre[v]=i;
                d=min(d,e[i].cap-e[i].flow);
                if(u==t)
                {
                    while(u!=s)
                    {
                        int j=pre[u];
                        e[j].flow+=d;
                        e[j^1].flow-=d;
                        u=e[j^1].v;
                    }
                    ans+=d;
                    d=inf;
                }
                break;
            }
        }
        if(i==-1)
        {
            if(--g[h[u]]==0) break;
            int hmin=n-1;
            for(int j=first[u];~j;j=e[j].next)
                if(e[j].cap>e[j].flow)
                hmin=min(hmin,h[e[j].v]);
            h[u]=hmin+1;
            g[h[u]]++;
            if(u!=s) u=e[pre[u]^1].v;
        }
    }
    return ans;
}
int build(ll w) //建圖
{
    tot=0;
    memset(first,-1,sizeof(first));
    st=0,ed=2*n+1; //超級源點匯點
    for(int i=1; i<=n; i++)
    {
        adde(st,i,a[i]);
        adde(i+n,ed,b[i]);
        for(int j=1; j<=n; j++)
            if(mp[i][j]<=w)
                adde(i,j+n,inf);
    }
    return ISAP(st,ed,2*n+2);
}
void solve()
{
    ll maxx=0;
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
            if(mp[i][j]<INF&&mp[i][j]>maxx)
                maxx=mp[i][j];
    if(build(maxx)<sum) printf("-1\n");
    else
    {
        ll l=0,r=maxx,ans;
        while(l<=r)
        {
            ll mid=(l+r)>>1;
            if(build(mid)>=sum)
            {
                ans=mid;
                r=mid-1;
            }
            else l=mid+1;
        }
        printf("%lld\n",ans);
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    sum=0;
    int u,v;
    ll w;
    for(int i=1; i<=n; i++)
    {
        scanf("%d%d",&a[i],&b[i]);
        sum+=a[i];
    }
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
            if(i==j) mp[i][j]=0;
            else mp[i][j]=INF;
    }
    for(int i=0; i<m; i++)
    {
        scanf("%d%d%lld",&u,&v,&w);
        if(w<mp[u][v]) mp[u][v]=mp[v][u]=w;
    }
    for(int k=1; k<=n; k++) //Floyd
        for(int i=1; i<=n; i++)
            if(mp[i][k]<INF)
                for(int j=1; j<=n; j++)
                    mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]);
    solve();
    return 0;
}

 

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