妖怪题目,做到现在:2017/8/19 - 1:41……

不过想想还是值得的,至少邻接矩阵型的Dinic算法模板get√

题目链接:http://poj.org/problem?id=1815

Time Limit: 2000MS Memory Limit: 20000K

Description

In modern society, each person has his own friends. Since all the people are very busy, they communicate with each other only by phone. You can assume that people A can keep in touch with people B, only if 
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

The first line of the input contains three integers N (2<=N<=200), S and T ( 1 <= S, T <= N , and S is not equal to T).Each of the following N lines contains N integers. If i knows j's number, then the j-th number in the (i+1)-th line will be 1, otherwise the number will be 0.

You can assume that the number of 1s will not exceed 5000 in the input.

Output

If there is no way to make A lose touch with B, print "NO ANSWER!" in a single line. Otherwise, the first line contains a single number t, which is the minimal number you have got, and if t is not zero, the second line is needed, which contains t integers in ascending order that indicate the number of people who meet bad things. The integers are separated by a single space.

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算法模板 - 邻接矩阵型]的更多相关文章

  1. POJ 1273 Drainage Ditches(网络流dinic算法模板)

    POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdi ...

  2. HDU1532最大流 Edmonds-Karp,Dinic算法 模板

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...

  3. poj 1815 Friendship (最小割+拆点+枚举)

    题意: 就在一个给定的无向图中至少应该去掉几个顶点才干使得s和t不联通. 算法: 假设s和t直接相连输出no answer. 把每一个点拆成两个点v和v'',这两个点之间连一条权值为1的边(残余容量) ...

  4. POJ 1815 Friendship(最小割)

    http://poj.org/problem? id=1815 Friendship Time Limit: 2000MS   Memory Limit: 20000K Total Submissio ...

  5. POJ 1815 Friendship(字典序最小的最小割)

    Friendship Time Limit: 2000MS   Memory Limit: 20000K Total Submissions: 10744   Accepted: 2984 Descr ...

  6. POJ - 1815 Friendship (最小点割集)

    (点击此处查看原题) 题目分析 题意:有n个人,编号记为1~n,n个人之间可能有人可以互相联系,如果A能和B联系,那么至少满足这两种情况之一:(1)A知道B的电话(2)A可以和C联系,并且C可以和B联 ...

  7. POJ 1815 Friendship ★(字典序最小点割集)

    [题意]给出一个无向图,和图中的两个点s,t.求至少去掉几个点后才能使得s和t不连通,输出这样的点集并使其字典序最大. 不错的题,有助于更好的理解最小割和求解最小割的方法~ [思路] 问题模型很简单, ...

  8. POJ 3469.Dual Core CPU 最大流dinic算法模板

    Dual Core CPU Time Limit: 15000MS   Memory Limit: 131072K Total Submissions: 24830   Accepted: 10756 ...

  9. POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]

    题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...

随机推荐

  1. 用Eclipse编写Android程序的代码提示功能

    用Eclipse编写Android程序的代码提示功能主要是在java和xml文件中,有时候会失效,默认的提示功能有限. 1)java文件自动提示     Window->Preferences- ...

  2. 使用npm国内镜像

    嫌npm指令速度慢的童鞋可以把npm的源转换成国内的即可提高响应速度: 镜像使用方法(三种办法任意一种都能解决问题,建议使用第1或者第3种,将配置写死,下次用的时候配置还在):1.通过config命令 ...

  3. Asp.net动态生成表单

    control.ascx <%@ Control Language="C#" AutoEventWireup="true" CodeBehind=&quo ...

  4. MySql 错误 Err [Imp] 1153 - Got a packet bigger than 'max_allowed_packet' bytes

    今天在用Navicat导入SQL文件时报错:MySql 错误 Err [Imp] 1153 - Got a packet bigger than 'max_allowed_packet' bytes ...

  5. Netty权威指南之BIO(Block Input/Output,同步阻塞I/O通信)通信模型

    网络编程的基本模型是Client/Server模型,也就是两个进程之间进行相互通信,其中服务端提供位置信息(绑定的IP地址和监听端口),客户端通过连接操作向服务端监听的地址发起连接请求,通过三次握手建 ...

  6. kohana 简单使用

    声明:基于公司使用的 Kohana 框架写的,不确定是否适用于原生 Kohana 附:Kohana 3 中文手册,传送门:http://www.lampblog.net/kohana3%E4%BD%B ...

  7. Bypass 360主机卫士SQL注入防御(多姿势)

    0x00 前言 在服务器客户端领域,曾经出现过一款360主机卫士,目前已停止更新和维护,官网都打不开了,但服务器中依然经常可以看到它的身影.从半年前的测试虚拟机里面,翻出了360主机卫士Apache版 ...

  8. Nginx(十一)-- keepalived简介

    1. 什么是keepalived 基于VRRP(虚拟路由器冗余协议)来实现对web服务的高可用方案. keepalived下载地址:http://download.csdn.net/detail/u0 ...

  9. GLIBC_2.14报错

    [linux]提示"libc.so.6: version `GLIBC_2.14' not found",系统的glibc版本太低 0.以下在系统CentOS 6.3 x86_64 ...

  10. 编写java的时候出现“编码GBK的不可映射字符”

    今天在编写文件的时候,使用 javac ***.java 但是java文件里面会出现一些中文的信息,So:会报错 方法: 加参数-encoding UTF-8 例如:javac -encodig UT ...