Description
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
You can assume that the number of 1s will not exceed 5000 in the input.
Output
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<algorithm>
const int inf=(1<<28);
using namespace std;
#define maxn 5000
struct edge
{
int u,v,next,f,pre;
int flow;
}e[2*maxn];//maxn不能开太大 否则超时
int num,rnum;
int head[maxn],rhead[maxn];
int d[maxn];
int numb[maxn];
int start[maxn];
int n;//见图后的点数(1->n)
int p[maxn];
int source,sink;
//要初始化source 和 sink,重定义n
void Init()
{
memset(head,-1,sizeof(head));
memset(rhead,-1,sizeof(rhead));
memset(p,-1,sizeof(p));
num=0;
return ;
}
void BFS()
{
int i,j;
for(i=1;i<=n;i++)
{
d[i]=n;
numb[i]=0;
}
int Q[maxn],head(0),tail(0);
d[sink]=0;
numb[0]=1;
Q[++tail]=sink;
while(head<tail)
{
i=Q[++head];
for(j=rhead[i];j!=-1;j=e[j].pre)
{
if(e[j].f==0||d[e[j].u]<n)
continue;
d[e[j].u]=d[i]+1;
numb[d[e[j].u]]++;
Q[++tail]=e[j].u;
}
}
return ;
}
int Augment()
{
int i;
int tmp=inf;
for(i=p[sink];i!=-1;i=p[e[i].u])
{
if(tmp>e[i].f)
tmp=e[i].f;
}
for(i=p[sink];i!=-1;i=p[e[i].u])
{
e[i].f-=tmp;e[i].flow+=tmp;
e[i^1].f+=tmp;e[i^1].flow-=tmp;
}
return tmp;
}
int Retreat(int &i)
{
int tmp,j,mind(n-1);
for(j=head[i];j!=-1;j=e[j].next)
{
if(e[j].f>0&&d[e[j].v]<mind)
mind=d[e[j].v];
}
tmp=d[i];
d[i]=mind+1;
numb[tmp]--;
numb[d[i]]++;
if(i!=source)
i=e[p[i]].u;
return numb[tmp];
}
int maxflow()
{
int flow(0),i,j;
BFS();
for(i=1;i<=n;i++)
start[i]=head[i];
i=source;
while(d[source]<n)
{
for(j=start[i];j!=-1;j=e[j].next)
if(e[j].f>0&&d[i]==d[e[j].v]+1)
break;
if(j!=-1)
{
start[i]=j;
p[e[j].v]=j;
i=e[j].v;
if(i==sink)
{
flow+=Augment();
i=source;
}
}
else
{
start[i]=head[i];
if(Retreat(i)==0)
break;
}
}
return flow;
}
//a->b=c;
void addedge(int a,int b,int c)
{
e[num].next=head[a];
e[num].flow=0;
head[a]=num;
e[num].pre=rhead[b];
rhead[b]=num;
e[num].f=c;
e[num].u=a;
e[num++].v=b;
e[num].next=head[b];
e[num].flow=0;
head[b]=num;
e[num].pre=rhead[a];
rhead[a]=num;
e[num].u=b;
e[num].v=a;
e[num++].f=0;
return ;
}
int mat[300][300];
int main()
{
int m,s,t;
while(scanf("%d%d%d",&m,&s,&t)==3)
{
Init();
n=m*2;
source=s;sink=t+m;
for(int i=1;i<=m;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%d",&mat[i][j]);
if(i==j&&mat[i][j])
{
if(i==s||i==t) addedge(i,i+m,inf);
else addedge(i,i+m,1);
}
else if(i!=j&&mat[i][j])
{
addedge(i+m,j,inf);
}
}
}
if(mat[s][t]) {printf("NO ANSWER!\n");continue;}
int cnt=maxflow();
printf("%d\n",cnt);
int ans[1000];int ln=0;
//枚举顶点
for(int l=1;l<=m;l++)
{
if(l==s||l==t) continue;
Init();
for(int i=1;i<=m;i++) mat[l][i]=-mat[l][i],mat[i][l]=-mat[i][l];
for(int i=1;i<=m;i++)
{
for(int j=1;j<=m;j++)
{
if(i==j&&mat[i][j]>0)
{
if(i==s||i==t) addedge(i,i+m,inf);
else addedge(i,i+m,1);
}
else if(i!=j&&mat[i][j]>0)
{
addedge(i+m,j,inf);
}
}
}
int tmp=maxflow();
if(tmp<cnt) ans[ln++]=l,cnt--;
else for(int i=1;i<=m;i++) mat[l][i]=-mat[l][i],mat[i][l]=-mat[i][l];
}
if(ln)
{
printf("%d",ans[0]);
for(int i=1;i<ln;i++) printf(" %d",ans[i]);printf("\n");
}
}
return 0;
}
/*
9 1 9
1 1 1 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0
1 1 1 0 1 1 0 0 0
0 1 0 1 0 0 1 0 0
0 1 1 0 1 0 1 1 0
0 0 1 0 0 1 0 1 0
0 0 0 1 1 0 1 1 1
0 0 0 0 1 1 1 1 1
0 0 0 0 0 0 1 1 1
4 1 4
1 1 1 0
1 1 0 1
1 0 1 1
0 1 1 1
*/
本文探讨了在现代社交网络中,如何通过图论及网络流算法确定最小数量的关键人物,一旦这些人物失去联系会导致两个特定个体之间的沟通中断。文章详细介绍了问题背景、输入输出格式及示例,并提供了一种基于最大流最小割算法的解决方案。
2783

被折叠的 条评论
为什么被折叠?



