Poj 3189 steady cow assignment (largest stream in the network flow + binary diagram)

Source: Internet
Author: User

Address: poj 3189

I'm dizzy... What is the use of quickly completing Daytime tasks... The saved time was wasted by my hands... After another whole day of adjustment, the problem was actually the inverse of N and m in one place !!! Reflection .. Reflection... Face to face...

This is the binary interval, and then the enumerated range position. Create a graph. Not to mention ..

The Code is as follows:

#include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <queue>#include <map>#include<algorithm>using namespace std;const int INF=0x3f3f3f3f;int head[1100], source, sink, nv, cnt, n, mp[1101][30], va[30];int num[1100], d[1100], pre[1100], cur[1100];struct node{    int u, v, cap, next;} edge[1000000];void add(int u, int v, int cap){    edge[cnt].v=v;    edge[cnt].cap=cap;    edge[cnt].next=head[u];    head[u]=cnt++;    edge[cnt].v=u;    edge[cnt].cap=0;    edge[cnt].next=head[v];    head[v]=cnt++;}void bfs(){    memset(d,-1,sizeof(d));    memset(num,0,sizeof(num));    queue<int>q;    q.push(sink);    d[sink]=0;    num[0]=1;    while(!q.empty())    {        int u=q.front();        q.pop();        for(int i=head[u]; i!=-1; i=edge[i].next)        {            int v=edge[i].v;            if(d[v]==-1)            {                d[v]=d[u]+1;                num[d[v]]++;                q.push(v);            }        }    }}int isap(){    memcpy(cur,head,sizeof(cur));    bfs();    int flow=0, u=pre[source]=source, i;    while(d[source]<nv)    {        if(u==sink)        {            int f=INF, pos;            for(i=source; i!=sink; i=edge[cur[i]].v)            {                if(f>edge[cur[i]].cap)                {                    f=edge[cur[i]].cap;                    pos=i;                }            }            for(i=source; i!=sink; i=edge[cur[i]].v)            {                edge[cur[i]].cap-=f;                edge[cur[i]^1].cap+=f;            }            flow+=f;            if(flow>=n)                return flow;            u=pos;        }        for(i=cur[u]; i!=-1; i=edge[i].next)        {            if(d[edge[i].v]+1==d[u]&&edge[i].cap) break;        }        if(i!=-1)        {            cur[u]=i;            pre[edge[i].v]=u;            u=edge[i].v;        }        else        {            if(--num[d[u]]==0) break;            int mind=nv;            for(i=head[u]; i!=-1; i=edge[i].next)            {                if(mind>d[edge[i].v]&&edge[i].cap)                {                    mind=d[edge[i].v];                    cur[u]=i;                }            }            d[u]=mind+1;            num[d[u]]++;            u=pre[u];        }    }    return flow;}int main(){    int m, i, j, l, r;    scanf("%d%d",&n,&m);    for(i=1; i<=n; i++)    {        for(j=1; j<=m; j++)        {            scanf("%d",&mp[i][j]);        }    }    for(i=1; i<=m; i++)    {        scanf("%d",&va[i]);    }    int low=1, high=m, mid, ans=-1, flag;    while(low<=high)    {        mid=(low+high)/2;        flag=0;        for(i=1; i<=m-mid+1; i++)        {            l=i;            r=l+mid-1;            source=0;            sink=n+m+1;            nv=sink+1;            memset(head,-1,sizeof(head));            cnt=0;            for(j=1; j<=n; j++)            {                add(source,j,1);                for(int k=l; k<=r; k++)                {                    add(j,mp[j][k]+n,1);                }            }            for(j=1; j<=m; j++)            {                add(j+n,sink,va[j]);            }            int x=isap();            //printf("%d\n",x);            if(x>=n)            {                flag=1;                break;            }        }        if(flag)        {            high=mid-1;            ans=mid;        }        else        {            low=mid+1;        }    }    printf("%d\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.