poj 1815 Friendship (最小割+拆点+枚举)
题意:
就在一个给定的无向图中至少应该去掉几个顶点才干使得s和t不联通。
算法:
假设s和t直接相连输出no answer。
把每一个点拆成两个点v和v'',这两个点之间连一条权值为1的边(残余容量)
v和v''各自是一个流进的点。一个流出的点。
依据求最小割的性质。权值小的边是可能被选择的(断开的)。
加入源点st=0和汇点en=2*n+1,源点与s连权值为inf的边。t''与汇点连权值为inf的边。
s与s'',t与t''连权值为inf的边,这样保证自己和自己是不会失去联系的。
假设i和j有边相连。则i''和j连权值为inf的边。j''与i连权值为inf的边。
这样建图后跑最大流,求得的流量即为点的个数。
然后编号从小到大枚举每一个点。尝试去掉这个点(即仅仅进不出)。又一次建图再跑最大流。
看最大流是否会减小。假设减小了,就是要去掉的点。记录下来最后输出就能够了。
PS:
建图也能够不加源点和汇点,直接没去掉的点,拆的两点直接连权值为1的边,有边相连的
两点连权值为INF的边。
最终理解了我写的Dinic模板一直是直接处理残余网络即e[i].val的。
还有把容量网络和流量分开写的Dinic。
#include<cstdio>
#include<iostream>
#include<cstring>
#define INF 0x3f3f3f3f
#define maxn 210
#define maxm 160000
using namespace std; struct node
{
int v,val,next;
}e[maxm<<1];
int head[maxn<<1],mp[maxn][maxn],cnt,st,en,s,t,n;
int d[maxn<<1],q[maxn<<1],mm[maxn],del[maxn]; void init()
{
memset(del,0,sizeof(del));
memset(mp,0,sizeof(mp));
st = 0;
en = 2*n+1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
scanf("%d",&mp[i][j]);
}
} void add(int x,int y,int z)
{
e[cnt].v = y;
e[cnt].val = z;
e[cnt].next = head[x];
head[x] = cnt++;
e[cnt].v = x;
e[cnt].val = 0;
e[cnt].next = head[y];
head[y] = cnt++;
} void build()
{
memset(head,-1,sizeof(head));
cnt = 0;
add(st,s,INF);
add(t+n,en,INF);
for(int i=1;i<=n;i++)
{
if(!del[i]) add(i,i+n,1);
for(int j=1;j<=n;j++)
{
if(mp[i][j])
add(i+n,j,INF);
}
}
add(s,s+n,INF);
add(t,t+n,INF);
}
bool bfs()
{
memset(d,-1,sizeof(d));
int f = 0,r = 0,u;
q[r++] = st;
d[st] = 0;
while(f<r)
{
u = q[f++];
for(int i=head[u];i!=-1;i=e[i].next)
{
int t = e[i].v;
if(e[i].val>0 && d[t]==-1)//>0
{
d[t] = d[u]+1;
q[r++] = t;
if(t==en) return true;
}
}
}
return false;
} int dfs(int x,int flow)
{
if(x==en) return flow;
int ret = 0,dd;
for(int i=head[x];ret<flow && i!=-1;i=e[i].next)
{
int t = e[i].v;
if(d[t] == d[x]+1 && e[i].val)
{
dd = dfs(t,min(flow,e[i].val));
e[i].val-=dd;
e[i^1].val+=dd;
flow-=dd;
ret+=dd;
}
}
if(!ret) d[x]=-1;
return ret;
}
int Dinic()
{
int tmp = 0,maxflow = 0;
while(bfs())
{
while(tmp=dfs(st,INF))
maxflow+=tmp;
}
return maxflow;
} void solve()
{
if(mp[s][t])
{
printf("NO ANSWER!\n");
return;
}
build();
int ans = Dinic();
printf("%d\n",ans);
if(!ans) return;
int tmp = ans,f = 0,now;
for(int i=1;i<=n;i++)
{
if(i==s || i==t) continue;
//if(!mp[s][i]) continue; //点i尽管与s不是直接连通。但可能间接连通,所以枚举时不能continue掉
del[i] = 1;
build();
now = Dinic();
if(now<tmp)
{
mm[f++] = i;
tmp = now;
}
else
del[i] = 0;
}
for(int i=0;i<f-1;i++)
printf("%d ",mm[i]);
printf("%d\n",mm[f-1]);
}
int main()
{
while(scanf("%d%d%d",&n,&s,&t)!=EOF)
{
init();
solve();
}
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 */
poj 1815 Friendship (最小割+拆点+枚举)的更多相关文章
- POJ 1815 Friendship(最小割)
http://poj.org/problem? id=1815 Friendship Time Limit: 2000MS Memory Limit: 20000K Total Submissio ...
- poj 1815(最小割、割集)
题目链接:http://poj.org/problem?id=1815 思路:题目要求是剔除多少个点,可以将其转化为剔除多少条边,因此需要拆点,将点i拆成i,i+n,便容量为1,表示每个人起的传递作用 ...
- POJ - 1815 Friendship (最小点割集)
(点击此处查看原题) 题目分析 题意:有n个人,编号记为1~n,n个人之间可能有人可以互相联系,如果A能和B联系,那么至少满足这两种情况之一:(1)A知道B的电话(2)A可以和C联系,并且C可以和B联 ...
- POJ 1815 Friendship(字典序最小的最小割)
Friendship Time Limit: 2000MS Memory Limit: 20000K Total Submissions: 10744 Accepted: 2984 Descr ...
- POJ 1815 Friendship (Dinic 最小割)
Friendship Time Limit: 2000MS Memory Limit: 20000K Total Submissions: 8025 Accepted: 2224 Descri ...
- POJ 1815 Friendship(最小割+字典序输出割点)
http://poj.org/problem?id=1815 题意: 在现代社会,每个人都有自己的朋友.由于每个人都很忙,他们只通过电话联系.你可以假定A可以和B保持联系,当且仅当:①A知道B的电话号 ...
- poj 1815 Friendship 字典序最小+最小割
题目链接:http://poj.org/problem?id=1815 In modern society, each person has his own friends. Since all th ...
- poj 1815 Friendship【最小割】
网络流的题总是出各种奇怪的错啊--没写过邻接表版的dinic,然后bfs扫到t点不直接return 1就会TTTTTLE-- 题目中的操作是"去掉人",很容易想到拆点,套路一般是( ...
- POJ 1815 Friendship(最大流最小割の字典序割点集)
Description In modern society, each person has his own friends. Since all the people are very busy, ...
随机推荐
- Facebook 调试工具Stetho配置入门
I decided to spend a few hours on Stetho.Stetho is a sophisticated debug bridge for Android applicat ...
- 对config配置文件的读取和修改
在c#中想要使用对congfig文件的操作必要引用一个dll“system.configuration.dll” 读取 : string str= System.Configuration.Conf ...
- shell命令实战详解
1.解析路径获取文件名和目录名. 获取文件名 #awk解法:用“/”做分隔符,然后打印出最后的那一部分. resFile=`echo /tmp/csdn/zhengyi/test/adb.l ...
- C#控件、窗体置顶
//控件置于顶层和底层 panel.BringToFront();//置于顶层 panel.SendToBack();//置于底层 //窗体置顶 TopMost = true;
- Generator & Co
Generator 搬运自 http://es6.ruanyifeng.com/#docs/generator 如果没有babel等环境也可以在线体验 可以在http://www.es6fiddle. ...
- Android中pendingIntent的深入理解
pendingIntent字面意义:等待的,未决定的Intent.要得到一个pendingIntent对象,使用方法类的静态方法 getActivity(Context, int, Intent, i ...
- iOS6和iOS7代码的适配(4)——tableView
iOS7上不少控件的样子有了变化(毕竟要扁平化嘛),不过感觉变化最大的肯定非tableView莫属.因为这个控件的高度可定制性,原先是使用及其广泛的,这样的一个改变自然也影响颇大. 1.accesso ...
- poj2013---二维数组指针使用
#include <stdio.h> #include <stdlib.h> #include<string.h> int main() { ; ][],arr2[ ...
- 奔五的人,准备学习iOS开发
这些年一直在java/web/android方面折腾,去年最终换成了apple的设备,本想就開始折腾iOS,却始终没能进入状态. 从今天開始,本人宣布:正式进入iOS/xcode 5的编程学习中,也希 ...
- android 内存优化
OOM 内存泄漏引起很多问题: 1:节目卡顿.反应慢(高内存使用情况JVM 虚拟机的频繁离职GC) 2:消失 3:直接崩溃 ANDROID 内存面临的问题 1: 有限的堆内存,原始仅仅有16M 2:内 ...