POJ--2391 -- Ombrophobic Bovines [split point + Floyd + Dinic optimization + binary answer] maximum network flow, bovinesdinic

Source: Internet
Author: User

POJ--2391 -- Ombrophobic Bovines [split point + Floyd + Dinic optimization + binary answer] maximum network flow, bovinesdinic

Link:Http://poj.org/problem? Id = 2391

Question:There are f pastures, and each pasture currently has a certain number of cows grazing. When it rains, it can make a certain number of cows shelter from the rain, and f pastures have m links, it takes a certain amount of time for every ox to go from one point to another. Now it is raining, and the farmer sends an alarm to immediately avoid rain. The information about each pasture and m edges is reported. How long does it take for a farmer to issue an alert in advance to ensure that all cattle can shelter from the rain? If not all cows can successfully avoid rain output-1.


Ideas:This question needs to be split. It is only known when we see Wei shen's blog. We split the source image into a bipartite graph to avoid breaking through the maximum distance limit. Each vertex becomes two points, that is, when I changes to I 'and I' ', a source point is established to connect each I', And the capacity is the initial number of cattle in each pasture. A sink point is established, and all I ''points to the sink point, capacity is the number of cattle that each pasture can accommodate. If two vertices I and j are connected, create a path between I 'and j'', and between J' And I' '. The capacity is INF, which can be infinite. Then this question is the same as the previous POJ2112. POJ2112 does not need to be split because it is a bipartite graph.

Next, Floyd processes the shortest path between two points, and then returns the second answer.

But this question is not complete yet. I set the previous Dinic template, TLE. The maximum number of edges in this question is larger than that in POJ2112, and the Case time limit is the same. I am writing a queue or TLE. I will remove the vis array and replace the vis function with dist, or TLE, I searched for the AC's Dinic code on the Internet and saw a code similar to mine. I pasted it and handed it in. TLE was so powerless...

I found a code that looks a little changed and changed it to my own, 219 MS AC.

After reading this, I think the main optimization is as follows:

1. my previous template is to find each augmented path from the source point for relaxation. The optimization algorithm is to find an augmented path for relaxation, exit, update the maximum stream value, and find the next one, until the update value returns 0, it indicates that the header has been updated to avoid unnecessary searches.

2. If no augmented path exists for the vertex at this time, delete it from the current layered network and do not search again after backtracking.

I think there are two optimizations. After the optimization, the efficiency is improved significantly!


#include<cstring>#include<string>#include<fstream>#include<iostream>#include<iomanip>#include<cstdio>#include<cctype>#include<algorithm>#include<queue>#include<map>#include<set>#include<vector>#include<stack>#include<ctime>#include<cstdlib>#include<functional>#include<cmath>using namespace std;#define PI acos(-1.0)#define MAXN 200100#define eps 1e-7#define INF 0x7FFFFFFF#define LLINF 0x7FFFFFFFFFFFFFFF#define seed 131#define mod 1000000007#define ll long long#define ull unsigned ll#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1struct node{    int u,w,next;}edge[100010];int head[410],dist[410],q[410];int aa[410],bb[410];ll e[410][410];int n,m,cnt,src,sink,f,p;void add_edge(int a,int b,int c){    edge[cnt].u = b;    edge[cnt].w = c;    edge[cnt].next = head[a];    head[a] = cnt++;}void floyd(){    int i,j,k;    for(k=1;k<=f;k++){        for(i=1;i<=f;i++){            if(e[i][k]==LLINF)  continue;            for(j=1;j<=f;j++){                if(e[i][k]+e[k][j]<e[i][j]&&e[i][k]!=LLINF&&e[k][j]!=LLINF)                    e[i][j] = e[i][k] + e[k][j];            }        }    }}void build_graph(ll minm){    int i,j;    cnt = 0;    memset(head,-1,sizeof(head));    for(i=1;i<=f;i++){        add_edge(src,i,aa[i]);        add_edge(i,src,0);        add_edge(i+f,sink,bb[i]);        add_edge(sink,i+f,0);        for(j=1;j<=f;j++){            if(e[i][j]<=minm){                add_edge(i,j+f,INF);                add_edge(j+f,i,0);            }        }    }}bool bfs(){    int i,j;    memset(dist,-1,sizeof(dist));    int f = 0, t = 1;    q[0] = src;    dist[src] = 1;    while(f<t){        int u = q[f++];        for(i=head[u];i!=-1;i=edge[i].next){            int v = edge[i].u;            if(dist[v]==-1&&edge[i].w){                dist[v] = dist[u] + 1;                q[t++] = v;            }        }    }    if(dist[sink]!=-1)  return true;    return false;}int dfs(int u,int delta){    int i,j;    int dd;    if(u==sink) return delta;    for(i=head[u];i!=-1;i=edge[i].next){        if(dist[edge[i].u]==dist[u]+1&&edge[i].w&&(dd = dfs(edge[i].u,min(delta,edge[i].w)))){            edge[i].w -= dd;            edge[i^1].w += dd;            return dd;        }    }    dist[u] = -1;    return 0;}int main(){    int i,j;    int ta,tb,tc;    int cow;    scanf("%d%d",&f,&p);    n = 2 * f + 2;    src = 0;    sink = 2 * f + 1;    cow = 0;    for(i=1;i<=f;i++){        scanf("%d%d",&aa[i],&bb[i]);        cow += aa[i];    }    for(i=0;i<=f;i++){        for(j=0;j<=f;j++)            e[i][j] = LLINF;        e[i][i] = 0;    }    for(i=0;i<p;i++){        scanf("%d%d%d",&ta,&tb,&tc);        if(tc<e[ta][tb])    e[ta][tb] = e[tb][ta] = tc;    }    floyd();    ll mid,l = 0,r = 0;    for(i=1;i<=f;i++){        for(j=1;j<=f;j++){            if(e[i][j]!=LLINF)  r = max(e[i][j],r);        }    }    int sum;    ll ans = -1;    while(l<=r){        mid = (l+r)>>1LL;        //cout<<l<<" "<<r<<endl;        sum = 0;        build_graph(mid);        while(bfs()){            while(1){                int ttt = dfs(src,INF);                if(!ttt)    break;                sum += ttt;            }        }        if(sum==cow){            ans = mid;            r = mid-1;        }        else    l = mid + 1;    }    printf("%I64d\n",ans);    return 0;}






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.