POJ 1815 - Friendship - [拆点最大流求最小点割集][暴力枚举求升序割点] - [Dinic算法模板 - 邻接矩阵型]
妖怪题目,做到现在:2017/8/19 - 1:41……
不过想想还是值得的,至少邻接矩阵型的Dinic算法模板get√
题目链接:http://poj.org/problem?id=1815
Time Limit: 2000MS Memory Limit: 20000K
Description
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
You can assume that the number of 1s will not exceed 5000 in the input.
Output
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
1 1 0
1 1 1
0 1 1
Sample Output
1
2
总的来说,就是求最小点割集,做法参考:
http://www.cnblogs.com/lochan/p/3870697.html
http://wugj03.blog.163.com/blog/static/1737650582011219115316710/
#include<cstdio>
#include<cstring>
#include<queue>
#define in(x) x
#define out(x) x+n
#define MAX 500
#define INF 0x3f3f3f3f
using namespace std;
struct Dinic{
int s,t,nv;//源点、汇点、点总数
int c[MAX][MAX],f[MAX][MAX],lev[MAX];
bool vis[MAX];
void addedge(int from,int to,int cap)
{
c[from][to]=cap, f[from][to]=;
c[to][from]=, f[to][from]=;
}
bool bfs()
{
memset(vis,,sizeof(vis));
queue<int> q;
q.push(s);
vis[s]=;
lev[s]=;
while(!q.empty())
{
int u=q.front();q.pop();
for(int v=;v<=nv;v++)
{
if(!vis[v] && c[u][v]>f[u][v])//属于残存网络的边
{
lev[v]=lev[u]+;
q.push(v);
vis[v]=;
}
} }
return vis[t];
}
int dfs(int u,int aug)
{
if(u==t) return aug;
int res=aug,tmp;
for(int v=;v<=nv;v++)
{
if(lev[v]==lev[u]+ && c[u][v]>f[u][v])
{
tmp=dfs(v,min(aug,c[u][v]-f[u][v]));
f[u][v]+=tmp;
f[v][u]-=tmp;
aug-=tmp;
}
}
return res-aug;
}
int maxflow()
{
int res=;
while(bfs()) res+=dfs(s,INF);
return res;
}
}dinic; int n,S,T;
int main(){
int a;
scanf("%d%d%d",&n,&S,&T);
dinic.nv=n*, dinic.s=out(S), dinic.t=in(T);
for(int i=;i<=n;++i)
{
if(i!=dinic.s && i!=dinic.t) dinic.addedge(in(i),out(i),);
for(int j=,tmp;j<=n;j++)
{
scanf("%d",&tmp);
if(i!=j && tmp) dinic.addedge(out(i),in(j),INF);
}
}
if(dinic.c[dinic.s][dinic.t]){
puts("NO ANSWER!\n");
return ;
}
int ans=dinic.maxflow();
printf("%d\n",ans); for(int i=;i<=n && ans;i++)
{
if(i==dinic.s|| i==dinic.t || !dinic.f[in(i)][out(i)]) continue;
memset(dinic.f,,sizeof(dinic.f));
dinic.c[in(i)][out(i)]=;
if(dinic.maxflow()<ans)
{
ans--;
printf("%d ",i);
}
else dinic.c[in(i)][out(i)]=;
}
printf("\n");
return ;
}
PS.为了方便后续使用该模板,把它也封装在一个struct里了,1~63行为模板。
POJ 1815 - Friendship - [拆点最大流求最小点割集][暴力枚举求升序割点] - [Dinic算法模板 - 邻接矩阵型]的更多相关文章
- POJ 1273 Drainage Ditches(网络流dinic算法模板)
POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdi ...
- HDU1532最大流 Edmonds-Karp,Dinic算法 模板
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- poj 1815 Friendship (最小割+拆点+枚举)
题意: 就在一个给定的无向图中至少应该去掉几个顶点才干使得s和t不联通. 算法: 假设s和t直接相连输出no answer. 把每一个点拆成两个点v和v'',这两个点之间连一条权值为1的边(残余容量) ...
- POJ 1815 Friendship(最小割)
http://poj.org/problem? id=1815 Friendship Time Limit: 2000MS Memory Limit: 20000K Total Submissio ...
- POJ 1815 Friendship(字典序最小的最小割)
Friendship Time Limit: 2000MS Memory Limit: 20000K Total Submissions: 10744 Accepted: 2984 Descr ...
- POJ - 1815 Friendship (最小点割集)
(点击此处查看原题) 题目分析 题意:有n个人,编号记为1~n,n个人之间可能有人可以互相联系,如果A能和B联系,那么至少满足这两种情况之一:(1)A知道B的电话(2)A可以和C联系,并且C可以和B联 ...
- POJ 1815 Friendship ★(字典序最小点割集)
[题意]给出一个无向图,和图中的两个点s,t.求至少去掉几个点后才能使得s和t不连通,输出这样的点集并使其字典序最大. 不错的题,有助于更好的理解最小割和求解最小割的方法~ [思路] 问题模型很简单, ...
- POJ 3469.Dual Core CPU 最大流dinic算法模板
Dual Core CPU Time Limit: 15000MS Memory Limit: 131072K Total Submissions: 24830 Accepted: 10756 ...
- POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]
题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...
随机推荐
- GLIBC_2.14报错
[linux]提示"libc.so.6: version `GLIBC_2.14' not found",系统的glibc版本太低 0.以下在系统CentOS 6.3 x86_64 ...
- apk反编译看包名什么的
首先默认你是装了java环境的 到google code里面下载apktool1.5.2.tar.bz2和apktool-install-windows-r05-ibot.tar.bz2(地址:htt ...
- myeclipse环境优化
在项目右键打开Project > Properties > BUILDERS,打开source的tab,选择你的目录,删之~重启myeclipse 以下转载自百度知道 优化一下,下面内容都 ...
- C数组&结构体&联合体快速初始化
背景 C89标准规定初始化语句的元素以固定顺序出现,该顺序即待初始化数组或结构体元素的定义顺序. C99标准新增指定初始化(Designated Initializer),即可按照任意顺序对数组某些元 ...
- java基础---->git的使用(一)
这里面记录一下git的使用,只是平时工作中遇到的一些问题的解决方案,不会涉及到git的一些基础概念及说明.人的天性便是这般凉薄,只要拿更好的来换,一定舍得. Git的一些使用 一.在码云建立好仓库之后 ...
- c++11 delete禁用函数
c++11添加了delete关键字. 不想用那个函数,在那个函数后面加 = delete就可以了: 比如: 在函数重载中,可用 delete 来滤掉一些函数的形参类型,如下: bool IsLucky ...
- Esper学习之九:EPL语法(五)
本篇的内容主要包括了Subquery(也就是子查询)和Join,内容不少,但是不难,基本上和sql差不太多. 1.Subquery EPL里的Subquery和sql的类似,是否比sql的用法更多我不 ...
- linux 设置pip 镜像 Pip Warning:–trusted-host 问题解决方案
pip升级到7.0以后,在使用http镜像进行包安装及升级的时候往往会有如下提示: Collecting beautifulsoup4The repository located at mirrors ...
- Maven —— scope 元素的值及其含义
1.compile 缺省值,所属依赖在所有的classpath中可用,同时它们也会被打包(随着项目一起发布). 2.provided 只有当JDK或者某个容器已提供该依赖之后才使用.如servlet. ...
- laravel curl post json
<?php namespace App\BO; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; us ...