洛谷P1345 [USACO5.4]奶牛的电信Telecowmunication

2018-02-03 19:48:01来源:cnblogs.com作者:自为风月马前卒人点击

1* / 3 - 2*

输入输出样例

`3 2 1 21 32 3`

`1一眼就能看出是最小割啊。。拆完点之后跑一边Dinic就行拆出的点之前连边权为1的边题目中给出的连边连INF的边关键是代码怎么写啊QWQ想了半天没想出来，看了一下学姐的发现了一种很巧妙的方法`

`#include<cstdio>#include<cstring>#include<queue>using namespace std;const int MAXN=801,INF=5*1e8+10;inline char nc(){    static char buf[MAXN],*p1=buf,*p2=buf;    return p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXN,stdin),p1==p2)?EOF:*p1++;}inline int read(){    char c=nc();int x=0,f=1;    while(c<'0'||c>'9'){if(c=='-')f=-1;c=nc();}    while(c>='0'&&c<='9'){x=x*10+c-'0';c=nc();}    return x*f;}int S=0,T=3001;struct node{    int u,v,flow,nxt;}edge[MAXN*20];int head[MAXN],cur[MAXN],num=0;inline void add_edge(int x,int y,int z){    edge[num].u=x;    edge[num].v=y;    edge[num].flow=z;    edge[num].nxt=head[x];    head[x]=num++;}inline void AddEdge(int x,int y,int z) {    add_edge(x,y,z);    add_edge(y,x,0);}int deep[MAXN];inline bool BFS(){    memset(deep,0,sizeof(deep));    deep[S]=1;    queue<int>q;    q.push(S);    while(q.size()!=0)    {        int p=q.front();        q.pop();        for(int i=head[p];i!=-1;i=edge[i].nxt)            if(!deep[edge[i].v]&&edge[i].flow)            {                deep[edge[i].v]=deep[p]+1;q.push(edge[i].v);                if(edge[i].v==T) return 1;            }    }    return deep[T];}int DFS(int now,int nowflow){    if(now==T||nowflow<=0)    return nowflow;    int totflow=0;    for(int &i=cur[now];i!=-1;i=edge[i].nxt)     {        if(deep[edge[i].v]==deep[now]+1&&edge[i].flow)        {            int canflow=DFS(edge[i].v,min(nowflow,edge[i].flow));            edge[i].flow-=canflow;edge[i^1].flow+=canflow;            totflow+=canflow;            nowflow-=canflow;            if(nowflow<=0) break;        }    }    return totflow;}int Dinic(){    int ans=0;    while(BFS())    {        memcpy(cur,head,sizeof(head));         ans+=DFS(S,INF);    }    return ans;}int main(){    #ifdef WIN32    freopen("a.in","r",stdin);    #else    #endif    memset(head,-1,sizeof(head));    int N=read(),M=read(),A=read(),B=read();       for(int i=1;i<=N;i++) AddEdge(i+N,i,1);    for(int i=1;i<=M;i++)    {        int x=read(),y=read();        AddEdge(x,y+N,INF);        AddEdge(y,x+N,INF);    }    S=A;T=B+N;    printf("%d",Dinic());    return  0;}`

无相关信息