poj 1815 Friendship 字典序最小+最小割
题目链接:http://poj.org/problem?id=1815
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.
题目描述:n个人,给出一些关系(两个人之间直接联系或可以间接联系),现在破坏一些人,使S和T这两个人不能联系。求出最小的人数(输出字典序最小的方案)。
算法分析:最小割解之,这个不用说了。重点在于怎么求解字典序最小:由于节点较少,我们可以一一枚举节点u,然后去掉u->u'后求解最小割是否会使最小割变小,是则必须删掉此边。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#define inf 0x7fffffff
using namespace std;
const int maxn=,M=; int n,from,to;
struct node
{
int v,flow;
int next;
} edge[M*],save[M*];
int head[maxn],edgenum; void add(int u,int v,int flow)
{
edge[edgenum].v=v ;edge[edgenum].flow=flow ;
edge[edgenum].next=head[u] ;head[u]=edgenum++; edge[edgenum].v=u ;edge[edgenum].flow=;
edge[edgenum].next=head[v] ;head[v]=edgenum++;
} int d[maxn];
int bfs()
{
memset(d,,sizeof(d));
d[from]=;
queue<int> Q;
Q.push(from);
while (!Q.empty())
{
int u=Q.front() ;Q.pop() ;
for (int i=head[u] ;i!=- ;i=edge[i].next)
{
int v=edge[i].v;
if (!d[v] && edge[i].flow)
{
d[v]=d[u]+;
Q.push(v);
if (v==to) return ;
}
}
}
return ;
} int dfs(int u,int flow)
{
if (u==to || flow==) return flow;
int cap=flow;
for (int i=head[u] ;i!=- ;i=edge[i].next)
{
int v=edge[i].v;
if (d[v]==d[u]+ && edge[i].flow)
{
int x=dfs(v,min(cap,edge[i].flow));
edge[i].flow -= x;
edge[i^].flow += x;
cap -= x;
if (cap==) return flow;
}
}
return flow-cap;
} int dinic()
{
int ans=;
while (bfs()) ans += dfs(from,inf);
return ans;
} int an[maxn][maxn];
int main()
{
while(scanf("%d%d%d",&n,&from,&to)!=EOF)
{
bool flag=false;
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
scanf("%d",&an[i][j]);
}
}
if(an[from][to])
{
printf("NO ANSWER!\n");
continue;
}
memset(head,-,sizeof(head));
edgenum=;
for (int i= ;i<=n ;i++)
{
if (i!=from && i!=to)
add(i,i+n,);
for (int j= ;j<=n ;j++)
{
if (an[i][j]&&i!=j)
{
if (i==from)
add(i,j,inf);
else if (i!=to)
add(i+n,j,inf);
}
}
}
memcpy(save,edge,sizeof(node)*edgenum);
int ans=dinic();
printf("%d\n",ans);
for (int i= ;i<=n ;i++)
{
if (i!=from && i!=to)
for (int j=head[i] ;j!=- ;j=edge[j].next)
{
if (edge[j].flow== && edge[j].v==i+n)
{
save[j].flow=save[j^].flow=;
memcpy(edge,save,sizeof(node)*edgenum);
if (dinic()!=ans-)
{
save[j].flow=;
save[j^].flow=;
continue;
}
if (flag)
printf(" ");
else
flag=true;
printf("%d",i);
ans--;
break;
}
}
}
printf("\n");
}
return ;
}
poj 1815 Friendship 字典序最小+最小割的更多相关文章
- POJ 1815 Friendship (Dinic 最小割)
Friendship Time Limit: 2000MS Memory Limit: 20000K Total Submissions: 8025 Accepted: 2224 Descri ...
- POJ 1815 Friendship ★(字典序最小点割集)
[题意]给出一个无向图,和图中的两个点s,t.求至少去掉几个点后才能使得s和t不连通,输出这样的点集并使其字典序最大. 不错的题,有助于更好的理解最小割和求解最小割的方法~ [思路] 问题模型很简单, ...
- 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 Friendship Time Limit: 2000MS Memory Limit: 20000K Total Submissio ...
- POJ 1815 Friendship(最小割+字典序输出割点)
http://poj.org/problem?id=1815 题意: 在现代社会,每个人都有自己的朋友.由于每个人都很忙,他们只通过电话联系.你可以假定A可以和B保持联系,当且仅当:①A知道B的电话号 ...
- poj 1815 Friendship (最小割+拆点+枚举)
题意: 就在一个给定的无向图中至少应该去掉几个顶点才干使得s和t不联通. 算法: 假设s和t直接相连输出no answer. 把每一个点拆成两个点v和v'',这两个点之间连一条权值为1的边(残余容量) ...
- poj 1815 Friendship【最小割】
网络流的题总是出各种奇怪的错啊--没写过邻接表版的dinic,然后bfs扫到t点不直接return 1就会TTTTTLE-- 题目中的操作是"去掉人",很容易想到拆点,套路一般是( ...
- POJ 1815 - Friendship - [拆点最大流求最小点割集][暴力枚举求升序割点] - [Dinic算法模板 - 邻接矩阵型]
妖怪题目,做到现在:2017/8/19 - 1:41…… 不过想想还是值得的,至少邻接矩阵型的Dinic算法模板get√ 题目链接:http://poj.org/problem?id=1815 Tim ...
- POJ 1815 Friendship(最大流最小割の字典序割点集)
Description In modern society, each person has his own friends. Since all the people are very busy, ...
随机推荐
- 微软ASP.NET MVC 学习地址
微软ASP.NET MVC4.0学习地址:http://www.asp.net/mvc
- mysql查询语句(mysql学习笔记七)
Sql语句 一般顺序GHOL : group by,having ,order by,limit 如果是分组,应该使用对分组字段进行排序的group by语法 ...
- Ubuntu14.04 切换root账户su root失败解决办法
原因是需要备份一个vimrc,可是cp就提示Permission denied. su root就提示su: Authentication failure 解决办法: sudo passwd root ...
- 低功耗蓝牙(BLE)透传模块 ——RF-BM-S01(BQB认证)
本文来源深圳信驰达科技www.szrfstar.com,技术交流群336720020. 低功耗蓝牙(BLE)透传模块 ——RF-BM-S01(BQB认证) 深圳市信驰达科技有限公司 2013年3月18 ...
- C 函数可变参数
C 函数可变参数 C 语言中用 ... 表示可变参数,例如: void fun(int x ...) 头文件 cstdarg.h 中包含可变参数类型va_list和处理可变参数的三个宏: va_lis ...
- 菜鸟学习Spring——60s让你学会动态代理原理
一.为什么要使用动态代理 当一个对象或多个对象实现了N中方法的时候,由于业务需求需要把这个对象和多个对象的N个方法加入一个共同的方法,比如把所有对象的所有方法加入事务这个时候有三种方法 ...
- MIFARE系列7《安全性》
飞利浦的MIFARE卡由于它的高安全性在市场上得到广泛应用,比如我们乘车用的公交卡,学校和企业食堂的饭卡等等.它每个扇区有独立的密匙(6个字节的密码),在通信过程中首先要验证密匙才能读写数据.它的关键 ...
- Transact-SQL 语句
当流程控制语句必须执行一个包含两条或两条以上Transact-SQL语句块时,可以使用BEGIN...END语句进行控制 use testDB; go declare @name varchar() ...
- Android--将Bitmip转化成字符串
因为自己做的东西想要上传到服务器,所以就选择了将Bitmip转化成了字符串在上传 其它格式的图片我们好像可以用Bitmap.Factory 去将他们转化成BitMap 转化成字符串的代码 //将bit ...
- hdu 4941 Magical Forest
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4941 Magical Forest Description There is a forest can ...