首页 > 代码库 > AC日记——[USACO5.4]奶牛的电信Telecowmunication 洛谷 P1345

AC日记——[USACO5.4]奶牛的电信Telecowmunication 洛谷 P1345

[USACO5.4]奶牛的电信Telecowmunication

 

思路:

  水题;

 

代码:

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define maxn 205#define maxm 20005#define INF 0x3f3f3f3fint head[maxn],cnt=1,n,m,E[maxm],V[maxm],F[maxm],s,t;int que[maxm<<2],deep[maxn],c1,c2;inline void in(int &now){    char Cget=getchar();now=0;    while(Cget>9||Cget<0) Cget=getchar();    while(Cget>=0&&Cget<=9)    {        now=now*10+Cget-0;        Cget=getchar();    }}inline void edge_add(int u,int v,int f){    E[++cnt]=head[u],V[cnt]=v,F[cnt]=f,head[u]=cnt;    E[++cnt]=head[v],V[cnt]=u,F[cnt]=0,head[v]=cnt;}bool bfs(){    memset(deep,-1,sizeof(deep));    int h=0,tail=1,now;que[0]=s,deep[s]=0;    while(h<tail)    {        now=que[h++];        for(int i=head[now];i;i=E[i])        {            if(deep[V[i]]<0&&F[i])            {                deep[V[i]]=deep[now]+1;                if(V[i]==t) return true;                que[tail++]=V[i];            }        }    }    return false;}int flowing(int now,int flow){    if(t==now||flow<=0) return flow;    int oldflow=0,pos;    for(int i=head[now];i;i=E[i])    {        if(!F[i]||deep[V[i]]!=deep[now]+1) continue;        pos=flowing(V[i],min(flow,F[i]));        flow-=pos,oldflow+=pos,F[i]-=pos,F[i^1]+=pos;        if(!flow) return oldflow;    }    if(!oldflow) deep[now]=-1;    return oldflow;}int main(){    in(n),in(m),in(c1),in(c2),s=c1+n,t=c2;    for(int i=1;i<=n;i++) edge_add(i,i+n,1);    while(m--)    {        in(c1),in(c2);        edge_add(c1+n,c2,INF);        edge_add(c2+n,c1,INF);    }    int ans=0;    while(bfs()) ans+=flowing(s,INF);    cout<<ans;    return 0;}

 

AC日记——[USACO5.4]奶牛的电信Telecowmunication 洛谷 P1345