您的位置:首页 > 其它

poj1815Friendship【最小割】

2016-07-27 19:50 260 查看
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,有哪些人去掉后依旧可以联系(去掉是指每次去掉其中一个)

做法:很明显的最小割拆点模型,自己i-i'流量1,相连的边流量正无穷,存储最开始的矩阵,每次把某点的相连边都去掉跑网络流。

可以认为是一种常用的技巧(我真觉得我好像做过这个题,要么就是类似的)去掉这个点后,如果是答案,最小割的流量比之前少一,这个点就不用加回去啦

#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int mm=1000000;
const int mn=505*505*3;
const int oo=1000000000;
int node,src,dest,edge;
int reach[mm],flow[mm],nxt[mm];
int head[mn],work[mn],dis[mn],q[mn];
inline int min(int a,int b)
{
return a<b?a:b;
}
inline void prepare(int _node,int _src,int _dest)
{
node=_node,src=_src,dest=_dest;
for(int i=0;i<node;++i)head[i]=-1;
edge=0;
}
inline void addedge(int u,int v,int c1,int c2=0)
{
reach[edge]=v,flow[edge]=c1,nxt[edge]=head[u],head[u]=edge++;
reach[edge]=u,flow[edge]=c2,nxt[edge]=head[v],head[v]=edge++;
}
bool Dinic_bfs()
{
int i,u,v,l,r=0;
for(i=0;i<node;++i)dis[i]=-1;
dis[q[r++]=src]=0;
for(l=0;l<r;++l)
for(i=head[u=q[l]];i>=0;i=nxt[i])
if(flow[i]&&dis[v=reach[i]]<0)
{
dis[q[r++]=v]=dis[u]+1;
if(v==dest)return 1;
}
return 0;
}
int Dinic_dfs(int u,int exp)
{
if(u==dest)return exp;
for(int &i=work[u],v,tmp;i>=0;i=nxt[i])
if(flow[i]&&dis[v=reach[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)
{
flow[i]-=tmp;
flow[i^1]+=tmp;
return tmp;
}dis[u]--;
return 0;
}
int Dinic_flow()
{
int i,ret=0,delta;
while(Dinic_bfs())
{
for(i=0;i<node;++i)work[i]=head[i];
while(delta=Dinic_dfs(src,oo))ret+=delta;
}
return ret;
}
///////////////////////////////
int n,s,t,a;
bool ans[410][410],tmp[210][210];
void buildgraph()
{
prepare(2*n,s<<1,t<<1|1);
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i!=j&&ans[i][j])
addedge(i<<1|1,j<<1,oo);//printf("i=%d,j=%d\n",i,j);
}
if(i==s||i==t)addedge(i<<1,i<<1|1,oo);
else addedge(i<<1,i<<1|1,1);
}
}
////////////////////////////
int main()
{
// freopen("cin.txt","r",stdin);

scanf("%d%d%d",&n,&s,&t);
{
bool fl=0;
s--;t--;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
scanf("%d",&a);
ans[i][j]=a;//
if(a==1&&i==s&&j==t)fl=1;
}
}
if(fl)printf("NO ANSWER!\n");
else
{
buildgraph();
int flowmax=Dinic_flow();
printf("%d\n",flowmax);
if(flowmax)
{
for(int i=0;i<n;i++)
{
if(i==s||i==t)continue;
for(int j=0;j<n;j++)
{
tmp[i][j]=ans[i][j];
tmp[j][i]=ans[j][i];
ans[j][i]=0;
ans[i][j]=0;
//printf("i=%d,j=%d,ansij=%d,ansji=%d\n",i+1,j+1,ans[i][j],ans[j][i]);
}
// for(int i=0;i<n;i++)for(int j=0;j<n;j++)printf("i=%d,j=%d,ans=%d\n",1+i,j+1,ans[i][j]);
buildgraph();
int tmpans=Dinic_flow();
// printf("i=%d,tmpans=%d\n",i+1,tmpans);
if(flowmax>tmpans)
{
flowmax--;
printf("%d ",i+1);
}
else
{
for(int j=0;j<n;j++)
{
ans[i][j]=tmp[i][j];
ans[j][i]=tmp[j][i];
}
}
}
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: