Poj1966cable TV network--min cut (maximum flow)

Source: Internet
Author: User

Title: http://poj.org/problem?id=1966

A point is split into points and out points, with a side right of 1 side, run the maximum flow is the smallest cut;

The original Benquan is assigned to the INF anti-cut;

Enumerating the source and sink points, two directly adjacent points do not have to be enumerated;

Note: 1, the source point is the out point of the enumeration point I, the meeting point is the entry point of the enumeration point J;

2, read in the way, free of space;

3, in the Dinic run the maximum flow process, will change the edge right, so each enumeration to replicate a group of side run maximum flow, so as not to affect the back;

Another: The points in the data start from 0, so the read-in time + + to use.

The code is as follows:

#include <iostream>#include<cstdio>#include<cstring>#include<queue>using namespaceStd;queue<int>Q;intn,m,head[ the],cur[ the],ct=1, inf=1e9,ans,d[ the];BOOLsid[ -][ -];structn{intto,next,w; N (intt=0,intn=0,intww=0): to (T), Next (N), W (WW) {}}edge[6005],ed[6005];voidAddintXintYintz) {ed[++ct]=n (y,head[x],z); head[x]=CT; ed[++ct]=n (X,head[y],0); head[y]=CT;}BOOLBFsintSintt) {memset (d,0,sizeofd);  while(Q.size ()) Q.pop (); D[s]=1; Q.push (s);  while(Q.size ()) {intx=Q.front (); Q.pop ();  for(intI=head[x];i;i=Edge[i].next) {            intu=edge[i].to; if(!d[u]&&EDGE[I].W) {D[u]=d[x]+1;            Q.push (U); }        }    }    returnd[t];}intDfsintXintFintt) {    if(x==t)returnF; intres=0;  for(intI=cur[x];i;i=Edge[i].next) {        intu=edge[i].to; if(d[u]==d[x]+1&&EDGE[I].W) {            intTmp=dfs (U,min (edge[i].w,f-res), T); EDGE[I].W-=tmp; Edge[i^1].w+=tmp; Res+=tmp; if(EDGE[I].W) cur[x]=i; if(res==f)returnF; }    }    if(!res) d[x]=0; returnRes;}intDinic (intSintt) {memcpy (edge,ed,sizeofED);//!!!    intres=0;  while(BFS (s+n,t)) {         for(intI=0; i<=2*n;i++) cur[i]=Head[i]; Res+=dfs (s+n,inf,t); }    returnRes;}intMain () { while(SCANF ("%d%d", &n,&m) = =2)    {        if(!n| | n==1) {printf ("%d\n", N); Continue; }        if(!m) {printf ("0\n"); Continue; } CT=1; ans=inf; memset (Head,0,sizeofhead); memset (SID,0,sizeofSID);  for(intI=1; i<=n;i++) Add (I,i+n,1);//Char dc=0;         for(intI=1; i<=m;i++)        {//dc=0;//while (dc!= ' (') scanf ("%c", &DC);            intx, y; scanf ("(%d,%d)", &x,&y);//or read in the annotated method, here is scanf ("%d,%d", &x,&y);x++;y++;//sid[x][y]=1; sid[y][x]=1; Add (x+N,y,inf); Add (Y+N,x,inf); }         for(intI=1; i<=n;i++)             for(intj=i+1; j<=n;j++)                if(!sid[i][j]) ans=min (ans,dinic (i,j)); if(Ans==inf) ans=N; printf ("%d\n", ans);//dc=0;//While (dc!= ') ') scanf ("%c", &DC);    }    return 0;}

Poj1966cable TV network--min cut (maximum flow)

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.