码迷,mamicode.com
首页 > 其他好文 > 详细

poj--1815--Friendship(最小割点集)(枚举求最小字典序)

时间:2016-05-12 20:24:36      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:


Time Limit: 2000MS   Memory Limit: 20000KB   64bit IO Format: %I64d & %I64u

 Status

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
n个人,n*n的矩阵表示每个人之间的关系,1表示认识,0表示不认识,现在要删除最少的点,使得S还有H不认识,输出字典序最小的方案
一个比较普通的最小割点集,建图之后,我们每次最大流求出的最大流都是最小割的费用,枚举每一个可以删除的点,如果说删除这个点之后最大流返回值减小,说明这个点删除是有意义的,我们就把他加入删点数组中,进行下一次遍历,如果S已经达不到H,说明最小割目的已经实现
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
#define MAXN 600
#define MAXM 1000000
#define INF 0x3f3f3f3f
int n,S,H,cnt;
int vis[MAXN],dis[MAXN],cur[MAXN],head[MAXN],rec[MAXN];
int map[MAXN][MAXN],temp[MAXN][MAXN];
struct node
{
	int u,v,cap,flow,next;
}edge[MAXM];
void add(int a,int b,int c)
{
	node E={a,b,c,0,head[a]};
	edge[cnt]=E;
	head[a]=cnt++;
	node E1={a,b,0,0,head[b]};
	edge[cnt]=E1;
	head[b]=cnt++;
}
bool BFS(int s,int t)
{
	queue<int>q;
	memset(vis,0,sizeof(vis));
	memset(dis,-1,sizeof(dis));
	q.push(s);
	vis[s]=1;
	dis[s]=0;
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		for(int i=head[u];i!=-1;i=edge[i].next)
		{
			node E=edge[i];
			if(E.cap>E.flow&&!vis[E.v])
			{
				vis[E.v]=1;
				dis[E.v]=dis[E.u]+1;
				if(E.v==t) return true;
				q.push(E.v);
			}
		}
	}
	return false;
}
int DFS(int x,int a,int t)
{
	if(a==0||x==t) return a;
	int flow=0,f;
	for(int &i=cur[x];i!=-1;i=edge[i].next)
	{
		node &E=edge[i];
		if(dis[x]+1==dis[E.v]&&(f=DFS(E.v,min(E.cap-E.flow,a),t))>0)
		{
			a-=f;
			flow+=f;
			edge[i].flow+=f;
			edge[i^1].flow-=f;
			if(a==0) break;
		}
	}
	return flow;
}
int MAXflow(int s,int t)
{
	int flow=0;
	while(BFS(s,t))
	{
		memcpy(cur,head,sizeof(head));
		flow+=DFS(s,INF,t);
	}
	return flow;
}
void getmap()
{
	cnt=0;
	memset(head,-1,sizeof(head));
	for(int i=1;i<=n;i++)//最小割建图 
	{
		for(int j=1;j<=n;j++)
		{
			if(map[i][j])
			{
				if(i==j)
				{
					if(i==S||i==H)
						add(i,i+n,INF);
					else
						add(i,i+n,1);
				}
				else
					add(i+n,j,INF);
			}
		}
	}
}
int main()
{
	while(scanf("%d%d%d",&n,&S,&H)!=EOF)
	{
		memset(map,0,sizeof(map));
		memset(head,-1,sizeof(head));
		cnt=0;
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=n;j++)
			{
				scanf("%d",&map[i][j]);
			}
		}
		getmap();
		if(map[S][H])
		{
			printf("NO ANSWER!\n");
			continue;
		}
		int ans=MAXflow(S,H+n);
		if(ans==0)
		{
			printf("0\n");
			continue;
		}
		int top=0;
		for(int i=1;i<=n;i++)
		{
			if(i==S||i==H)
			continue;
			for(int j=1;j<=n;j++)//枚举每一个可以删除的点 
			{
				temp[i][j]=map[i][j];
				temp[j][i]=map[j][i];
				map[i][j]=map[j][i]=0;
			}
			getmap();
			int t=MAXflow(S,H+n);
			if(t<ans)//如果删点之后费用减小 
			rec[top++]=i,ans=t;
			else
			{
				for(int j=1;j<=n;j++)
				{
					map[i][j]=temp[i][j];
					map[j][i]=temp[j][i];//如果没有减小,回复 
				}
			}
		}
		printf("%d\n",top);
		for(int i=0;i<top;i++)
		{
			if(i) printf(" ");
			printf("%d",rec[i]);
		}
		printf("\n");
	}
	return 0;
}

poj--1815--Friendship(最小割点集)(枚举求最小字典序)

标签:

原文地址:http://blog.csdn.net/qq_29963431/article/details/51355602

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!