POJ 1815 Friendship (Dinic 最小割)
|
Friendship
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 Sample Output 1 Source |
题意:有n个人,他们两两之间能够联系的要求是直接能够联系或者能同时联系到某一个人,现在给出A B两人,问最少删除多少个人后,A B无法联系,如果有多种方案,输出字典序最小的
算法:枚举+最大流
每个点拆点,之间连一条容量为1的弧,保证每个人最多被删掉一次
新增源点s汇点t,s只与x相连,t只与y’相连,容量均为+oo
如果i能与j联系,则在I’与J之间连一条容量为1的弧
求最大流并记录
如果x与y直接相连,则输出no answer并退出,如果x与y不连通(即最大流量为0),则输出0
然后从1到n一次删除该点(如果是x或y则不操作),如果删除该点后,最大流不变,则该点不是必要的,再恢复,最后得到tot值,即为删去的点,在枚举时用b数组标记每个点是否被删去,最后将被删去的点依次输出,满足字典序的条件
首先是构图,比较容易看出,这是最小割可以做的题目。
因为是无向图
首先是将每个点拆点,比如将i点拆成i,i+n点,两点之间连边,边长为1,这么设置可以让每个人的作用只出现一次,也能被最小割割到。
接着就是若两点之间相连,那么就让i+n和j相连,容量为无穷大。从而构造了最小割模型
注意S,T这两个点的构造有所不同,连向S+N,T+N的边的容量是无穷大,这样可以让他们不被最小割割中。。。
这个模型可以说是比较经典的一个模型了
主要是按字母序输出这个比较麻烦。
因为人数是一定的,可以依次枚举。
每次枚举一个人,把和他的联系全部断开,重新构图。
如果断开这个人的联系得到的流比原流小,那么就将原流减一,并对这个人做记录。如果不比原流小,那么就还原图
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue> using namespace std; const int VM=;
const int EM=;
const int INF=0x3f3f3f3f; struct Edge{
int to,nxt;
int cap;
}edge[EM<<]; int n,cnt,head[VM],src,des;
int dep[VM]; void addedge(int cu,int cv,int cw){ //虽然是无向图建边,但是是拆点建边,所以反向边依然为0
edge[cnt].to=cv; edge[cnt].cap=cw; edge[cnt].nxt=head[cu];
head[cu]=cnt++;
edge[cnt].to=cu; edge[cnt].cap=; edge[cnt].nxt=head[cv];
head[cv]=cnt++;
} int BFS(){
queue<int> q;
while(!q.empty())
q.pop();
memset(dep,-,sizeof(dep));
dep[src]=;
q.push(src);
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=head[u];i!=-;i=edge[i].nxt){
int v=edge[i].to;
if(edge[i].cap> && dep[v]==-){
dep[v]=dep[u]+;
q.push(v);
}
}
}
return dep[des]!=-;
} int DFS(int u,int minx){
if(u==des)
return minx;
int tmp;
for(int i=head[u];i!=-;i=edge[i].nxt){
int v=edge[i].to;
if(edge[i].cap> && dep[v]==dep[u]+ && (tmp=DFS(v,min(minx,edge[i].cap)))){
edge[i].cap-=tmp;
edge[i^].cap+=tmp;
return tmp;
}
}
dep[u]=-;
return ;
} int Dinic(){
int ans=,tmp;
while(BFS()){
while(){
tmp=DFS(src,INF);
if(tmp==)
break;
ans+=tmp;
}
}
return ans;
} int s,t,map[VM][VM],g[VM][VM]; void buildgraph(){
cnt=;
memset(head,-,sizeof(head));
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(i!=j){
if(map[i][j]==) //拆点建边,连通着的两点边权为INF保证不会成为割边
addedge(n+i,j,INF);
}else{
if(i==s || i==t) //s点,t点,题意表明不会出现问题,即拆成的两点不会成为割边
addedge(i,n+i,INF);
else
addedge(i,n+i,); //其余点则有可能成为割边,边权为1保证只切割一次
}
} int main(){ //freopen("input.txt","r",stdin); int res[VM];
while(~scanf("%d%d%d",&n,&s,&t)){
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
scanf("%d",&map[i][j]);
if(map[s][t]==){
puts("NO ANSWER!");
continue;
}
buildgraph();
src=s, des=n+t;
int ans=Dinic(); //原始最小割,即原来最多的相互联系的人数
printf("%d\n",ans);
if(ans==)
continue;
int tot=;
for(int i=;i<=n;i++){ //枚举去掉每一个人,(不包括s和t)
if(i==s || i==t)
continue;
//memcpy(g,map,sizeof(map)); //用这个函数超时啊,下同
for(int j=;j<=n;j++)
for(int k=;k<=n;k++){
g[j][k]=map[j][k];
if(j==i || k==i)
map[j][k]=;
}
buildgraph();
int tmp=Dinic();
if(ans>tmp){
ans--; //ans>tmp说明去掉这个人,联系变少了,所以原始最大流减一,并保存该人的id号
res[tot++]=i;
}else{
//memcpy(map,g,sizeof(g));
for(int j=;j<=n;j++) //最大流不变则恢复原图
for(int k=;k<=n;k++)
map[j][k]=g[j][k];
}
if(tmp==) //当tmp==0时,说明s与t已经无法联系了,所以得到的是最优值,-->结束
break;
}
for(int i=;i<tot-;i++)
printf("%d ",res[i]);
printf("%d\n",res[tot-]);
}
return ;
}
POJ 1815 Friendship (Dinic 最小割)的更多相关文章
- POJ 1815 Friendship(最小割+字典序输出割点)
http://poj.org/problem?id=1815 题意: 在现代社会,每个人都有自己的朋友.由于每个人都很忙,他们只通过电话联系.你可以假定A可以和B保持联系,当且仅当:①A知道B的电话号 ...
- poj 1815 Friendship【最小割】
网络流的题总是出各种奇怪的错啊--没写过邻接表版的dinic,然后bfs扫到t点不直接return 1就会TTTTTLE-- 题目中的操作是"去掉人",很容易想到拆点,套路一般是( ...
- poj 1815 Friendship 字典序最小+最小割
题目链接:http://poj.org/problem?id=1815 In modern society, each person has his own friends. Since all th ...
- POJ 1815 Friendship ★(字典序最小点割集)
[题意]给出一个无向图,和图中的两个点s,t.求至少去掉几个点后才能使得s和t不连通,输出这样的点集并使其字典序最大. 不错的题,有助于更好的理解最小割和求解最小割的方法~ [思路] 问题模型很简单, ...
- POJ 1815 Friendship(最大流最小割の字典序割点集)
Description In modern society, each person has his own friends. Since all the people are very busy, ...
- POJ 1815 Friendship(字典序最小的最小割)
Friendship Time Limit: 2000MS Memory Limit: 20000K Total Submissions: 10744 Accepted: 2984 Descr ...
- POJ 1815 Friendship(最小割)
http://poj.org/problem? id=1815 Friendship Time Limit: 2000MS Memory Limit: 20000K Total Submissio ...
- poj 1815 Friendship (最小割+拆点+枚举)
题意: 就在一个给定的无向图中至少应该去掉几个顶点才干使得s和t不联通. 算法: 假设s和t直接相连输出no answer. 把每一个点拆成两个点v和v'',这两个点之间连一条权值为1的边(残余容量) ...
- POJ - 1815 Friendship (最小点割集)
(点击此处查看原题) 题目分析 题意:有n个人,编号记为1~n,n个人之间可能有人可以互相联系,如果A能和B联系,那么至少满足这两种情况之一:(1)A知道B的电话(2)A可以和C联系,并且C可以和B联 ...
随机推荐
- PHP使用DOM XML操作XML[总结]
1.前言 XML树状层次结构鲜明,非常适合作为配置文件.PHP中可以使用DOM XML来操作XML.本文总结一下PHP使用DOM XML创建.添加节点.查询XML文件. 2.使用DOM XML XML ...
- java 中xml转换为json对象
1.前提须要jar包: json-lib-2.4-jdk15.jar 和 xom-1.2.5.jar ,maven 仓库: net.sf.json-lib json-lib 2.4 jdk15 xom ...
- 怎样在一个项目里用logger在控制台打印信息
第一步: 导入jar包,maven项目可以直接添加 <dependency><groupId>log4j</groupId><artifactId>lo ...
- Solidworks公司电脑图纸被加密之后如何解密输出
第一步:打开总装配的组件(该组件需要包含你所有需要的零件),比如打开其中一个: 第二步:Solidworks的菜单中依次:"文件"→"打包"(有的版本是pa ...
- C++ 第十一课 标准c内存函数
calloc() 分配一个二维储存空间 free() 释放已分配空间 malloc() 分配空间 realloc() 改变已分配空间的大小 calloc 语法: #include <st ...
- 持续集成篇_08_Hudson持续集成服务器的使用(自动化编译、分析、打包、部署)
持续集成篇_08_Hudson持续集成服务器的使用(自动化编译.分析.打包.部署) 1.创建任务 svn用户验证 验证通过 *****五颗*表示每分钟检查svn路径是否有变更,有变更就会重新构建,相当 ...
- JDK5.0特性-线程 Callable和Future
来自:http://www.cnblogs.com/taven/archive/2011/12/17/2291466.html import java.util.concurrent.Callable ...
- map集合中value()、keySet()、entrySet()区别
在Map集合中 values():方法是获取集合中的所有的值----没有键,没有对应关系, KeySet():将Map中所有的键存入到set集合中.因为set具备迭代器.所有可以迭代方式取出所有的键, ...
- 前台登录和Token信息交互流程
原来总是对前台登录,怎么利用token有点迷惑,后面仔细的想了一遍,把自己简单的想法记录下来,留作记录,以便后续优化 各路大神有什么看法也可以说,能更完善整个流程. 不说了,暴力的上图: 该图是出自c ...
- MySQL:Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT. INSERT...
1:错误日志大量错误 150602 14:40:02 [Warning] Unsafe statement written to the binary log using statement form ...