【u035】奶牛的电信

Time Limit: 1 second
Memory Limit: 128 MB

【问题描述】

农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流。这些机器用如下的方式发送电邮:如果存
在一个由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不能互相通信需要坏掉的电脑数目的最小值。

【数据规模】

Sample Input1

3 2 1 2
1 3
2 3
Sample Output1
1

【题目链接】:http://noi.qz5z.com/viewtask.asp?id=u035

【题解】

最小割点集问题;
做法是把每个点x化为两个点2*x和2*x+1;
这两个点的关系如下
这里写图片描述
两个点之间的边权为1;
指向x的边都直接指向2*x;从x出去的边都从2*x+1出去;
然后把出了2*x和2*x+1之间的边之外的边都设置为无穷大;
然后就能按照最小割(最大流)去做了;
因为每次找增广路的时候都会找一个边权最小的然后减掉;而显然;一个被泛化为两个点之间的边权是最小的为1;这样就只会删掉属于同一个点的两个点之间的边了,而这等价于删掉这个节点;
从2*s+1开始进行最大流终点是2*t;
如果想知道这些割点是什么;可以把每个点都假设先删掉(也即把2*x和2*x+1这两个点之间的边删掉);然后看一下最大流减少的是不是1;如果是的话说明这个点在最小点割集中;然后最小割-1再重复上述步骤就好;

【完整代码】

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long

using namespace std;

const int MAXN = 300;
const int dx[5] = {0,1,-1,0,0};
const int dy[5] = {0,0,0,-1,1};
const double pi = acos(-1.0);
const int INF = 21e8;

int n,m,s,t;
int flow[MAXN][MAXN],pre[MAXN];
bool mark[MAXN];
queue <int> dl;

void rel(LL &r)
{
    r = 0;
    char t = getchar();
    while (!isdigit(t) && t!='-') t = getchar();
    LL sign = 1;
    if (t == '-')sign = -1;
    while (!isdigit(t)) t = getchar();
    while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
    r = r*sign;
}

void rei(int &r)
{
    r = 0;
    char t = getchar();
    while (!isdigit(t)&&t!='-') t = getchar();
    int sign = 1;
    if (t == '-')sign = -1;
    while (!isdigit(t)) t = getchar();
    while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
    r = r*sign;
}

int get_f()
{
    int f= 0;
    while (true)
    {
        memset(mark,false,sizeof(mark));
        memset(pre,0,sizeof(pre));
        while (!dl.empty())
            dl.pop();
        mark[s<<1|1] = true;
        dl.push(s<<1|1);
        while (!dl.empty())
        {
            int x = dl.front();
            if (x==(t<<1))
                break;
            dl.pop();
            for (int i = 2;i <= 2*n+1;i++)
                if (flow[x][i] && !mark[i])
                {
                    mark[i] = true;
                    dl.push(i);
                    pre[i] = x;
                }
        }
        if (!mark[t<<1])
            break;
        int i = t<<1,mi = INF;
        while (i!=(s<<1|1))
        {
            mi = min(mi,flow[pre[i]][i]);
            i = pre[i];
        }
        i = t<<1;
        while (i!=(s<<1|1))
        {
            flow[pre[i]][i]-=mi;
            flow[i][pre[i]]+=mi;
            i = pre[i];
        }
        f += mi;
    }
    return f;
}

int main()
{
    //freopen("F:\\rush.txt","r",stdin);
    rei(n);rei(m);rei(s);rei(t);
    for (int i = 1;i <= n;i++)
        flow[2*i+1][2*i]=1,flow[2*i][2*i+1] = 1;
    for (int i = 1;i <= m;i++)
    {
        int x,y;
        rei(x);rei(y);
        flow[2*x+1][2*y]=INF;
        flow[2*y+1][2*x]=INF;
    }
    printf("%d\n",get_f());
    return 0;
}
posted @ 2017-10-06 19:22  AWCXV  阅读(180)  评论(0编辑  收藏  举报