zoukankan      html  css  js  c++  java
  • 最大流最小割求最小割点集(存在多种情况输出字典序最小的割点集)

    poj1815

    Friendship
    Time Limit: 2000MS   Memory Limit: 20000K
    Total Submissions: 8768   Accepted: 2462

    Description

    In modern society, each person has his own friends. Since all the people are very busy, they communicate with each other only by phone. You can assume that people A can keep in touch with people B, only if 
    1. A knows B's phone number, or 
    2. A knows people C's phone number and C can keep in touch with B. 
    It's assured that if people A knows people B's number, B will also know A's number. 

    Sometimes, someone may meet something bad which makes him lose touch with all the others. For example, he may lose his phone number book and change his phone number at the same time. 

    In this problem, you will know the relations between every two among N people. To make it easy, we number these N people by 1,2,...,N. Given two special people with the number S and T, when some people meet bad things, S may lose touch with T. Your job is to compute the minimal number of people that can make this situation happen. It is supposed that bad thing will never happen on S or T. 

    Input

    The first line of the input contains three integers N (2<=N<=200), S and T ( 1 <= S, T <= N , and S is not equal to T).Each of the following N lines contains N integers. If i knows j's number, then the j-th number in the (i+1)-th line will be 1, otherwise the number will be 0. 

    You can assume that the number of 1s will not exceed 5000 in the input. 

    Output

    If there is no way to make A lose touch with B, print "NO ANSWER!" in a single line. Otherwise, the first line contains a single number t, which is the minimal number you have got, and if t is not zero, the second line is needed, which contains t integers in ascending order that indicate the number of people who meet bad things. The integers are separated by a single space. 

    If there is more than one solution, we give every solution a score, and output the solution with the minimal score. We can compute the score of a solution in the following way: assume a solution is A1, A2, ..., At (1 <= A1 < A2 <...< At <=N ), the score will be (A1-1)*N^t+(A2-1)*N^(t-1)+...+(At-1)*N. The input will assure that there won't be two solutions with the minimal score. 

    Sample Input

    3 1 3
    1 1 0
    1 1 1
    0 1 1
    

    Sample Output

    1
    2
    题意:
    输入n个点,源点和汇点,接下来有n行。每行n个数,Gij==1代表i和j可以连通,否则:不连通,问去掉最少多少个点能使S与T不相连,
    分析:拆点用最小割求最大流,建边方式:每个点拆成i和i+n,双向边相连,权值为1,S,T除外,其权值为inf,表示不会被隔断,然后j+n到i和i+n到j建立单向边,权值为inf;

    #include"stdio.h"
    #include"string.h"
    #include"iostream"
    #define M 1009
    #define inf 999999999
    using namespace std;
    struct st
    {
        int u,v,w,next;
    }edge[400009];
    int head[M],dis[M],q[M],work[M],cnt[M],use[M],t,que[M];
    int min(int a,int b)
    {
        return a<b?a:b;
    }
    void init()
    {
        t=0;
        memset(head,-1,sizeof(head));
    }
    void add(int u,int v,int w)
    {
        edge[t].u=u;
        edge[t].v=v;
        edge[t].w=w;
        edge[t].next=head[u];
        head[u]=t++;
    }
    int bfs(int S,int T)
    {
        int rear=0;
        memset(dis,-1,sizeof(dis));
        dis[S]=0;
        q[rear++]=S;
        for(int i=0;i<rear;i++)
        {
            for(int j=head[q[i]];j!=-1;j=edge[j].next)
            {
                int v=edge[j].v;
                if(edge[j].w&&dis[v]==-1)
                {
                    dis[v]=dis[q[i]]+1;
                    q[rear++]=v;
                    if(v==T)
                        return 1;
                }
            }
        }
        return 0;
    }
    int dfs(int S,int a,int T)
    {
        if(S==T)
            return a;
        for(int &i=work[S];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            if(edge[i].w&&dis[v]==dis[S]+1)
            {
                int tt=dfs(v,min(a,edge[i].w),T);
                if(tt)
                {
                    edge[i].w-=tt;
                    edge[i^1].w+=tt;
                    return tt;
                }
            }
        }
        return 0;
    }
    int Dinic(int S,int T)
    {
        int ans=0;
        while(bfs(S,T))
        {
            memcpy(work,head,sizeof(head));
            while(int tt=dfs(S,inf,T))
                ans+=tt;
        }
        return ans;
    }
    int G[222][222];
    int main()
    {
        int S,T,n,i,j;
        while(scanf("%d%d%d",&n,&S,&T)!=-1)
        {
            init();
            for(i=1;i<=n;i++)
            {
                for(j=1;j<=n;j++)
                {
                    scanf("%d",&G[i][j]);
                }
                if(i==S||i==T)
                    G[i][i]=inf;
                add(i,i+n,G[i][i]);
                add(i+n,i,G[i][i]);
            }
            for(i=1;i<=n;i++)
            {
                for(j=1;j<=n;j++)
                {
                    if(G[i][j]&&i!=j)
                    {
                        add(i,j+n,inf);
                        add(j+n,i,0);
                    }
                }
            }
            if(G[S][T])
            {
                printf("NO ANSWER!
    ");
                continue;
            }
            int ans=Dinic(S,T);
            printf("%d
    ",ans);
            if(!ans)
                continue;
            int k=0;
            for(i=1;i<=n;i++)//从小到大枚举割点集,先隔断改点,此时的最大流cnt若比ans小,则cnt=ans;把该点入队
                //否则把改点还原
            {
                if(i!=S&&i!=T)
                {
                    for(j=1;j<=n;j++)
                    edge[j*2-1].w=edge[j*2-2].w=G[j][j];
                    edge[i*2-1].w=edge[i*2-2].w=0;
                    G[i][i]=0;//注意更改g[i][i];
                    int cnt=Dinic(S,T);
                    int flag=0;
                    if(cnt<ans)
                    {
                        flag++;
                        que[k++]=i;
                        ans=cnt;
                    }
                    if(!flag)
                    {
                        edge[i*2-1].w=edge[i*2-2].w=1;
                        G[i][i]=1;
                    }
                    if(ans==0)
                        break;
                }
            }
            for(i=0;i<k;i++)
            {
                if(i==0)
                    printf("%d",que[i]);
                else
                printf(" %d",que[i]);
            }
            printf("
    ");
        }
    }
    



  • 相关阅读:
    基于网络监听方式的电子邮件实现基础
    在一个存储过程里面执行另一个存储过程的应用
    Vim复制单个字符
    Linux下fsck修复文件系统
    (转载)2011年金山软件C++开发工程师笔试题 关于逆序输出
    (转载)C++ string详解
    (转载)Linux下网络API
    (转载)软中断和硬中断
    (转载)找工作经历总结百度offer
    (转载)Vim入门图解说明
  • 原文地址:https://www.cnblogs.com/mypsq/p/4348234.html
Copyright © 2011-2022 走看看