P1345 [USACO5.4]奶牛的電信Telecowmunication

來源:互聯網
上載者:User

標籤:void   post   dinic   inf   bfs   pre   markdown   ++   i++   

#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<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]奶牛的電信Telecowmunication

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.