您的位置:首页 > 其它

poj 1815 Friendship (最小割 (贪心 最小割点集))

2015-03-03 00:02 393 查看
Friendship

Time Limit: 2000MSMemory Limit: 20000K
Total Submissions: 9513Accepted: 2647
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

朋友之间 通过电话联系 且电话联系是可以传递的

问 如果要切断S T之间的联系 最少要使多少人失去联系

如果两个人之间有联系 就建边 求最少去掉几个顶点 可以使得S T之间不连通

超级源点s=0 超级汇点t=2*N+1

s与S之间相连 权值INF T+N与t之间相连 权值INF

拆点 i与i+N之间建边 权值为1

如果i j之间有联系 i+N与j之间建边 权值INF

最后 S S+N之间权值为INF T T+N之间权值为INF 这两个点不可以去掉

求的最大值之后 因为要求得最小割点集 所以贪心枚举每个点

看删去这个点之后 最大流的值会不会减少 从而来判断是不是割点

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 10010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl;
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

struct Node
{
    int w,f;
}net[500][500];
int set[500];//最小割集
int N,S,T;
bool flag[500];
int q[500],d[500];//队列 层次网络(距离标号)
int s,t;//源点 汇点

bool bfs()
{
    int head=0,tail=0,u,v;
    MEM(flag,0);
    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)
{
    if(v==t)  return low;
    int flow;
    for(int 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;
    while(bfs())//求最大流
    {
        int flow;
        while(flow=dfs(s,INF))
            ans+=flow;
    }
    printf("%d\n",ans);
    //割点枚举
    if(ans==0) return;
    int cnt=0,temp=ans;
    for(int 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(int j=1;j<=t;j++)
            for(int k=1;k<=t;k++)
                net[j][k].f=0;
        int num=0;
        while(bfs())
        {
            int flow;
            while(flow=dfs(S,INF))
                num+=flow;
        }
        if(num!=temp)
        {
            set[cnt++]=i;//存储最小割点
            temp=num;
        }
        else  net[i][i+N].w=1;
    }
    for(int i=0;i<ans-1;i++)
        printf("%d ",set[i]);
    printf("%d\n",set[ans-1]);
}

int main()
{
//    fread;
    while(scanf("%d%d%d",&N,&S,&T)!=EOF)
    {
        MEM(net,0);
        s=0; t=2*N+1;
        add_edge(s,S,INF);  add_edge(T+N,t,INF);
        for(int i=1;i<=N;i++)
        {
            add_edge(i,i+N,1);
            for(int j=1;j<=N;j++)
            {
                int c;
                scanf("%d",&c);
                if(c)
                    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!\n");
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: