P1345 [USACO5.4] telecommunications cows [split minimum cut point +]

Title Description

Farmer John's cows like to keep in touch by e-mail, so they built a cow computer network to communicate with each other. These machines mail transmission in the following manner: If there is a sequence of the c-a1 computer composition, a2, ..., A (c), and connected to a1 a2, a2 ​​is connected with a3, etc., and then the computer a1 a (c) can send each e-mail.

Unfortunately, sometimes the cows accidentally stepped on a computer, Farmer John's car might also run over the computer, this hapless computer will be broken. This means that this computer can not send e-mail, so associated with this computer's connection will not be used.

Have two cows thought: If the two of us can not send each other e-mail, at least how many computers broken it? Write a program for them to calculate the minimum.

In such a network as an example:

1* / 3 - 2*

This picture is two computers 3 connected. We want to transfer information between computers 1 and 2. 3,2 and direct communication with the computer 31. 3 If the computer is broken, the computer 1 and 2 can not send each other the information.

Resolve

Look, obviously the split point minimum cut, no technical content, dinic quick fix.

Note that some of the details:

  1. The starting point with the end point can not be deleted
  2. Note that all sides should all network traffic even reverse side 0

Reference Code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<queue>
#include<vector>
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define N 1010
#define M 3010
#define MOD 2520
#define E 1e-12
using namespace std;
inline int read()
{
    int f=1,x=0;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}
struct rec{
    int next,ver,leng;
}g[M<<2];
int head[N],tot=1,n,m,s,t,d[N];
inline void add(int x,int y,int val)
{
    g[++tot].ver=y,g[tot].leng=val;
    g[tot].next=head[x],head[x]=tot;
}
inline bool bfs()
{
    memset(d,0,sizeof(d));
    queue<int> q;
    d[s]=1;q.push(s);
    while(q.size()){
        int x=q.front();q.pop();
        for(int i=head[x];i;i=g[i].next){
            int y=g[i].ver,z=g[i].leng;
            if(!z||d[y]) continue;
            d[y]=d[x]+1;
            q.push(y);
            if(y==t) return 1;
        }
    }
    return 0;
}
inline int dinic(int x,int flow)
{
    if(x==t) return flow;
    int rest=flow;
    for(int i=head[x];i&&rest;i=g[i].next){
        int y=g[i].ver,z=g[i].leng;
        if(!z||d[y]!=d[x]+1) continue;
        int k=dinic(y,min(rest,z));
        if(!k) d[y]=0;
        else{
            g[i].leng-=k;
            g[i^1].leng+=k;
            rest-=k;
        }
    }
    return flow-rest;
}
int main()
{
    n=read(),m=read(),s=read(),t=read();
    for(int i=1;i<=m;++i){
        int u,v;
        u=read(),v=read();
        add(u+n,v,INF),add(v+n,u,INF);
        add(v,u+n,0),add(u,v+n,0);
    }
    for(int i=1;i<=n;++i){
        if(i==s||i==t) add(i,i+n,INF),add(i+n,i,0);
        add(i,i+n,1),add(i+n,i,0);
    }
    int now,ans=0;
    while(bfs())
        while((now=dinic(s,INF))) ans+=now;
    cout<<ans<<endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/DarkValkyrie/p/11414641.html