您的位置:首页 > 理论基础 > 计算机网络

hdu 1815 网络流寻找字典序最小割集 好题

2013-08-27 23:45 337 查看
Friendship

Time Limit: 2000MS Memory Limit: 20000K
Total Submissions: 8259 Accepted: 2306
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
 
题意:
给N个人,给出这N个人之间的关系,如果A能联系上B的话,那么B也就能联系上A,给出S,T,问至少要去掉几个人,才能使S不能联系上T。  并且输出割集  而且割集的字典序最小 

 
思路:
 
利用拆点法 拆点  建图
 
 
将每个点拆成两个点u和u',之间连容量为1的边,原来从u到v的边,变成从u'到v的边,边容量都是无穷大,新源点为s',新汇点还是t
 
之后找割边   用枚举找  从最小的点开始找
 
由于割边的减少会使得最大流减小所以如下
 

枚举思路:

 枚举一条边,并计算没有这条边的最大流,如最大流减少,则删去此边,记下此时的最大流,否则,不删边,枚举下一条边;这样反复枚举,计算,记录,直到最后最大流变为0.枚举结束,求得最小割的边。

 

  

#include<vector>
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
#define INF 0x3f3f3f3f
#define MAXN 5000
struct Edge{
int from,to,cap,flow;
};

struct Dinic{
int n,m,s,t;
vector<Edge> edges;
vector<int> G[MAXN];
bool vis[MAXN];
int d[MAXN];
int cur[MAXN];
void init(int n){
this->n=n;
for(int i=0;i<=n;i++)G[i].clear();
edges.clear();
}
void AddEdge(int from,int to,int cap){
Edge e;
e.from=from;e.to=to;e.cap=cap;e.flow=0;
edges.push_back(e);
e.from=from;e.to=to;e.cap=0;e.flow=0;
edges.push_back(e);
m=edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool BFS(){
memset(vis,0,sizeof(vis));
queue<int> Q;
Q.push(s);
d[s]=0;
vis[s]=1;
while(!Q.empty()){
int x=Q.front();
Q.pop();
for(int i=0;i<(int)G[x].size();i++){
Edge& e=edges[G[x][i]];
if(!vis[e.to]&&e.cap>e.flow){
vis[e.to]=1;
d[e.to]=d[x]+1;
Q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x,int a){
if(x==t||a==0)return a;
int flow=0,f;
for(int& i=cur[x];i<(int)G[x].size();i++){
Edge& e=edges[G[x][i]];
if(d[x]+1==d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>0){
e.flow+=f;
edges[G[x][i]^1].flow-=f;
flow+=f;
a-=f;
if(a==0)break;
}
}
return flow;
}
int Maxflow(int s,int t,int need){
this->s=s;this->t=t;
int flow=0;
while(BFS()){
memset(cur,0,sizeof(cur));
flow+=DFS(s,INF);
if(flow>need)return flow;
}
return flow;
}
//最小割割边
vector<int> Mincut(){
BFS();
vector<int> ans;
for(int i=0;i<(int)edges.size();i++){
Edge& e=edges[i];
if(vis[e.from]&&!vis[e.to]&&e.cap>0)ans.push_back(i);
}
return ans;
}
void Reduce(){
for(int i = 0; i <(int)edges.size(); i++) edges[i].cap -= edges[i].flow;
}
void ClearFlow(){
for(int i = 0; i <(int)edges.size(); i++) edges[i].flow = 0;
}
};
Dinic g;
int map[222][222],Erase[222];
int getflow(int n,int src,int des)
{
int i;
g.init(2*n);
src=src+n;
for(i=1;i<=n;i++)
if(!Erase[i])
g.AddEdge(i,i+n,1);
else g.AddEdge(i,i+n,0);
for(i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j) continue;
if(map[i][j]==1)
g.AddEdge(i+n,j,INF);
}
}
return g.Maxflow(src,des,INF);
}

int main()
{
int n,i,j,src,des;
while(scanf("%d %d %d",&n,&src,&des)!=EOF)
{
memset(Erase,0,sizeof(Erase));
memset(map,0,sizeof(map));
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&map[i][j]);
int maxflow=getflow(n,src,des);
if(map[src][des]) {printf("NO ANSWER!\n");continue;}
else
printf("%d\n",maxflow);
vector<int>ans;
for(i=1;i<=n;i++)
{
if(i==src||i==des) continue;

Erase[i]=1;
int flow=getflow(n,src,des);
if(flow<maxflow)
{
ans.push_back(i);
maxflow=flow;
}
else Erase[i]=0;
if(maxflow<=0) break;

}
int cnt=ans.size();
if(cnt==0) continue;

for(i=0;i<cnt-1;i++)
printf("%d ",ans[i]);
printf("%d\n",ans[i]);
}
return 0;
}


 

 
 
 
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: