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 ...
随机推荐
- 高可用(HA)架构
http://aokunsang.iteye.com/blog/2053719 浅谈web应用的负载均衡.集群.高可用(HA)解决方案 http://zhuanlan.51cto.com/art/ ...
- Linux+Redis实战教程_day01_常用命令【重点】
3.常用命令[重点] Linux命令中参数,一般都是无序的.特殊情况下除外 3.1.磁盘管理命令 ls命令:列出目录内容 参数: -a 查询所有文件和文件夹.包含隐藏的 -l 查询详细列表 ls ...
- 一个java程序员的年终总结
年底了,该给自己写点总结了! 从毕业到现在已经快4年啦,一直在Java的WEB开发行业混迹.我不是牛人,但是自我感觉还算是个合格的程序员,有必要写下自己将近4年来的经历,给自我以提示,给刚入行的朋友提 ...
- Python中的类(下)
本文将介绍一下类的构造函数和初始化函数,以及如何通过"魔术方法"定制一个类. 类构造和初始化 在前面的文章中,经常使用初始化函数"__init__",下面看看& ...
- 线程同步 –Mutex和Semaphore
上一篇介绍了同步事件EventWaitHandle,以及它的两个子类型AutoResetEvent和ManualResetEvent.下面接着介绍WaitHandle的另外两个子类型Mutex和Sem ...
- Docker应用之容器
容器是独立运行的一个或一组应用,以及他们的运行态环境 1.启动容器(基于镜像新建一个容器并启动或将终止状态的容器重新启动) run后面添加--name参数可以指定容器的名称,否则系统默认会给名称:使用 ...
- 【RF库Collections测试】Reverse List
Name:Reverse ListSource:Collections <test library>Arguments:[ list_ ]Reverses the given list i ...
- openjdk源码阅读导航
转自:http://rednaxelafx.iteye.com/blog/1549577 这是链接帖.主体内容都在各链接中. 怕放草稿箱里过会儿又坑掉了,总之先发出来再说…回头再慢慢补充内容. 先把I ...
- Java并发编程--BlockingQueue
概述 BlockingQueue支持两个附加操作的Queue:1)当Queue为空时,获取元素线程被阻塞直到Queue变为非空:2)当Queue满时,添加元素线程被阻塞直到Queue不满.Blocki ...
- windows内核情景分析之—— KeRaiseIrql函数与KeLowerIrql()函数
windows内核情景分析之—— KeRaiseIrql函数与KeLowerIrql()函数 1.KeRaiseIrql函数 这个 KeRaiseIrql() 只是简单地调用 hal 模块的 KfRa ...