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不连通,输出这样的点集并使其字典序最大. 不错的题,有助于更好的理解最小割和求解最小割的方法~ [思路] 问题模型很简单, ...
随机推荐
- 牛客网暑期ACM多校训练营(第九场)D
链接:https://www.nowcoder.com/acm/contest/147/D来源:牛客网 Niuniu likes traveling. Now he will travel on a ...
- Codeforces 645D Robot Rapping Results Report【拓扑排序+二分】
题目链接: http://codeforces.com/problemset/problem/645/D 题意: 给定n个机器人的m个能力大小关系,问你至少要前几个大小关系就可以得到所有机器人的能力顺 ...
- CodeForces 582A【multiset使用样例】
题意: 给一些无序的数字,求解一个矩阵,使得矩阵的每一个元素都是行和列标志数的gcd,输出行标志数. 首先对数字进行排序.复杂度n*log(n^2). 这题的证明有官方的英文题解==在这直接贴英文题解 ...
- hdu6200 mustedge mustedge mustedge (并查集+dfs序树状数组)
题意 给定一个n个点m条边无向图(n,m<=1e5) 支持两个操作 1.添加一条边 2.询问点u到点v的所有路径中必经边的条数 操作数<=1e5 分析 第一眼看起来像是要动态维护无向图的边 ...
- 2018.11.3 PION模拟赛
期望:100 实际:100 #include<cstdio> #include<cstring> #include<iostream> #include< ...
- lemon oa前端页面——由user-base-list谈项目组织
content user-base-list.jsp中指定<%pageContext.setAttribute("currentHeader", "user&quo ...
- 【转】c++内存管理学习纲要
http://blog.csdn.net/zhanghefu/article/details/5003407 转自:http://blog.csdn.net/wdzxl198/article/deta ...
- linux动态库的种种要点
linux下使用动态库,基本用起来还是非常easy.但假设我们的程序中大量使用动态库来实现各种框架/插件,那么就会遇到一些坑,掌握这些坑才有利于程序更稳健地执行. 本篇先谈谈动态库符号方面的问题. 測 ...
- glTF格式初步了解
glTF格式初步了解 近期看到Qt 3D的进展.偶然了解到了一种新的格式:glTF格式.这样的格式据说比现有的3D格式更加符合OpenGL应用的须要.这引起了我的好奇.于是我在Qt 3D的外部链接中找 ...
- E. Dreamoon and Strings(Codeforces Round #272)
E. Dreamoon and Strings time limit per test 1 second memory limit per test 256 megabytes input stand ...