Ural 1277 cops ans thieves (minimum cut model), uralcops

Source: Internet
Author: User

Ural 1277 cops ans thieves (minimum cut model), uralcops

Question address: http://acm.timus.ru/problem.aspx? Space = 1 & num = 1277

Here we want to split the points. Split a vertex into I, I '. How can I, j has edges, and creating edges (I, J', inf), (j, I ', inf ). Then each vertex edge (I ', I, R [I]). After edge creation, if we want to block the path from s to f, we must break down some edges. In order to minimize the edge weight, we must destroy the edge with the Edge Weight less than inf, corresponding to the split edge (j '-> j) in the graph ). In fact, this edge represents the vertex right of vertex j. The minimum cut is the answer. There is a special issue when s = f.

VIEW CODE

#include<cstdio>#include<algorithm>#include<iostream>#include<cmath>#include<queue>#include<stack>#include<string>#include<cstring>#include<map>#include<vector>#include<set>#include<ctime>#include<stdlib.h>using namespace std;const int mmax= 210;const int mod=1000000007;const int inf=0x3fffffff;using namespace std;struct node{    int flow;    int en;    int next;}E[40010+mmax];int p[mmax];int num;void init(){    memset(p,-1,sizeof p);    num=0;}void add(int st,int en,int flow){    E[num].en=en;    E[num].flow=flow;    E[num].next=p[st];    p[st]=num++;    E[num].en=st;    E[num].flow=0;    E[num].next=p[en];    p[en]=num++;}int d[mmax];bool vis[mmax];int qq[mmax];int cur[mmax];bool bfs(int st,int en){    memset(vis,0,sizeof vis);    int qcnt=0;    qq[++qcnt]=st;    d[st]=0;    vis[st]=1;    while(qcnt)    {        int x=qq[qcnt];        qcnt--;        for(int i=p[x]; i+1; i=E[i].next)        {            int v=E[i].en;            if(!vis[v]&&E[i].flow)            {                vis[v]=1;                qq[++qcnt]=v;                d[v]=d[x]+1;            }        }    }    return vis[en];}int dfs(int st,int en,int  flow){    if(st==en||flow==0)        return flow;    int f=0,dd;    for(int &i=cur[st]; i+1;i=E[i].next)    {        int v=E[i].en;        if(d[st]+1==d[v]&&(dd=dfs(v,en,min(flow,E[i].flow)))>0)        {            E[i].flow-=dd;            E[i^1].flow+=dd;            flow-=dd;            f+=dd;            if(flow==0)                break;        }    }    return f;}int dinic(int st,int en,int n){    int flow=0;    while(bfs(st,en))    {        for(int i=0;i<=n;i++)            cur[i]=p[i];        flow+=dfs(st,en,inf);    }    return flow;}int main(){    int k,n,m,s,f;    while(cin>>k)    {        init();        scanf("%d %d %d %d",&n,&m,&s,&f);        for(int i=1;i<=n;i++)        {            int x;            scanf("%d",&x);            add(n+i,i,x);        }        for(int i=0;i<m;i++)        {            int u,v;            scanf("%d %d",&u,&v);            add(u,v+n,inf);            add(v,u+n,inf);        }        if(s==f)        {            puts("NO");            continue;        }        int ans=dinic(s,f+n,2*n);        if(ans<=k)            puts("YES");        else            puts("NO");    }    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.