poj 1815 Friendship //最小割

Friendship
Time Limit: 2000MS   Memory Limit: 20000K
Total Submissions: 3921   Accepted: 976

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

POJ Monthly

 

纠结啊,纠结!

首先是构图,比较容易看出,这是最小割可以做的题目。

 

因为是无向图

首先是将每个点拆点,比如将i点拆成i,i+n点,两点之间连边,边长为1,这么设置可以让每个人的作用只出现一次,也能被最小割割到。

接着就是若两点之间相连,那么就让i+n和j相连,容量为无穷大。从而构造了最小割模型

注意S,T这两个点的构造有所不同,连向S+N,T+N的边的容量是无穷大,这样可以让他们不被最小割割中。。。

这个模型可以说是比较经典的一个模型了

 

主要是按字母序输出这个比较麻烦。

因为人数是一定的,可以依次枚举。

每次枚举一个人,把和他的联系全部断开,重新构图。

如果断开这个人的联系得到的流比原流小,那么就将原流减一,并对这个人做记录。如果不比原流小,那么就还原图

 

此题是看着别人的想法过的,还有很多地方不是彻底明白,还需要一定的时间去练习最小割啊

 

革命尚未成功,同志仍需努力

 

#include<cstdio>
#include<cstring>
const int N=410;
const int M=40001;
const int inf=0x7fffffff;
int head[N];
int map[201][201],ss,tt,gmap[201][201];
bool cas[N];
int ans[N];
struct Edge
{
    int v,next,w;
} edge[M];
int cnt,n,s,t,m;
void addedge(int u,int v,int w)
{
    edge[cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].next=head[u];
    head[u]=cnt++;
    edge[cnt].v=u;
    edge[cnt].w=0;
    edge[cnt].next=head[v];
    head[v]=cnt++;
}
int sap()
{
    int pre[N],cur[N],dis[N],gap[N];
    int flow=0,aug=inf,u;
    bool flag;
    for(int i=0; i<n; i++)
    {
        cur[i]=head[i];
        gap[i]=dis[i]=0;
    }
    gap[s]=n;
    u=pre[s]=s;
    while(dis[s]<n)
    {
        flag=0;
        for(int &j=cur[u]; j!=-1; j=edge[j].next)
        {
            int v=edge[j].v;
            if(edge[j].w>0&&dis[u]==dis[v]+1)
            {
                flag=1;
                if(edge[j].w<aug) aug=edge[j].w;
                pre[v]=u;
                u=v;
                if(u==t)
                {
                    flow+=aug;
                    while(u!=s)
                    {
                        u=pre[u];
                        edge[cur[u]].w-=aug;
                        edge[cur[u]^1].w+=aug;
                    }
                    aug=inf;
                }
                break;
            }
        }
        if(flag) continue;
        int mindis=n;
        for(int j=head[u]; j!=-1; j=edge[j].next)
        {
            int v=edge[j].v;
            if(edge[j].w>0&&dis[v]<mindis)
            {
                mindis=dis[v];
                cur[u]=j;
            }
        }
        if((--gap[dis[u]])==0)
            break;
        gap[dis[u]=mindis+1]++;
        u=pre[u];
    }
    return flow;
}
void back()
{
    for(int i=1;i<=m;i++)
      for(int j=1;j<=m;j++)
      map[i][j]=gmap[i][j];
}
void change(int x)
{
    for(int i=1;i<=m;i++)
      for(int j=1;j<=m;j++)
        gmap[i][j]=map[i][j];
    for(int i=1;i<=m;i++)
      for(int j=1;j<=m;j++)
      if(i==x||j==x)  map[i][j]=0;
}
void buildgraph()
{
        cnt=0;
        memset(head,-1,sizeof(head));
        for(int i=1;i<=m;i++)
          for(int j=1;j<=m;j++)
          if(i!=j)
          {
              if(map[i][j]==1)  addedge(i+m,j,inf);
          }
          else
          {
              if(i==ss||i==tt)  addedge(i,i+m,inf);
              else addedge(i,i+m,1);
          }
}
int  main()
{
    while(scanf("%d%d%d",&n,&ss,&tt)!=EOF)//模板从0开始的
    {
        memset(cas,0,sizeof(cas));
        s=ss;
        t=tt+n;
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                    scanf("%d",&map[i][j]);
        if(map[ss][tt]==1)
        {
            printf("NO ANSWER!/n");
            continue;
        }
        m=n;
        buildgraph();
        n=n*2+1;
        int flow=sap();
        printf("%d/n",flow);
        if(flow==0)  continue;
        int num=0;
        for(int i=1;i<=m;i++)
        {
            if(i==ss||i==tt)  continue;
            change(i);
            buildgraph();
            int t=sap();
            if(t<flow)
            {
                flow--;
                cas[i]=1;
                ans[num++]=i;
            }else back();
            if(t==0)  break;
        }
        for(int i=0;i<num-1;i++)
        printf("%d ",ans[i]);
        printf("%d/n",ans[num-1]);
    }
    return 0;
}

你可能感兴趣的:(c,struct,SAP,input,each,output)