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联 ...
随机推荐
- RefreshListView中onItemClick点击错位
在使用RefreshListView的时候.发现有使用 /**** * parent.getAdapter().getItem(position)√ * adpter.getItem(id);√ * ...
- 浅谈压缩感知(十四):傅里叶矩阵与小波变换矩阵的MATLAB实现
主要内容: 傅里叶矩阵及其MATLAB实现 小波变换矩阵及其MATLAB实现 傅里叶矩阵及其MATLAB实现 傅里叶矩阵的定义:(来源: http://mathworld.wolfram.com/F ...
- Solidworks如何在零件表面贴图
在要增加图片的表面上右击,然后选择修改这个面的颜色(可以是曲面) 切换到高级,然后选择一个图片 你可以缩放图片的大小,从而决定图片的重复次数 如果图片上下或者左右颠倒了,可以在映射里面勾选 ...
- Jenkins Xcode 证书设置错误 Code Sign error: No matching codesigning identity found: No codesigning identities
Jenkins 集成Xcode 项目的时候在证书上遇到了问题.实际上如果在本地的话.只要Xcode工程里选择了项目就不需要重新设置证书了.jenkins会自动找到这个证书,只要在build setti ...
- 微软BI 之SSAS 系列 - 在 SQL Server 2012 下查看 SSAS 分析服务的模型以及几个模型的简单介绍
在SSDT中部署一个 SSAS 项目到本地服务器上出现错误. You cannot deploy the model because the localhost deployment server i ...
- 第七周 Word文档修订
come from:http://www.sxszjzx.com/~c20/12-2/office-gj/ 第七周 Word文档修订 教学时间 2013-4-8 教学课时 2 教案序号 12 教学目标 ...
- 将Spring-boot应用部署到Docker容器
1:Docker中设置阿里云加速 使用阿里云的加速器,因为在使用docker的时候,会需要从docker的网站下载镜像文件,下载速度可能会很慢.获得阿里云加速,需要登录阿里云开发者平台,然后点击右侧的 ...
- UART,SPI,IIC的一点理解
转自:http://bbs.21ic.com/icview-253715-1-1.html UART通用异步收发器,UART是通用的异步传输模式,在它这种基础上加上其他接口或者解码器就衍生出多种异步传 ...
- Android 调用webservice并解析
这是调用webService的具体方法 private final static String nameSpace="http://tempuri.org/"; private f ...
- hadoop mahout 算法和API说明
org.apache.mahout.cf.taste.hadoop.item.RecommenderJob.main(args) --input 偏好数据路径,文本文件.格式 userid\t ite ...