[USACO5.4]奶牛的电信Telecowmunication 最小割

题目描述

农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流。这些机器用如下的方式发送电邮:如果存在一个由c台电脑组成的序列a1,a2,...,a(c),且a1与a2相连,a2与a3相连,等等,那么电脑a1和a(c)就可以互发电邮。

很不幸,有时候奶牛会不小心踩到电脑上,农夫约翰的车也可能碾过电脑,这台倒霉的电脑就会坏掉。这意味着这台电脑不能再发送电邮了,于是与这台电脑相关的连接也就不可用了。

有两头奶牛就想:如果我们两个不能互发电邮,至少需要坏掉多少台电脑呢?请编写一个程序为她们计算这个最小值。

以如下网络为例:

1* / 3 - 2*

这张图画的是有2条连接的3台电脑。我们想要在电脑1和2之间传送信息。电脑1与3、2与3直接连通。如果电脑3坏了,电脑1与2便不能互发信息了。

输入输出格式

输入格式:

第一行 四个由空格分隔的整数:N,M,c1,c2.N是电脑总数(1<=N<=100),电脑由1到N编号。M是电脑之间连接的总数(1<=M<=600)。最后的两个整数c1和c2是上述两头奶牛使用的电脑编号。连接没有重复且均为双向的(即如果c1与c2相连,那么c2与c1也相连)。两台电脑之间至多有一条连接。电脑c1和c2不会直接相连。

第2到M+1行 接下来的M行中,每行包含两台直接相连的电脑的编号。

输出格式:

一个整数表示使电脑c1和c2不能互相通信需要坏掉的电脑数目的最小值。

输入输出样例

输入样例#1: 复制
3 2 1 2
1 3
2 3
输出样例#1: 复制
1


求最小的割点使图不连通;
显然拆点处理:
将一个点拆为出,入两个点;
容量为1;
其余连边设为inf;
那么求割的时候只能割1的边;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 200005
#define inf 0x3f3f3f3f
#define INF 9999999999
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
    ll x = 0;
    char c = getchar();
    bool f = false;
    while (!isdigit(c)) {
        if (c == '-') f = true;
        c = getchar();
    }
    while (isdigit(c)) {
        x = (x << 1) + (x << 3) + (c ^ 48);
        c = getchar();
    }
    return f ? -x : x;
}

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }

/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
    if (!b) {
        x = 1; y = 0; return a;
    }
    ans = exgcd(b, a%b, x, y);
    ll t = x; x = y; y = t - a / b * y;
    return ans;
}
*/



ll qpow(ll a, ll b, ll c) {
    ll ans = 1;
    a = a % c;
    while (b) {
        if (b % 2)ans = ans * a%c;
        b /= 2; a = a * a%c;
    }
    return ans;
}


int n, m;
int st, ed;
struct node {
    int u, v, nxt, w;
}edge[maxn<<1];

int head[maxn], cnt;

void addedge(int u, int v, int w) {
    edge[cnt].u = u; edge[cnt].v = v; edge[cnt].nxt = head[u];
    edge[cnt].w = w; head[u] = cnt++;
}

int rk[maxn];

int bfs() {
    queue<int>q;
    ms(rk);
    rk[st] = 1;
    q.push(st);
    while (!q.empty()) {
        int tmp = q.front(); q.pop();
        for (int i = head[tmp]; i != -1; i = edge[i].nxt) {
            int to = edge[i].v;
            if (rk[to] || edge[i].w <= 0)continue;
            rk[to] = rk[tmp] + 1; q.push(to);
        }
    }
    return rk[ed];
}

int dfs(int u, int flow) {
    if (u == ed)return flow;
    int add = 0;
    for (int i = head[u]; i != -1 && add < flow; i = edge[i].nxt) {
        int v = edge[i].v;
        if (rk[v] != rk[u] + 1 || !edge[i].w)continue;
        int tmpadd = dfs(v, min(edge[i].w, flow - add));
        if (!tmpadd) { rk[v] = -1; continue; }
        edge[i].w -= tmpadd; edge[i ^ 1].w += tmpadd;
        add += tmpadd;
    }
    return add;
}

int ans;
void dinic() {
    while (bfs())ans += dfs(st, inf);
}

int main()
{
    //ios::sync_with_stdio(0);
    memset(head, -1, sizeof(head));
    rdint(n);rdint(m);rdint(st);rdint(ed);
    for(int i=1;i<=n;i++)addedge(i,i+n,1),addedge(i+n,i,0);
    for(int i=0;i<m;i++){
        int u,v;rdint(u);rdint(v);
        addedge(u+n,v,inf);addedge(v,u+n,0);
        addedge(v+n,u,inf);addedge(u,v+n,0);

    }
    st+=n;
    dinic();
    cout<<ans<<endl;
    return 0;
}
 
  

猜你喜欢

转载自www.cnblogs.com/zxyqzy/p/10187794.html