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



Friendship

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

Submit 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;
}

时间: 2024-11-08 23:38:47

poj--1815--Friendship(最小割点集)(枚举求最小字典序)的相关文章

POJ 1815 - Friendship - [拆点最大流求最小点割集][暴力枚举求升序割点] - [Dinic算法模板 - 邻接矩阵型]

妖怪题目,做到现在:2017/8/19 - 1:41-- 不过想想还是值得的,至少邻接矩阵型的Dinic算法模板get√ 题目链接:http://poj.org/problem?id=1815 Time Limit: 2000MS Memory Limit: 20000K Description In modern society, each person has his own friends. Since all the people are very busy, they communic

Poj 1815 Friendship 枚举+求最小割

给以一个图和两个点S,T,问你拿掉最少多少个点可以使得S和T不连通.输出点数并且输出拿掉的是哪些点,如果有多种方法就输出字典序最小的那个. 这就是一个求最小点割集的问题.无向(有向)图G中,给定源点s和终点t,至少要删去多少个点(具体一点,删哪些点),使得s和t不连通.这个问题就是点连通度,也叫最小点割集. 解法其实理解起来不难,只要把图中的每一个点v拆成v',v''两个点,并建立<v',v''>权为1,这样就把最小点割集转化成求最小割的问题. 对于原图的转化也很简单,对于原来的每条边,转化成

poj 1815 Friendship (最小割+拆点+枚举)

题意: 就在一个给定的无向图中至少应该去掉几个顶点才能使得s和t不联通. 算法: 如果s和t直接相连输出no answer. 把每个点拆成两个点v和v'',这两个点之间连一条权值为1的边(残余容量) v和v''分别是一个流进的点,一个流出的点. 根据求最小割的性质,权值小的边是可能被选择的(断开的). 添加源点st=0和汇点en=2*n+1,源点与s连权值为inf的边,t''与汇点连权值为inf的边. s与s'',t与t''连权值为inf的边,这样保证自己和自己是不会失去联系的. 如果i和j有边

POJ 1815 Friendship(最小割)

http://poj.org/problem?id=1815 Friendship Time Limit: 2000MS   Memory Limit: 20000K Total Submissions: 9026   Accepted: 2534 Description In modern society, each person has his own friends. Since all the people are very busy, they communicate with eac

POJ 1815 Friendship(最小割)

POJ 1815 Friendship 链接:http://poj.org/problem?id=1815 题目:在现代社会,每个人都有自己的朋友.由于每个人都很忙,他们只通过电话联系.你可以假定A 可以和B 保持联系,当且仅当: (1) A 知道B 的电话号码,或 (2) A 知道C 的号码,而C 能联系上B. 如果A 知道B 的电话号码,则B 也知道A 的电话号码.有时,有人可能会碰到比较糟糕的事情,导致他与其他人失去联系.例如,他可能会丢失了电话簿,或者换了电话号码. 在本题中,告知N 个

poj 1815 Friendship 最小割输出最小方案

这题卡了好久啊,最小割模型很容易想,拆点就行.就像poj的Thieves一样 每个点 a拆成 a->a',容量为1. 其他相连的点 a'->b ,容量为INF 源连接s',t连接汇 问题在于输出最小的割集 更好的方法我还不会,只能枚举. 这里基于贪心的思想,从小到大删边, 若删除i->i',会使得最小割变小,则输出i,并且i->i'这条边不要恢复 若最小割不变,则恢复这条边,继续枚举. 一开始就是因为恢复了要割去的边,无限WA. #include<cstdio> #in

ACM/ICPC 之 Dinic+枚举最小割点集(可做模板)(POJ1815)

最小割的好题,可用作模板. //Dinic+枚举字典序最小的最小割点集 //Time:1032Ms Memory:1492K #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<queue> using namespace std; #define MAXN 205 #define INF 0x3f3f3f3f int N, S, T

【POJ 2400】 Supervisor, Supervisee(KM求最小权匹配)

[POJ 2400] Supervisor, Supervisee(KM求最小权匹配) Supervisor, Supervisee Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2538   Accepted: 719 Description Suppose some supervisors each get to hire a new person for their department. There are N

POJ 3308 Paratroopers 最小点权覆盖 求最小割

不懂这个建模是什么原理,以后把二分图相关的东西看完再补上把= = #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #i

poj 1815 Friendship 字典序最小+最小割

题目链接:http://poj.org/problem?id=1815 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 kno