hdu4289 city network flow with criminals

  Subject to the effect that the criminals from the delivery of goods from point s to t point, police in some cities interception, interception in every city has a certain cost, ask how much is the minimum cost possible to intercept criminals.

  Now, at some point obstacles, so that the entire chart can not be in China Unicom, we know that a similar problem: cut some point no longer makes Figure Unicom - minimum cut problem, then the demolition of these points, the point becomes the right edge right, not what is the minimum cut problem, we know the minimum cut equal to the maximum flow, so this is a point of network flow problem + demolition.

  Code:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
const int maxn=2100,maxm=50005;
const int inf=2147483647;
int n,m; 
int S,T;
int x,y;
struct zhw{
    int to,last,val;
}tu[maxm<<2];
int tot,head[maxn];
void add(int x,int y,intv) 
{ 
    all ++, you [all] = .last head [x] head [x] = all you [all] .to = y, you [all] .val = v; 
    all ++, you [all] = .last head [and] head [y] = all you [all] .to = x, you [all] .val = 0 ; 
} 
Int years;
bool vis [maxn];
int dis [maxn]; 
Queue < int > q;
bool BFS () 
{ 
    memset (vis, 0 , sizeof (vis)); 
    memset (dis, 0 , sizeof (dis)); 
    q.push (S) vis [S] = 1 ; dis [S] = 1 ;
    while(!q.empty())
    {
        int k=q.front();q.pop();
        for(int i=head[k];i;i=tu[i].last)
        {
            if(!vis[tu[i].to]&&tu[i].val)
            {
                vis[tu[i].to]=1;
                dis[tu[i].to]=dis[k]+1;
                q.push(tu[i].to);
            }
        }
    }
    return vis[T];
}
int m_flow(int now,int lim)
{
    if(!lim||now==T)return lim;
    int flow=0,f;
    for(int i=head[now];i;i=tu[i].last)
    {
        if(dis[tu[i].to]==dis[now]+1&&tu[i].val>0)
        {
            f=m_flow(tu[i].to,min(lim,tu[i].val));
            if(!f)continue;
            flow+=f,lim-=f;
            tu[i].val-=f,tu[i^1].val+=f; 
            if(!lim)break;
        }
    }
    if(!flow)dis[now]=-1;
    return flow;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        scanf("%d%d",&x,&y);
        S=x,T=y;
        tot=1,memset(head,0,sizeof(head));
        add(S,x,inf),add(y+n,T,inf);
        for(int i=1;i<=n;++i)
        {
            Scanf (" % D " , & X); 
            the Add (I, I + // This is the portion of the split point, a point of left and right split into two parts, this point must be the side from the left, a; n, x) side must be from the right side out, the edge weight is the original point right 
        } 
        for ( int I = . 1 ; I <= m; ++ I) 
        { 
            Scanf ( " % D% D " , & X, & Y); 
            the Add (X + n-, Y, INF); // above, since it is their bound themselves, must be left into the right side of the road is a two-way side, no restraint, so the flow is positive infinity 
            the Add (Y + n-, X, INF); 
        } 
        ANS = 0 ; 
        T = T +n; // Here we must note that the final will definitely have to go to the right of the T can Yeah, I started here it is wrong
         the while (bfs ()) 
        { 
            ANS + = m_flow (S, INF); 
        } 
        printf ( " D% \ n- " , ANS); 
    } 
    return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/yuelian/p/12301079.html
Recommended