POJ1815 Friendship 枚举+拆点+最小割

本题是一个无向图,然后让我们求出去掉最少的点,可以使s和t不连通,并要求我们输出字典序最小的一种方案。
看出了是最小割,但没想到要拆点,又学到了。
建图:
1:把除了s和t外其他所有点连一条弧(i,i+n,1)(这条弧表示i没有丢失自己的联系人,没有这条弧,说明i不能喝他的朋友联系了。因为i能联系到的人我是用i+n相连的)
2:如果i知道s的号码,则连一条弧(s,i,INF),
     如果i知道t的号码,则连一条弧(i+n,t,INF)
3:如果i知道j的号码 ,则连弧(i+n,j,INF) 和(j+n,i,INF)
然后求一个最小割
 
然后是字典序最小得方案输出。
从小到大枚举,确保字典序最小。
先去掉最小的点,判断最小割是否减小,减小则入队,否则从新将点加入。直到最小割变成0为止(此时s到t不连通)
 
 
 
Friendship
Time Limit: 2000MS Memory Limit: 20000K
Total Submissions: 6898 Accepted: 1908

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<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>

using namespace std;

#define mm(a) memset((a),0,sizeof((a)))
#define INF 0xFFFFFF
#define MAXN 500

struct node
{
	int to,c;
	int next;
}e[9999999];

int n,m,en,dis[MAXN],gap[MAXN];
int st,ed;
int g[MAXN];
int matrix[210][210];
int que[MAXN];
bool deleted[MAXN/2];

void add(int a,int b,int c)
{
	e[en].to=b;
	e[en].c=c;
	e[en].next=g[a];
	g[a]=en++;
	e[en].to=a;
	e[en].c=0;
	e[en].next=g[b];
	g[b]=en++;	
}

int sap(int u,int flow)
{
	if(u==ed) return flow;
	int ans=0,i,t;
	for(i=g[u];i!=-1;i=e[i].next)
	{
		if( e[i].c && dis[u]==dis[e[i].to]+1)
		{
			t=sap(e[i].to,min(flow-ans,e[i].c));
			e[i].c-=t,e[i^1].c+=t,ans+=t;
			if(ans==flow) return ans;
		}
	}
	if(dis[st]>=n*2) return ans;
	if(!--gap[dis[u]]) dis[st]=n*2;
	++gap[++dis[u]];
	return ans;
}

void build()
{
	en=0;
	memset(g,-1,sizeof(g));
	for(int i=1;i<=n;i++) 
		if(i!=st && i!=ed && !deleted[i]) add(i,i+n,1);
	for(int i=1;i<=n;i++)
	{
		if(i!=st)
			if(matrix[st][i]) add(st,i,INF);
		if(i!=ed )
			if(matrix[ed][i]) add(i+n,ed+n,INF);
	}
	for(int i=1;i<=n;i++)
	{
		if(i==st || i==ed)	continue;
		for(int j=1;j<=n;j++)
		{
			if(i==j) continue;
			if(matrix[i][j]) add(i+n,j,INF);
			
		}
	}
}

void solve()
{
	int ans=0,temp;
	memset(deleted,0,sizeof(deleted));
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			scanf("%d",&matrix[i][j]);
	if(matrix[st][ed]==1)
	{
		printf("NO ANSWER!\n");
		return;
	}
	build();
	mm(gap),mm(dis);
	ed=ed+n;
	for(gap[0]=n*2;dis[st]<n*2;)
		ans+=sap(st,INF);
	printf("%d\n",ans);
	if(!ans) return ;
	int t=0,f=0;
	for(int i=1,j=ans;j;i++)
	{
		temp=j;
		j=0;
		deleted[i]=true;
		ed=ed-n;
		build();
		mm(gap),mm(dis);
		ed+=n;
		for(gap[0]=n*2;dis[st]<n*2;)
			j+=sap(st,INF);
		if(j<temp)
			que[t++]=i;
		else
			deleted[i]=false;
	}
	while(f<t)
	{
		printf("%d",que[f++]);
		if(f!=t)
			printf(" ");
	}
	printf("\n");	
}

int main()
{
	while(scanf("%d%d%d",&n,&st,&ed)!=EOF) solve();
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值