zoukankan      html  css  js  c++  java
  • poj 1815

    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<map>
    #include<set>
    #include<stack>
    #include<queue>
    #include<cmath>
    #include<vector>
    #include<cstdio>
    #include<string>
    #include<cstring>
    #include<cstdlib>
    #include<iostream>
    #include<algorithm>
    #define  inf 0x0f0f0f0f
    
    using namespace std;
    
    int Map[200+10][200+10],g[200+10][200+10];
    int N,S,T;
    bool vist[200+10];
    
    struct Edge
    {
        int from,to,cap,flow;
    };
    
    struct DINIC{
        static const int maxn=2500+10;
        int n,m,s,t;
        vector<Edge>edges;
        vector<int>G[maxn];
        int d[maxn],cur[maxn];
        bool vis[maxn];
    
        void AddEdge(int from,int to,int cap)
        {
            Edge temp;
            temp.cap=cap; temp.flow=0; temp.from=from; temp.to=to;
            edges.push_back(temp);
            temp.cap=0; temp.flow=0; temp.from=to; temp.to=from;
            edges.push_back(temp);
            m=edges.size();
            G[from].push_back(m-2);
            G[to].push_back(m-1);
        }
    
        bool BFS()
        {
            memset(vis,0,sizeof(vis));
            queue<int>Q;
            Q.push(s);
            d[s]=0;
            vis[s]=1;
            while(!Q.empty())
            {
                int x=Q.front();Q.pop();
                for (int i=0;i<G[x].size();i++)
                {
                    Edge& e=edges[G[x][i]];
                    if (!vis[e.to] && e.cap>e.flow)
                    {
                        vis[e.to]=1;
                        d[e.to]=d[x]+1;
                        Q.push(e.to);
                    }
                }
            }
            return vis[t];
        }
    
        int DFS(int x,int a)
        {
            if (x==t || a==0) return a;
            int flow=0,f;
            for (int& i=cur[x];i<G[x].size();i++)
            {
                Edge& e=edges[G[x][i]];
                if (d[x]+1==d[e.to] && (f=DFS(e.to,min(a,e.cap-e.flow)))>0)
                {
                    e.flow+=f;
                    edges[G[x][i]^1].flow-=f;
                    flow+=f;
                    a-=f;
                    if (a==0) break;
                }
            }
            return flow;
        }
    
        int Dinic()
        {
            int flow=0;
            while (BFS())
            {
                memset(cur,0,sizeof(cur));
                flow+=DFS(s,inf);
            }
            return flow;
        }
    
        void init()
        {
            for (int i=0;i<=maxn;i++) G[i].clear();
            edges.clear();
        }
    };
    
    DINIC friends;
    
    void build()
    {
        friends.init();
        for (int i=1;i<=N;i++)
        {
            if (i==S || i==T)
            {
                friends.AddEdge(i,i+N,inf);
            }
            else friends.AddEdge(i,i+N,1);
            for (int j=1;j<=N;j++)
            {
                if (Map[i][j] && i!=j) friends.AddEdge(i+N,j,inf);
            }
        }
    }
    
    int main()
    {
        //freopen("in.txt","r",stdin);
        while(scanf("%d%d%d",&N,&S,&T)!=EOF)
        {
            friends.s=S; friends.t=T+N; friends.n=2*N;
            for (int i=1;i<=N;i++)
            for (int j=1;j<=N;j++){
                scanf("%d",&Map[i][j]);
            }
            if (Map[S][T]==1)
            {
                printf("NO ANSWER!
    ");
                continue;
            }
            build();
            int ans=friends.Dinic();
            printf("%d
    ",ans);
            memset(vist,0,sizeof(vist));
            for (int i=1;i<=N;i++)
            {
                if (i==S || i==T)
                continue;
                for (int j=1;j<=N;j++)
                for (int k=1;k<=N;k++)
                {
                    g[j][k]=Map[j][k];
                    if (j==i || k==i) Map[j][k]=0;
                }
                build();
                int ans1=friends.Dinic();
                if (ans1<ans) {
                    vist[i]=true;
                    ans--;
                }
                else
                {
                    for (int j=1;j<=N;j++)
                    for (int k=1;k<=N;k++)
                    Map[j][k]=g[j][k];
                }
            }
            int k=1;
            for (int i=1;i<=N;i++)
            {
                if (vist[i])
                {
                    if (k==1) {printf("%d",i);k++;}
                    else printf(" %d",i);
                }
            }
            printf("
    ");
        }
        return 0;
    }
    至少做到我努力了
  • 相关阅读:
    部署时,出现用户代码未处理 System.Security.Cryptography.CryptographicException 错误解决方法
    sqlserver学习笔记(三)—— 为数据库添加新的用户
    [.Net MVC] Win7下IIS部署
    CSS的4种引入方式及优先级
    阿里巴巴图标库全部下载
    div的默认position值是静态的static
    阿里巴巴图标库iconfont上传svg后,显示不了图片
    ext.net单元格内容换行显示
    WEB内容换行
    SQL修改日期类型字段为字符串类型
  • 原文地址:https://www.cnblogs.com/chensunrise/p/3785702.html
Copyright © 2011-2022 走看看