POJ 3189 Steady Cow Assignment (largest stream in the network flow + binary diagram)
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
#include
#include
#include
#include
#include
#include
#include
#includeusing 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
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]
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;}