• poj1815


                                                                                                   Friendship

    Time Limit: 2000MS   Memory Limit: 20000K
    Total Submissions: 8233   Accepted: 2297

    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
    
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #define INF 1000000
    
    struct NODE
    {
        int w, f;
    };
    NODE net[500][500];
    int set[500];        //最小割点集
    int N, S, T;
    bool flag[500];        //标志数组
    int q[500], d[500]; //队列;层次网络(距离标号)
    int s, t;            //源点;汇点
    
    int min( int a, int b )
    {
        return a < b ? a : b;
    }
    
    bool BFS( )    //构建层次网络
    {
        int head, tail, u, v;
        head = 0, tail = 0;    //初始化
        memset( flag, 0, sizeof(flag) );
        q[tail++] = t, d[t] = 0, flag[t] = 1;
        while( head < tail )
        {
            u = q[head++];
            for( v = 0; v <= t; v++ )
            {
                if( !flag[v] && net[v][u].w>net[v][u].f )
                {
                    d[v] = d[u] + 1;
                    q[tail++] = v;  flag[v] = 1;
                }
                if( flag[s] )  return 1;
            }
        }
        return 0;
    }
    
    int DFS( int v, int low )    //DFS增广
    {
        int i;
        if( v == t )  return low;
        int flow;
        for( i=0; i <= t; i++ )
        {
            if( net[v][i].w>net[v][i].f && d[v]==d[i] + 1 )
            {
                if( flow = DFS( i, min(low,net[v][i].w-net[v][i].f) ) )    //递归调用
                {
                    net[v][i].f += flow;    //修改流量
                    net[i][v].f =- net[v][i].f;
                    return flow;
                }
            }
        }
        return 0;
    }
    
    void Add_Edge( int a, int b, int c )
    {
        net[a][b].w = c;
    }
    
    void Dinic( )
    {
        int ans = 0, c, k, i, a, b, cnt, temp;
        while( BFS( ) )    //求最大流
        {
            int flow;
            while( flow = DFS(s,INF) )  ans += flow;
        }
        printf( "%d
    ", ans );
        //开始枚举
        if( ans == 0 )  return;
        cnt = 0;  temp = ans;
        for( i = 1; i <= N && temp; i++ )
        {
            if( i == S || i == T )  continue;
            if( !net[i][i + N].f )  continue;
            net[i][i + N].w = 0;
            for( a = 1; a <= t; a++ )
            {
                for( b = 1; b <= t; b++ )
                    net[a][b].f = 0;
            }
            k = 0;
            while( BFS( ) )
            {
                int flow;
                while( flow = DFS(S,INF) )
                    k += flow;
            }
            if( k != temp )
            {
                set[cnt++] = i;  //将顶点存入最小割点集
                temp = k;
            }
            else  net[i][i + N].w = 1;
        }
        for( c=0; c < ans-1; c++ )  printf( "%d ", set[c] );    //输出
        printf( "%d
    ", set[c] );
    }
    
    int main( )
    {
        int tail, i, j;
        scanf( "%d%d%d", &N, &S, &T );
        memset( net, 0, sizeof(net) );    //初始化
        s = 0;  t = 2*N + 1;
        Add_Edge( s, S, INF ); 
        Add_Edge( T + N, t, INF );
        for( i=1; i <= N; i++ )
        {
            Add_Edge( i, i + N, 1 );
            for( j=1; j <= N; j++ )
            {
                scanf( "%d", &tail );
                if( tail )  Add_Edge( i + N, j, INF );
            }
        }
        Add_Edge( S, S + N, INF );   Add_Edge( T, T + N, INF ); 
        if( !net[S + N][T].w )  Dinic( );
        else  printf( "NO ANSWER!
    " );  //源点汇点相连时直接输出
        return 0;
    }
  • 相关阅读:
    try,except,finally的用法
    python实现蓝牙通信
    分布式全局ID的几种生成方案
    为什么要两次调用encodeURI来解决乱码问题
    jenkins配置到gitlab拉代码
    查看IOS-app证书到期时间
    使用SSH方式实现Git远程连接GitHub/gitlab
    Git 分支
    jenkins构建后操作archive the artfacts的用法
    MAC 安装jenkins
  • 原文地址:https://www.cnblogs.com/Deng1185246160/p/3271949.html
Copyright © 2020-2023  润新知