poj 1815 Friendship【最小割】
网络流的题总是出各种奇怪的错啊……没写过邻接表版的dinic,然后bfs扫到t点不直接return 1就会TTTTTLE……
题目中的操作是“去掉人”,很容易想到拆点,套路一般是(s,i')(i,t)(u',v)(v,u),然后关于输出顺序最小方案,可以枚举删点,如果删去一个点后求得最小割比原来的小,那么说明这个点在最小割中,就在图中删掉它并且把最小割标准-1,否则把这个点加回来。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int N=505,inf=1e9;
int n,a[N][N],s,t,mx,le[N],ans[N],c[N][N];
bool del[N];
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
void build()
{
for(int i=1;i<=n;i++)
if(!del[i])
c[i][i+n]=1;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(a[i][j])
c[i+n][j]=inf;
}
bool bfs()
{
queue<int>q;
memset(le,0,sizeof(le));
le[s]=1;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int v=1;v<=n+n;v++)
if(!le[v]&&c[u][v]>0)
{
le[v]=le[u]+1;
if(v==t)
return 1;//!!!!!!
q.push(v);
}
}
return 0;
}
int dfs(int u,int f)
{
if(u==t||!f)
return f;
int us=0;
for(int v=1;v<=n+n&&us<f;v++)
if(le[u]+1==le[v]&&c[u][v]>0)
{
int t=dfs(v,min(c[u][v],f-us));
c[u][v]-=t;
c[v][u]+=t;
us+=t;
}
if(!us)
le[u]=0;
return us;
}
int dinic()
{
int re=0;
while(bfs())
re+=dfs(s,inf);//,cout<<re<<endl;
return re;
}
int main()
{
n=read(),s=read(),t=read();
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
a[i][j]=read();
if(a[s][t])
{
puts("NO ANSWER!");
return 0;
}
s+=n;
build();
mx=dinic();//printf("%d\n",mx);
for(int i=1;i<=n;i++)
if(i!=s-n&&i!=t)
{//cout<<i<<endl;
del[i]=1;
build();
if(dinic()<mx)
{
ans[++ans[0]]=i;
mx--;
if(!mx)
break;
}
else
del[i]=0;
}
printf("%d\n",ans[0]);
for(int i=1;i<=ans[0];i++)
printf("%d ",ans[i]);
return 0;
}
poj 1815 Friendship【最小割】的更多相关文章
- 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(最小割、割集)
题目链接: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 字典序最小+最小割
题目链接:http://poj.org/problem?id=1815 In modern society, each person has his own friends. Since all th ...
- 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 ★(字典序最小点割集)
[题意]给出一个无向图,和图中的两个点s,t.求至少去掉几个点后才能使得s和t不连通,输出这样的点集并使其字典序最大. 不错的题,有助于更好的理解最小割和求解最小割的方法~ [思路] 问题模型很简单, ...
随机推荐
- sgu179 SGU起航!
//发现dfs除了搜索功能外的其他功能,他本身是一种序列,这个题恰是"先序"的下一个(合法范围内)序列! #include<iostream> #include< ...
- solr相关文章
Solr集群架构概述及delta-import详细配置 背景 由于项目原因,重新熟悉了下Solr,版本为3.6,搭建了主从Solr服务,并使用DIH从RDBMS数据源增量更新索引. 其实也没什么技术含 ...
- HDU 5883 欧拉路径异或值最大 水题
The Best Path Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...
- Exchanger使用
Exchanger使用
- LeetCode 67 Add Binary(二进制相加)(*)
翻译 给定两个二进制字符串,返回它们的和(也是二进制字符串). 比如, a = "11" b = "1" 返回 "100". 原文 Give ...
- js中window.onload 与 jquery中$(document.ready()) 測试
js中window.onload 与 jquery中$(document.ready())差别,验证代码例如以下(调换js代码和Jquer代码书写顺序測试.执行结果一样.因此与代码书写位置没关系): ...
- CDN具体解释(篇二)
还有还有一个问题就是全部的内容都放在同一个地方.假设我们的server在芝加哥,那么美国中西部的人们訪问server的响应时间和用户体验就比香港.德国.南非以及佛罗里达州的用户好.由于那些用户离ser ...
- B+树在NTFS文件系统中的应用
B+树在NTFS文件系统中的应用 flyfish 2015-7-6 卷(volume) NTFS的结构首先从卷開始. 卷相应于磁盘上的一个逻辑分区,当你将一个磁盘或者磁盘的一部分格式化成NTFS,卷将 ...
- .net面试整理
NET程序员的层次:http://blog.csdn.net/dinglang_2009/article/details/6913852 .NET牛人应该知道些什么http://www.douban. ...
- strtok函数
strtok函数是cstring文件里的函数 strtok函数是cstring文件里的函数 其功能是截断字符串 原型为:char *strtok(char s[],const char *delin) ...