P1345 [USACO5.4] cow's telecom telecowmunication

Source: Internet
Author: User

#include <iostream> #include <cstring> #include <cstdio> #define N 1000005#define inf 0x3f3f3f3fusing namespace std;struct node{int to,nxt,cap;};    int que[n];int h,qt;struct dinic{int n,m;    int s,t;    int tot;    int d[n];    int cur[n];    Node E[n];    int vis[n];    int head[n];    Dinic () {tot=1;}        void Add (int f,int t,int c) {e[++tot].to=t;        E[TOT].NXT=HEAD[F];        E[tot].cap=c;        Head[f]=tot;        E[++tot].to=f;        E[TOT].NXT=HEAD[T];        E[tot].cap=0;    Head[t]=tot;        } bool BFs () {memset (vis,false,sizeof (VIS));        for (int i=0;i<=n;++i) cur[i]=head[i];        Que[qt=1]=s,h=0;int top,to;d[s]=0;vis[s]=1;            while (H&LT;QT) {top=que[++h]; for (int i=head[top];i;i=e[i].nxt) if (!vis[e[i].to]&&e[i].cap) {to=e[i].to;que[++                    Qt]=to;                    vis[to]=true;d[to]=d[top]+1;                if (to==t) return true;      }        }  return vis[t];        } int dfs (int x,int fl) {if (x==t) return FL;        int flow=0,f;                    for (int i=cur[x];i;i=e[i].nxt) if (d[e[i].to]==d[x]+1) if (F=dfs (E[i].to,min (FL,E[I].CAP))) {                    E[i].cap-=f;                    E[i^1].cap+=f;                    Flow+=f;fl-=f;                if (fl<=0) break;        } if (fl) d[x]=-1;    return flow;        } int dinic (int s,int t) {this->s=s;        this->t=t;        int ans=0;        while (BFS ())) Ans+=dfs (S,inf);    return ans;    }};int N,m,s,t;int Main () {dinic F;    cin>>n>>m>>s>>t;f.n=3*n;        for (int i=1;i<=m;i++) {int A, B;        cin>>a>>b;        F.add (A+n,b,inf); F.add (b,a+n,0);    F.add (B+n,a,inf); F.add (a,b+n,0);    } for (int i=1;i<=n;i++) F.add (i,i+n,1), F.add (i+n,i,0);    Cout<<f.dinic (s+n,t) <<endl; return 0;}

P1345 [USACO5.4] cow's telecom telecowmunication

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.