zoukankan      html  css  js  c++  java
  • poj 1815(最小割 + 枚举)

    好像就只有枚举这个方法, 没有看到其他的方法,挂不得题目给了2s。

    除了枚举一开始比较难想,这个网络流建图还是比较好想到的, 只有用最小割的性质,就可以知道将每个点拆成两个点,之间连一条权为1的边 ,其他相连的边都为INF。

    然后就是枚举选字典序最小的点.

    Friendship
    Time Limit: 2000MS   Memory Limit: 20000K
    Total Submissions: 7560   Accepted: 2087

    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
    

    Source

    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <string>
    #include <iostream>
    using namespace std;
    
    #define INF 0x3ffffff
    #define N 440
    
    int n;
    int nn;
    int s,t;
    int g[N][N];
    int cnt,save[N];
    int lv[N],gap[N];
    int map[N][N];
    
    int sdfs(int k,int w)
    {
        if(k==t) return w;
        int f=0;
        int mi=nn-1;
        for(int i=1;i<=2*n;i++)
        {
            if(g[k][i]>0)
            {
                if(lv[k]==lv[i]+1)
                {
                    int tmp=sdfs(i,min(g[k][i],w-f));
                    f+=tmp;
                    g[k][i]-=tmp;
                    g[i][k]+=tmp;
                    if(f==w)
                    {
                        return f;
                    }
                    if(lv[s]==nn) return f;
                }
                if(lv[i]<mi) mi=lv[i];
            }
        }
        if(f==0)
        {
            gap[lv[k]]--;
            if(gap[lv[k]]==0)
            {
                lv[s]=nn;
                return f;
            }
            lv[k]=mi+1;
            gap[lv[k]]++;
        }
        return f;
    }
    
    int sap()
    {
        nn=n*2;
        int sum=0;
        memset(lv,0,sizeof(lv));
        memset(gap,0,sizeof(gap));
        gap[0]=nn;
        while(lv[s]<nn)
        {
            sum += sdfs(s,INF);
        }
        return sum;
    }
    
    int main()
    {
        s=0;
        scanf("%d%d%d",&n,&s,&t);
        memset(g,-1,sizeof(g));
        memset(map,0,sizeof(map));
    
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                int tmp;
                scanf("%d",&tmp);
                map[i][j]=tmp;
                if(i==j||tmp==0) continue;
                if(i==s||i==t)
                {
                    g[i][j]=INF;
                    continue;
                }
                g[n+i][j]=INF;
            }
        }
        if(g[s][t]>0) 
        {
            printf("NO ANSWER!");
            return 0;
        }
        for(int i=1;i<=n;i++)
        {
            if(i!=s&&i!=t)
            {
                g[i][n+i]=1;
                g[n+i][i]=0;
            }
        }
        int ans;
        printf("%d\n",ans=sap());
        if(ans==0) return 0;
        int mark[N];
        memset(mark,0,sizeof(mark)); // 记录那些点已经删掉了
        for(int i1=1;i1<=n;i1++) // 枚举每一个点.
        {
            memset(g,-1,sizeof(g));
            if(i1==s||i1==t) continue;
            mark[i1]=1;
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    int tmp;
                    tmp = map[i][j];
                    if(i==j||tmp==0||mark[i]==1||mark[j]==1) continue; // 如果这个点被删除了
                    if(i==s||i==t)
                    {
                        g[i][j]=INF;
                        continue;
                    }
                    g[n+i][j]=INF;
                }
            }
            for(int i=1;i<=n;i++)
            {
                if(i!=s&&i!=t&&mark[i]==0)
                {
                    g[i][n+i]=1;
                    g[n+i][i]=0;
                }
            }
            int kk;
            if((kk=sap())<ans)
            {
                ans=kk;
                if(ans==0) break;
            }
            else
            {
                mark[i1]=0;
            }
        }
        for(int i=1;i<=n;i++)
            if(mark[i]==1) printf("%d ",i);
        return 0;
    }
  • 相关阅读:
    makefile文件编写
    soem函数库的编译
    加秘钥的SSH
    ssh传文件
    ssh1
    安装paramiko的方法
    Ftp客户端(上传文件)
    ftp服务端
    vi编辑器没有颜色的解决办法
    socket服务器
  • 原文地址:https://www.cnblogs.com/chenhuan001/p/2911280.html
Copyright © 2011-2022 走看看